17 minute read

Master Debian System Administration: Essential Commands for Efficient Linux Management in Part 4.

Introduction:

Welcome to the fourth section of our comprehensive guide, “Linux CLI Essentials for Debian Users.” In this segment, we delve into the intricacies of managing and administering your Linux system.

Before we embark on this journey into system administration, let’s take a moment to revisit what we’ve covered thus far. In the previous section, you gained the knowledge and skills to explore your computer’s vital statistics and monitor its performance effectively.

Now, in Part 4, we shift our focus to system administration—an essential aspect of maintaining a healthy and efficient Debian environment. Here, we will explore commands such as sudo, dpkg, apt-get, kill, htop, hostname, history, and systemctl. These commands empower you to take control, from managing packages and repositories to monitoring processes and system services.

System administration is at the core of maintaining a secure, well-functioning system. Whether you’re an IT professional or an enthusiast seeking to enhance your CLI skills, this section equips you with the tools and knowledge needed to manage your workstation effectively.

System Administration:

28. sudo - execute a command as a superuser:

The sudo command in Linux is a powerful tool that allows a user to execute a command with superuser or administrative privileges. This enables the user to perform tasks that require elevated permissions, such as system configuration, package installation, and file management. Here’s a breakdown of the sudo command:

  1. Command Name: sudo
    • This is the command, used to execute another command with superuser privileges.

Syntax:

The basic syntax of the sudo command is as follows:

sudo [options] command [arguments]
  • [options]: These are optional flags that you can use to customize the behaviour of sudo.
  • command: This is the command you want to execute with superuser privileges.
  • [arguments]: These are optional arguments that you can pass to the command being executed.

Examples:

  1. Installing software:
  sudo apt-get install package-name

This command uses sudo to execute the apt-get install command with superuser privileges, allowing you to install a package.

  1. Updating the system:
  sudo apt-get update

Running sudo with the apt-get update command updates the package repository information, which requires administrative access.

  1. Viewing system logs:
  sudo tail -f /var/log/syslog

This command uses sudo to view the system log file, which is typically restricted to the root user.

  1. Accessing a directory normally only accessible to root:
  sudo cd /root

In this example, sudo is used to change the current directory to /root, which is a directory normally accessible only to the root user.

Options and Modifiers:

The sudo command has several options and modifiers that allow you to customize its behaviour:

  • -b: Run the command in the background.
  • -e: Edit a file using the user’s default editor (usually vi or nano).
  • -i: Run a login shell as the target user, which sets up the environment as if you had logged in as that user.
  • -s: Run the shell specified in the SHELL environment variable.
  • -u: Execute the command as a user other than root. You can specify the username with this option.
  • -l: Display the commands allowed by the security policy.
  • -v: Update the user’s time stamp without running a command. Useful for extending the sudo session without executing a new command.

These options can be used to tailor the behaviour of sudo to your specific needs. For example, using sudo -i will start a login shell with root privileges, providing a complete root environment.

Using sudo is a powerful way to perform administrative tasks while maintaining security, as it requires authentication before granting superuser access. However, it’s essential to use sudo with caution to prevent unintentional damage to the system.

29. dpkg - Install, query, and manage Debian package files directly:

The dpkg command is a powerful tool in Debian-based Linux distributions for installing, querying, and managing Debian package files directly. Debian packages (with the .deb extension) contain software, libraries, and configuration files that can be installed on your system. Here’s an explanation of the dpkg command:

  1. Command Name: dpkg
    • This is the primary command used to interact with Debian package files.

Syntax:

The basic syntax of the dpkg command is as follows:

dpkg [options] <action>
  • [options]: These are optional flags that customize the behaviour of dpkg.
  • <action>: This is the action you want to perform with dpkg, such as installing, querying, or managing packages.

Examples:

  1. Installing a package:

To install a Debian package, you use the -i (or --install) option followed by the path to the package file:

  dpkg -i package.deb

