← Back to quizzesFree quiz

Linux Sysadmin Essential Commands

Welcome to the Linux Sysadmin Essential Commands course. This module is designed for aspiring and intermediate system administrators who want to master the most frequently used commands for…

5 questions~3 min
Linux Sysadmin Essential Commands — Qwi
0 / 5
Score: 0%
1

A junior sysadmin needs to replace a corrupted package configuration file without removing user data. Which command should they use?

2

During a service outage, you need to reload the Nginx configuration without stopping the service. Which systemctl option achieves this?

3

A mid‑level admin must extend a logical volume to use all free space in its volume group. Which command sequence is correct?

4

When configuring SELinux to allow Nginx to listen on port 8080, which command correctly updates the SELinux policy?

5

A junior sysadmin notices high CPU usage on a server. Which command combination best identifies the top CPU‑consuming processes?

Linux Sysadmin Essential Commands

Welcome to the Linux Sysadmin Essential Commands course. This module is designed for aspiring and intermediate system administrators who want to master the most frequently used commands for package management, service control, storage handling, SELinux policy adjustments, and performance monitoring. By the end of this lesson you will be able to confidently replace corrupted configuration files, reload services without downtime, extend logical volumes, modify SELinux rules, and pinpoint CPU‑intensive processes.

1. Managing Packages with apt

Package management is the backbone of any Debian‑based Linux distribution. While apt update refreshes the package index and apt install adds new software, the correct way to replace a corrupted configuration file without losing user data is to purge the package and then reinstall it.

  • Why use apt purge? The purge option removes the package **and** its configuration files from /etc. This ensures that any broken or outdated configuration is cleared, allowing a fresh copy to be installed.
  • Preserving user data: User data typically resides in /var or custom directories, not in the package’s default configuration path. Purging does not touch those locations.
  • Command syntax:
    sudo apt purge <package_name>
    sudo apt install <package_name>

After reinstalling, verify the new configuration with dpkg -L <package_name> and adjust any custom settings as needed.

2. Reloading Services with systemctl

Systemd’s systemctl utility provides fine‑grained control over services. When a configuration change is made to a running service—such as Nginx—you often want to apply the new settings without interrupting active connections.

  • Reload vs. Restart: restart stops and then starts the service, causing a brief downtime. reload tells the daemon to reread its configuration files while staying up.
  • Correct command for Nginx:
    systemctl reload nginx
  • When to use: After editing /etc/nginx/nginx.conf or any site‑specific file, run the reload command. If the configuration contains syntax errors, the reload will fail, leaving the current configuration untouched—providing a safety net.

Always check the service status after reloading:

systemctl status nginx

3. Extending Logical Volumes

Logical Volume Management (LVM) allows flexible storage allocation. A common task is to expand a logical volume (LV) to consume all remaining free space in its volume group (VG). The correct sequence involves two steps:

  • Resize the LV with lvextend using the +100%FREE flag, which tells LVM to allocate every free extent.
    lvextend -l +100%FREE /dev/vg/lv
  • Resize the filesystem. For ext4 filesystems, the appropriate tool is resize2fs.
    resize2fs /dev/vg/lv

Putting it together in a single line improves efficiency and reduces the chance of forgetting the second step:

lvextend -l +100%FREE /dev/vg/lv && resize2fs /dev/vg/lv

If the LV uses XFS, replace resize2fs with xfs_growfs and point it at the mount point instead of the block device.

4. Adjusting SELinux Policies for Custom Ports

Security‑Enhanced Linux (SELinux) enforces mandatory access controls. By default, the http_port_t type permits Nginx to bind only to standard ports (80, 443). To allow Nginx on a non‑standard port such as 8080, you must add the port to the SELinux policy.

  • Command to add a port:
    semanage port -a -t http_port_t -p tcp 8080
  • Explanation of flags:
    • -a – add a new rule.
    • -t http_port_t – assign the HTTP service type.
    • -p tcp – specify the protocol.
  • Verification: After adding the rule, confirm it with:
    semanage port -l | grep 8080

Remember to reload or restart Nginx after updating the policy so the daemon can bind to the newly permitted port.

5. Identifying CPU‑Intensive Processes

Performance troubleshooting often starts with locating the processes that consume the most CPU. While tools like top provide a live view, a quick, script‑friendly snapshot can be obtained with ps combined with sorting.

  • Command to list top consumers:
    ps aux --sort=-%cpu | head -10
  • Breakdown:
    • ps aux – displays all processes with detailed information.
    • --sort=-%cpu – sorts the output in descending order of CPU usage.
    • head -10 – shows only the first ten entries, i.e., the most demanding processes.
  • When to use: Ideal for automated monitoring scripts, log collection, or when you need a concise report without interactive navigation.

For continuous monitoring, pair ps with watch:

watch -n 5 "ps aux --sort=-%cpu | head -10"

6. Putting It All Together – A Real‑World Scenario

Imagine you are called to a production server where the web application is failing to start because Nginx cannot read its configuration file. Simultaneously, the system reports high CPU usage. Using the concepts covered, you would:

  1. Check the Nginx configuration syntax with nginx -t. If corrupted, purge and reinstall the package:
    sudo apt purge nginx && sudo apt install nginx
  2. Reload Nginx to apply the fresh configuration without downtime:
    systemctl reload nginx
  3. If the application requires more storage, extend the logical volume and resize the filesystem:
    lvextend -l +100%FREE /dev/vg/appdata && resize2fs /dev/vg/appdata
  4. Because the app now listens on port 8080, update SELinux:
    semanage port -a -t http_port_t -p tcp 8080
  5. Finally, identify any rogue processes consuming CPU:
    ps aux --sort=-%cpu | head -10

Document each step, verify service status, and communicate the changes to your team. This systematic approach minimizes downtime and maintains security compliance.

7. Quick Reference Cheat Sheet

  • Replace corrupted package config: sudo apt purge <pkg> && sudo apt install <pkg>
  • Reload a service: systemctl reload <service>
  • Extend LV (ext4): lvextend -l +100%FREE /dev/vg/lv && resize2fs /dev/vg/lv
  • Allow Nginx on custom port: semanage port -a -t http_port_t -p tcp 8080
  • Top CPU processes: ps aux --sort=-%cpu | head -10

8. Further Learning Resources

To deepen your expertise, explore the following official documentation and community guides:

  • apt(8) manual page
  • systemctl documentation
  • LVM management guide
  • SELinux policy management
  • ps command reference

By mastering these essential commands, you will be better equipped to maintain robust, secure, and high‑performance Linux environments.