Posted on
Advanced

Bash one-liners for system profiling

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Mastering Linux System Profiling with Bash One-Liners

For system administrators and developers, having a toolbox equipped with the right commands can make data gathering about system health and performance both thorough and efficient. Linux, with its robust set of utilities, provides an excellent platform for detailed system profiling.

In this post, we’re going to look at some powerful Bash one-liners that can help you quickly profile a Linux system. We will also cover the installation of necessary packages using different Linux package managers such as apt (for Debian-based systems), dnf (for Fedora and RHEL-based systems), and zypper (for openSUSE).

CPU and Memory Utilization

A basic yet crucial aspect of system profiling is monitoring CPU and memory usage. Here’s how you can quickly check it using the top command:

top -b -n 1 | head -5

This command runs top in batch mode (-b) to prevent it from running interactively and refreshes just once (-n 1), then pipes the first five lines (head -5) that contain your system’s load average and CPU/Memory usage.

Installing htop for Enhanced System Monitoring

For a more detailed view, htop provides an interactive monitor that visualizes processes’ resource usage more clearly than top. To install htop:

Debian/Ubuntu:

sudo apt install htop

Fedora:

sudo dnf install htop

openSUSE:

sudo zypper install htop

Once installed, run it by simply typing:

htop

Disk Usage

To check the disk usage by directories sorted by size you can use:

du -sh /* | sort -rh | head -10

This displays the size of each directory within the root directory in a human-readable format (-sh), sorts them (sort -rh) in human-readable (-h) reverse (-r) order, and then shows the top 10.

Network Connections and Listening Ports

Checking active network connections and listening ports can be critical, particularly for a system exposed to the internet. Use:

netstat -tuln

However, netstat might not be installed by default. Let's install net-tools:

Debian/Ubuntu:

sudo apt install net-tools

Fedora:

sudo dnf install net-tools

openSUSE:

sudo zypper install net-tools

Real-time Monitoring with vmstat

For real-time system performance analysis, vmstat is invaluable. Install it and execute it to view report on system processes, memory, paging, block I/O, traps, and CPU activity.

vmstat 1 5

This command means vmstat runs every second (1) for five seconds (5).

System Uptime

Checking how long the system has been running is as simple as:

uptime

Conclusion

These Bash one-liners provide a quick snapshot of your system’s health and are handy for both swift checks and deep dives into how the operating system is performing. Remember, while one-liners are powerful, they can also be destructive if used improperly. Always ensure you understand a command before running it, especially as the superuser.

Happy profiling, and may your systems run smoothly and swiftly!