This command installs the specified package on your system.

  1. Removing a package:

To remove a package, you use the -r (or --remove) option followed by the package name:

  dpkg -r package-name

This command removes the specified package from your system.

  1. Querying package information:

To query package information (e.g., package details, version, and status), you use the -l (or --list) option followed by the package name:

  dpkg -l package-name

This command displays detailed information about the specified package.

Options and Modifiers:

The dpkg command provides various options and modifiers to perform specific tasks:

  • -i (--install): Install a package.
  • -r (--remove): Remove a package.
  • -l (--list): List information about installed packages.
  • -s (--status): Display the status of a package.
  • -L (--listfiles): List the files installed by a package.
  • -p (--print-avail): Display information about an available package.
  • -I (--info): Display detailed information about an installed package.
  • -c (--contents): List the contents of an installed package.
  • -S (--search): Search for packages that own specific files.
  • -X (--purge): Purge (remove configuration files) a package.
  • --configure: Configure a package that has been unpacked but not yet configured.
  • --unpack: Unpack a package but do not configure it.
  • --audit: Show the integrity status of installed packages.
  • -t (--triggers-only): Perform triggers only for pending packages.
  • --no-triggers: Do not execute triggers.
  • -G (--get-selections): Get package selections for backup or restore.
  • --set-selections: Set package selections for installation.
  • --clear-selections: Clear all package selections.
  • --get-selections: Get package selections.
  • --compare-versions: Compare two package versions.
  • -A (--pending): Process packages with state “unpacked but not yet configured.”
  • --get-architecture: Display the system architecture.

These options and modifiers allow you to perform various package management tasks using the dpkg command, such as installing, removing, querying, and configuring Debian packages. It’s a fundamental tool for package management on Debian-based systems like Ubuntu.

30. apt-get - manage packages and repositories

The apt-get command is a powerful package management tool in Debian-based Linux systems, such as Ubuntu and Debian. It is used to manage software packages and repositories. Let’s break down its components, syntax, options, and provide examples:

  • Command Name: apt-get
  • This is the primary command used for package management.

Syntax:

The syntax of the apt-get command is as follows:

sudo apt-get [options] <command> [package-name]
  • options: Optional. These are various options that modify the behaviour of the apt-get command.
  • command: Required. This specifies the action to be performed, such as update, install, upgrade, remove, etc.
  • package-name: Optional. This is the name of the package you want to perform the action on. It is required for actions like install, upgrade, and remove.

Examples:

  1. Updating the Package Index:

To update the local package index to get the latest information about available packages, use the update command:

  sudo apt-get update
  1. Installing a Package:

To install a package, use the install command followed by the package name:

  sudo apt-get install package-name

Example:

  sudo apt-get install apache2

This will install the Apache web server.

  1. Upgrading Installed Packages:

To upgrade all installed packages to their latest versions, use the upgrade command:

  sudo apt-get upgrade
  1. Removing a Package:

To remove a package, use the remove command followed by the package name:

  sudo apt-get remove package-name

Example:

  sudo apt-get remove apache2

This will uninstall the Apache web server.

  1. Completely Removing a Package with Configuration Files:

To remove a package along with its configuration files, use the --purge option with the remove command:

  sudo apt-get --purge remove package-name

Example:

  sudo apt-get --purge remove apache2

Options and Modifiers:

The apt-get command provides various options and modifiers to customize its behaviour. Here are some commonly used ones:

  • -y: Automatically answers “yes” to prompts, allowing for unattended installation and upgrades. Example:

    sudo apt-get -y install package-name
    
  • -qq: Runs in quiet mode, suppressing most output messages. Example:

    sudo apt-get -qq update
    
  • --no-install-recommends: When installing packages, it prevents the installation of recommended packages, reducing the total number of installed packages. Example:

    sudo apt-get --no-install-recommends install package-name
    
  • --fix-broken: Attempts to fix broken dependencies automatically. Example:

    sudo apt-get --fix-broken install
    

