Chapter 4 - Server Command Line Interface: Mastering the Terminal
This chapter covers the most important aspect of Linux: the terminal. The terminal is the beating heart of Linux, enabling you to accomplish virtually anything. For the first eight years I used Linux, I relied exclusively on the terminal, never even considering a graphical interface. The terminal is reliable, quick, and efficient, making it an indispensable tool for Linux users.
Pro Tip: The terminal is always accessible. If you're stuck on a black screen or graphical interface, press Ctrl + Alt + F2
or Ctrl + Alt + F3
to switch to a terminal. To return to the graphical interface, press Ctrl + Alt + F7
.
Navigating the Command Line Interface
Directory Movement Commands and Examples:
-
cd
(change directory):cd ..
(move up a directory)cd name
(move into the specified directory)cd /
(move to the root directory)cd ~
(move to the home directory, e.g.,/home/titus
)zoxide
- A better package for navigating the file system, using fuzzy finding and remembering past directories.
-
ls
(list files):ls -a
(show all files, including hidden ones)ls -l
(long listing format showing permissions)
-
rm
(remove files):rm -rf
(remove files/directories recursively and forcefully)rmdir
(remove empty directories)trash-cli
- An alternative torm
that moves files to the trash instead of permanently deleting them.
-
cp
(copy files):cp -r
(copy files and subdirectories recursively)rsync
- A more efficient tool for copying or syncing large amounts of files.
Directory Layout and File System Overview
/
- Root directory/bin
- System executable files/boot
- Bootloader files/dev
- Device files (requires mounting to browse)/etc
- System configuration files/home
- User files and configuration files/media
- Mounted external media/mnt
- Temporary mount points/opt
- Optional files/root
- Root user files and configurations/tmp
- Temporary files/usr
- System resources/var
- Logs, databases, and other variable data
Most interactions with the file system occur within the /home/user
directory. This includes configuration files and executable programs. Direct editing of system-wide configuration files in /etc
or adding programs to /bin
should be done sparingly.
System Services and Configuration
Modern Linux distributions use an "init system" to control system operations, primarily systemd. Systemd manages services, boot processes, networking, and more.
Controlling Services and Units
systemctl
is the primary tool for managing services and units in systemd. Units can represent services, device drivers, network mounts, timers (similar to crontab
), and more.
Common systemctl
Commands:
systemctl status servicename
- View the current status of a service.systemctl start servicename
- Start a service.systemctl stop servicename
- Stop a service.systemctl restart servicename
- Restart a service (stop and start).systemctl reload servicename
- Reload the configuration file without restarting the service.systemctl enable servicename
- Enable a service to start at system boot.systemctl is-enabled servicename
- Check if a service is enabled at startup.systemctl is-active servicename
- Check if a service is running and active.systemctl list-units
- List all running systemd units.systemctl list-units --all
- List all units, both active and inactive.systemctl list-units --all --state=inactive
- List all inactive units.systemctl list-units --all --type=service
- List all units of type "service."
Unit File Locations
Systemd unit files are typically located in the following directories:
/usr/lib/systemd
- The main location for system-created unit files./etc/systemd/system
- Contains system-wide unit files, often symbolic links to/usr/lib/systemd
. This directory has top priority when reading unit files.~/.config/systemd/user/
- Contains user-specific unit files. This directory is not created by default and requires the--user
option for systemctl commands.
Example System Unit File
Below is an example of a system unit file:
[Unit]
Description=service_description
After=network.target
[Service]
ExecStart=path_to_executable
Type=forking
[Install]
WantedBy=default.target
For a full reference, visit: Systemd Unit Documentation.
User-Based Systemd Services and Unit Files
User-specific unit files are stored in the ~/.config/systemd/user/
directory. These files can be managed using the --user
option with systemctl
.
Example Command:
systemctl --user start usercreatedfile.service
This command starts a user-created service file located in the user's home directory.
Example User Unit File:
[Unit]
Description=Run service as user
DefaultDependencies=no
After=network.target
[Service]
Type=simple
User=titus
Group=users
ExecStart=/home/titus/scripts/startup_script.sh
TimeoutStartSec=0
RemainAfterExit=yes
[Install]
WantedBy=default.target
Bluetooth Configuration
To configure Bluetooth, you can use the bluetoothctl
command-line tool. This tool allows you to manage Bluetooth devices and settings directly from the terminal.
Syntax for bluetoothctl
commands:
bluetoothctl [command]
Common bluetoothctl
Commands:
power on
- Turn on the Bluetooth adapter.power off
- Turn off the Bluetooth adapter.agent on
- Enable the agent for pairing.scan on
- Start scanning for nearby Bluetooth devices.scan off
- Stop scanning for Bluetooth devices.pair <device_address>
- Pair with a specific Bluetooth device.connect <device_address>
- Connect to a paired Bluetooth device.disconnect <device_address>
- Disconnect from a connected Bluetooth device.devices
- List all known Bluetooth devices.info <device_address>
- Show detailed information about a specific Bluetooth device.
Example Usage:
bluetoothctl
power on
agent on
scan on
This will turn on the Bluetooth adapter, enable the agent for pairing, and start scanning for nearby Bluetooth devices.
Note: blueman
is a graphical Bluetooth manager that can be used as an alternative to bluetoothctl
for those who prefer a GUI.
Audio Configuration
Linux provides several command-line tools for managing audio. The two main sound systems are ALSA (Advanced Linux Sound Architecture) and PulseAudio, with PulseAudio running on top of ALSA.
PulseAudio Commands (pactl
)
The pactl
command is used to control the PulseAudio sound server:
# List audio sources and sinks
pactl list sources # List input devices
pactl list sinks # List output devices
# Volume control
pactl set-sink-volume @DEFAULT_SINK@ 50% # Set volume to 50%
pactl set-sink-mute @DEFAULT_SINK@ toggle # Toggle mute
# List loaded modules
pactl list modules
ALSA Mixer (alsamixer
)
alsamixer
is a terminal-based mixer program for ALSA:
alsamixer # Open the mixer interface
Navigation in alsamixer
:
- Arrow keys: Navigate between channels
- Up/Down: Adjust volume
- M: Toggle mute
- F6: Select sound card
- Esc: Exit
Common Audio Troubleshooting Commands
# Restart PulseAudio
pulseaudio -k # Kill the PulseAudio daemon
pulseaudio --start # Start PulseAudio
# Check audio devices
aplay -l # List all ALSA playback devices
arecord -l # List all ALSA recording devices
# Test audio output
speaker-test -c 2 # Test stereo speakers
Audio Device Management with pavucontrol
While pavucontrol
is technically a GUI application, it can be installed and launched from the terminal:
sudo apt install pavucontrol # Install on Debian/Ubuntu
pavucontrol # Launch the control interface
Pipewire Audio System
Pipewire is the modern replacement for both PulseAudio and JACK. It maintains compatibility with PulseAudio clients, so pactl
commands work with Pipewire. Additionally, Pipewire provides its own tools:
# Check Pipewire status
pw-cli status
pw-top # Show real-time audio processing graph
# List audio devices
pw-dump # Detailed info about audio devices and nodes
pw-cli list-objects # List all Pipewire objects
# Control specific nodes
pw-cli node-id # Get ID of audio nodes
pw-metadata -n settings 0 clock.force-rate 48000 # Set sample rate
# Monitor audio
pw-mon # Monitor Pipewire events in real-time
You can still use PulseAudio tools with Pipewire:
pactl
commands work as shown abovepavucontrol
works normally- ALSA applications work without modification
Note: Most modern Linux distributions are transitioning to Pipewire as their default audio system due to its improved latency, Bluetooth handling, and compatibility with both PulseAudio and JACK applications.
Typical Pipewire Package Names:
pipewire
- Core Pipewire packagepipewire-pulse
- PulseAudio compatibility layerpipewire-jack
- JACK compatibility layerpipewire-alsa
- ALSA compatibility layerwireplumber
- Session manager for Pipewire
Note: Do NOT use pipewire-media-session, as it is deprecated and replaced by wireplumber.