Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

System Monitoring Commands Tutorial

1. Introduction

System monitoring is a crucial aspect of managing a Linux-based system. It involves keeping track of various system metrics such as CPU usage, memory usage, disk I/O, and network activity. This tutorial covers some of the fundamental commands used to monitor system performance in Linux.

2. The top Command

The top command provides a dynamic, real-time view of the system's performance. It displays a list of processes running on the system along with CPU and memory usage statistics.

Example usage:

top
top - 15:32:12 up 10 days,  3:45,  1 user,  load average: 0.58, 0.72, 0.75
Tasks: 187 total,   1 running, 186 sleeping,   0 stopped,   0 zombie
%Cpu(s):  2.0 us,  1.0 sy,  0.0 ni, 96.0 id,  0.0 wa,  0.0 hi,  1.0 si,  0.0 st
KiB Mem :  16340036 total,   819292 free,  11548440 used,   3970304 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  3580120 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND            
  123 root      20   0  162156   2048   1584 S   0.7  0.0   0:00.08 top                
  456 user      20   0  123456  56789   2345 S   0.3  0.3   0:05.42 some_process       
                

3. The htop Command

The htop command is an interactive process viewer for Unix systems. It is similar to top but provides a more user-friendly and colorful interface.

Example usage:

htop

Note: Install htop using sudo apt-get install htop if it's not already installed.

4. The vmstat Command

The vmstat (Virtual Memory Statistics) command reports information about processes, memory, paging, block IO, traps, and CPU activity.

Example usage:

vmstat 5
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 819292 3970304 11548440    0    0     0     1   11   13  2  1 96  0  0
 0  0      0 819292 3970304 11548440    0    0     0     1   11   13  2  0 97  0  0
                

5. The iostat Command

The iostat command is used for monitoring system input/output device loading by observing the time the devices are active relative to their average transfer rates.

Example usage:

iostat
Linux 4.15.0-20-generic (hostname) 	09/17/2023 	_x86_64_	(4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           2.00    0.00    1.00    0.00    0.00   97.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.50         1.00         2.00      1000      2000
                

6. The netstat Command

The netstat command displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

Example usage:

netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN
udp        0      0 0.0.0.0:68              0.0.0.0:*
udp        0      0 127.0.0.1:631           0.0.0.0:*
                

7. The sar Command

The sar command collects, reports, and saves system activity information. It can be used to report on various system loads and performance metrics.

Example usage:

sar -u 5 3
Linux 4.15.0-20-generic (hostname) 	09/17/2023 	_x86_64_	(4 CPU)

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:00:06 AM     all      2.00      0.00      1.00      0.00      0.00     97.00
12:00:11 AM     all      2.00      0.00      1.00      0.00      0.00     97.00
12:00:16 AM     all      2.00      0.00      1.00      0.00      0.00     97.00

Average:        all      2.00      0.00      1.00      0.00      0.00     97.00
                

8. Conclusion

This tutorial covered several basic yet powerful commands for monitoring system performance in a Linux environment. By using these commands, system administrators can effectively monitor and troubleshoot system performance issues. Always refer to the man pages (e.g., man top, man iostat) for more detailed information and additional options.