The apt-get command is a vital tool for managing software packages and repositories on Debian-based Linux systems, simplifying tasks such as installation, upgrade, and removal of software packages.

31. kill - send a signal to a process to end:

The kill command in Linux is used to send a signal to a process, which can be used to terminate or manipulate its behaviour. Here’s a breakdown of the kill command, including its components, syntax, options, and examples:

  • Command Name: kill
  • This is the primary command used to send signals to processes.

Syntax:

The basic syntax of the kill command is as follows:

kill [options] [pid]
  • [options]: These are optional flags that customize the behaviour of the kill command.
  • [pid]: This is the Process ID of the target process you want to send a signal to.

Examples:

  1. Terminating a Process:

To terminate a process by sending the default SIGTERM signal, use the kill command followed by the Process ID (PID):

  kill 1234

This command will send the SIGTERM signal to the process with PID 1234, requesting it to terminate gracefully.

  1. Terminating a Process with a Specific Signal:

You can specify a different signal using the -s (or --signal) option:

  kill -s SIGKILL 5678

This command sends the SIGKILL signal to the process with PID 5678, forcefully terminating it. The SIGKILL signal cannot be ignored or caught by the process.

  1. Listing Available Signal Names:

To list available signal names, use the -l (or --list) option:

  kill -l

This command will display a list of signal names that can be used with the -s option.

Options and Modifiers:

The kill command provides various options and modifiers to control how signals are sent to processes:

  • **-s (--signal) **: Specify the signal to be sent to the process. You can use signal names like `SIGKILL`, `SIGTERM`, etc.
  • -l (--list): List available signal names.
  • -9 (--sigkill): Send the SIGKILL signal to forcefully terminate a process.
  • -15 (--sigterm): Send the SIGTERM signal to request graceful termination.
  • -19 (--sigstop): Send the SIGSTOP signal to suspend a process.
  • -18 (--sigcontinue): Send the SIGCONT signal to resume a suspended process.

These options allow you to send specific signals to processes, manage their behaviour, and gracefully terminate them when needed. The ability to control processes is essential for managing a Linux system effectively.

32. htop - advanced version of top for system monitoring:

The htop command is a powerful Linux terminal tool for monitoring system processes. It provides an interactive and user-friendly interface for displaying and managing processes, making it easier to monitor system resource usage and control running processes. Let’s break down its components, syntax, and options, along with examples:

  • Command Name: htop
  • This is the primary command to launch the htop process monitor.

Syntax:

The htop command has a straightforward syntax:

htop
  • Simply typing htop in the terminal will launch the htop process monitor.

Examples:

  1. Launching htop:

To start the htop process monitor, open a terminal and type:

  htop

This will launch htop, and you’ll see an interactive display of running processes and system resource usage.

  1. Sorting Processes by CPU Usage:

While running htop, you can sort processes by various criteria. For example, to sort by CPU usage, press the P key. This rearranges the processes with the highest CPU usage at the top.

  1. Sorting Processes by Memory Usage:

To sort processes by memory usage, press the M key while htop is running. This sorts the processes with the highest memory usage at the top.

  1. Killing a Process:

If you need to terminate a process, you can do so interactively within htop. Select the process using the arrow keys, and then press the F9 key. You’ll be prompted to confirm the process termination.

  1. Searching for a Specific Process:

To search for a specific process, press the / key and then type the name of the process you want to find. htop will highlight matching processes as you type.

  1. Viewing the Process Hierarchy:

To view the process hierarchy as a tree, press the t key. This displays parent-child relationships between processes, making it easier to understand process dependencies.

Options and Modifiers:

htop is primarily an interactive tool, and most of its functionality is accessible through its interactive interface. It doesn’t have a wide range of command-line options like some other commands. The examples above illustrate how to use its various features interactively.

In summary, htop is an advanced and interactive process monitoring tool for Linux systems, providing real-time insights into system resource usage and process management. Its interactive interface makes it a popular choice for system administrators and users who need to monitor and manage processes efficiently.

33. hostname - display the system hostname:

The hostname command in Linux is used to display or change the system’s hostname, which is the unique name that identifies a computer or device on a network. The hostname is essential for network communication and system identification. Let’s break down its components, syntax, options, and provide examples:

  • Command Name: hostname
  • This is the primary command used to view or modify the hostname.

Syntax:

The hostname command has a simple syntax:

  1. To display the current hostname:
  hostname
  1. To change the hostname (temporary):
  hostname [new_hostname]
  1. To change the hostname (permanent), you need to edit the /etc/hostname file:
  sudo nano /etc/hostname

After editing this file, you should also update the /etc/hosts file to include the new hostname:

  sudo nano /etc/hosts

Replace the old hostname with the new one in both files.

Examples:

  1. Displaying the Current Hostname:

To view the current hostname of your Linux system, simply run:

  hostname

This command will output the current hostname.

  1. Changing the Hostname Temporarily:

To change the hostname temporarily (it reverts after a reboot), use:

  hostname new_hostname

Replace new_hostname with the desired hostname. This change will take effect immediately but is not persistent.

  1. Changing the Hostname Permanently:

To change the hostname permanently, first use the hostname command to set the new hostname:

  hostname new_hostname

Then, edit the /etc/hostname file:

  sudo nano /etc/hostname

Replace the current hostname in the file with your desired hostname, save the file, and exit.

Next, edit the /etc/hosts file:

  sudo nano /etc/hosts

Find the line that starts with 127.0.0.1 and replace the old hostname with the new one, save the file, and exit.

Your hostname is now changed and will persist across reboots.

Options and Modifiers:

The hostname command doesn’t have many options or modifiers. It is primarily used for displaying or changing the hostname as described above. The main options are:

  • -i (--ip-address): Display the IP address associated with the hostname.
  • --fqdn: Display the fully qualified domain name.

Most users typically use the hostname command without any options to view or change the hostname as needed.

34. history - display a list of previously executed commands:

The history command in Linux is a handy feature for managing and recalling previously executed commands in the terminal. Let’s break down its components, syntax, options, and provide examples:

  • Command Name: history
  • This is the primary command used to display the command history.

Syntax:

The history command has a straightforward syntax:

history [options]

Examples:

  1. Displaying Command History:

To view a list of previously executed commands along with their command numbers, simply run:

  history

This command will display a numbered list of commands, with the most recent commands at the bottom. Example output:

  1 ls
  2 cd ~/Documents
  3 mkdir my_folder
  4 touch file.txt
  5 history

Each line in the output represents a command executed in the past, and the number at the beginning of each line is the command number.

  1. Executing a Specific Command from History:

To execute a specific command from the history, use the !<number> syntax, where <number> is the command number. For example, to re-run command number 2 from the history:

  !2

This will execute the cd ~/Documents command.

  1. Executing the Most Recent Command:

To execute the most recent command from the history, you can use:

  !!

This is a quick way to re-run the last command you executed.

  1. Executing the Most Recent Command Starting with a String:

If you want to execute the most recent command that starts with a specific string, you can use:

  !<string>

For example, to execute the most recent command that starts with ls:

  !ls

This will execute the most recent ls command from your history.

Options and Modifiers:

The history command doesn’t have many options or modifiers. By default, it displays the command history in reverse order (most recent commands at the bottom). However, you can use options to customize its behaviour:

  • -c: Clears the command history, erasing all previously executed commands.
  • -d <number>: Deletes a specific command from the history by specifying its command number. For example, to delete command number 3:
 history -d 3

After this, the command mkdir my_folder would be removed from the history.

  • -a: Appends new commands from the current session to the history file. By default, the history file is updated when you exit the terminal, but this option can force an immediate update.
  • -r: Reads the command history from the history file, replacing the current session’s history. Useful if you want to discard the current session’s history and reload from the saved file. Example:
history -c  # Clears the command history
history -a  # Appends the current session's history
history -r  # Reads the history from the file, replacing the current session's history

The history command is a valuable tool for managing and recalling commands in the Linux terminal, enhancing productivity and simplifying repetitive tasks.

35. systemctl – Controlling and managing the systemd system

The systemctl command in Linux is used for controlling and managing the systemd system and service manager, which is a core component of many modern Linux distributions. It allows you to interact with system services, units, and other aspects of the systemd system. Below, I’ll break down the components, provide examples, and explain some common options and modifiers:

Syntax:

systemctl [OPTIONS] [COMMAND] [UNIT]
  • OPTIONS: These are optional flags that modify the behavior of the systemctl command.
  • COMMAND: This specifies the action you want to perform (e.g., start, stop, enable, disable) on a systemd unit.
  • UNIT: This is the name of the systemd unit (service, socket, target, etc.) you want to operate on.

Common Commands:

  1. Start a Service:

    systemctl start service-name
    

    Example: systemctl start apache2

  2. Stop a Service:

    systemctl stop service-name
    

    Example: systemctl stop apache2

  3. Restart a Service:

    systemctl restart service-name
    

    Example: systemctl restart apache2

  4. Enable a Service to Start at Boot:

    systemctl enable service-name
    

    Example: systemctl enable apache2

  5. Disable a Service from Starting at Boot:

    systemctl disable service-name
    

    Example: systemctl disable apache2

  6. Check the Status of a Service:

    systemctl status service-name
    

    Example: systemctl status apache2

  7. List All Active Units:

    systemctl list-units
    

    Example: systemctl list-units

  8. List All Unit Files and Their States:

    systemctl list-unit-files
    

    Example: systemctl list-unit-files

Common Options:

  • -a or --all: Show all units/dependencies, including inactive ones.
  • --failed: Show only failed units.
  • --type=TYPE: Filter units by type (service, socket, target, etc.).
  • --user: Perform the action on the user’s systemd instance (requires user privileges).

Modifiers:

  • Service Unit Files: Most commonly, systemctl is used to manage services. Service unit files are typically stored in /etc/systemd/system/ or /lib/systemd/system/ and have a .service file extension.

  • Target Unit Files: These define custom states or groups of services. For example, multi-user.target represents the state where the system is in multi-user mode.

  • Socket Unit Files: Used for socket-activated services. They specify the sockets on which the service should listen.

  • Timer Unit Files: Used for systemd timers to schedule tasks.

  • Device Unit Files: Define devices that systemd manages.

  • Mount Unit Files: Specify mount points for filesystems.

  • Automount Unit Files: Define automount points for filesystems.

  • And more: systemd supports various other unit types for different purposes.

Keep in mind that systemctl commands typically require root or superuser privileges when dealing with system services.

Conclusion:

In this section, you’ve delved deep into the realm of system administration, acquiring a set of powerful commands and techniques for managing your system. From sudo, which allows you to execute commands as a superuser, to dpkg and apt-get, which facilitate package management and installation, you’ve gained essential skills for maintaining a well-configured system.

Commands like kill enable you to control processes, while htop offers advanced insights into system resource usage. hostname provides information about your system’s identity, and history allows you to review and reuse previously executed commands. With systemctl, you can take charge of system services, further enhancing your system administration capabilities.

As you conclude this section, you’re well-prepared to venture into the next part of our guide, “User Management & Terminal and Output.” Here, you’ll explore user management techniques and learn how to harness the full power of the terminal for efficient and effective Linux CLI usage.

By mastering these CLI essentials, you’re not only becoming a proficient user but also gaining valuable skills that can empower you in various IT roles and endeavours. Stay engaged, keep exploring, and embrace the power of the CLI as you advance to the next section of this guide.

Leave a comment