Introduction
In Linux systems, system monitoring and performance analysis are an indispensable part of the daily work of system administrators and developers. By monitoring linux performance, we can promptly identify and resolve potential issues, ensuring system stability and efficiency. This article will introduce 5 powerful Linux performance monitoring commands and showcase their capabilities through detailed usage examples.
1. Linux Performance Monitoring Commands – top
1.1 Overview
The top
command is one of the most commonly used real-time linux performance monitoring commands. It dynamically displays the activity of system processes, including the usage of CPU, memory, swap partition, and other resources.
1.2 Usage Examples
1.2.1 Basic Usage
By entering the top
command in the terminal, the system will display the current real-time status:
top
The output will be as follows:
top - 14:32:01 up 2:15, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3952.4 total, 1023.2 free, 1234.5 used, 1694.7 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2345.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 123456 12345 6789 R 0.3 0.3 0:00.01 top
1.2.2 Sorting by CPU Usage
In the top
interface, pressing the P
key will sort the processes by CPU usage:
top - 14:32:01 up 2:15, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3952.4 total, 1023.2 free, 1234.5 used, 1694.7 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2345.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 123456 12345 6789 R 0.3 0.3 0:00.01 top
1.2.3 Sorting by Memory Usage
In the top
interface, pressing the M
key will sort processes by memory usage:
top - 14:32:01 up 2:15, 3 users, load average: 0.00, 0.01, 0.05
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3952.4 total, 1023.2 free, 1234.5 used, 1694.7 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 2345.6 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 root 20 0 123456 12345 6789 R 0.3 0.3 0:00.01 top
1.3 Summary
The top
command is a very powerful real-time monitoring tool that helps us quickly understand the system’s operational state and resource usage. By sorting by CPU or memory usage, we can quickly identify processes that consume significant resources.
2. Linux Performance Monitoring Commands –vmstat
2.1 Overview
The vmstat
command is used to report virtual memory statistics, including processes, memory, paging, block IO, traps, and CPU activity. It helps us understand the overall performance status of the system.
2.2 Usage Examples
2.2.1 Basic Usage
By entering the vmstat
command in the terminal, the system will display the current virtual memory statistics:
vmstat
The output will be as follows:
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 1023456 123456 234567 0 0 12 34 567 1234 1 2 97 0 0
2.2.2 Periodic Refresh
We can use the periodic refresh feature of the vmstat
command to refresh statistics at regular intervals. For example, refreshing every 2 seconds:
vmstat 2
The output will be as follows:
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 1023456 123456 234567 0 0 12 34 567 1234 1 2 97 0 0
0 0 0 1023456 123456 234567 0 0 0 0 567 1234 1 2 97 0 0
2.2.3 Displaying a Specified Number of Statistics
We can use the vmstat
command to display statistics a specified number of times. For example, displaying statistics 5 times:
vmstat 2 5
The output will be as follows:
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 1023456 123456 234567 0 0 12 34 567 1234 1 2 97 0 0
0 0 0 1023456 123456 234567 0 0 0 0 567 1234 1 2 97 0 0
0 0 0 1023456 123456 234567 0 0 0 0 567 1234 1 2 97 0 0
0 0 0 1023456 123456 234567 0 0 0 0 567 1234 1 2 97 0 0
0 0 0 1023456 123456 234567 0 0 0 0 567 1234 1 2 97 0 0
2.3 Summary
The vmstat
command helps us comprehensively understand the usage of virtual memory, processes, CPU, and other resources. By periodically refreshing and displaying a specified number of statistics, we can better monitor system performance.
3. Linux Performance Monitoring Commands –iostat
3.1 Overview
The iostat
command is used to monitor the load of input/output devices in the system, including CPU utilization, device utilization, throughput, and other information. It helps us analyze disk IO performance issues.
3.2 Usage Examples
3.2.1 Basic Usage
By entering the iostat
command in the terminal, the system will display the current IO statistics:
iostat
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.23 0.00 0.45 0.12 0.00 98.20
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1.23 12.34 56.78 123456 567890
3.2.2 Periodic Refresh
We can use the periodic refresh feature of the iostat
command to refresh statistics at regular intervals. For example, refreshing every 2 seconds:
iostat 2
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.23 0.00 0.45 0.12 0.00 98.20
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1.23 12.34 56.78 123456 567890
sda 0.00 0.00 0.00 0 0
3.2.3 Displaying a Specified Number of Statistics
We can use the iostat
command to display statistics a specified number of times. For example, displaying statistics 5 times:
iostat 2 5
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
1.23 0.00 0.45 0.12 0.00 98.20
Device tps kB_read/s kB_wrtn/s kB_read kB_wrtn
sda 1.23 12.34 56.78 123456 567890
sda 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
sda 0.00 0.00 0.00 0 0
3.3 Summary
The iostat
command helps us monitor the IO device load and analyze disk IO performance issues. By periodically refreshing and displaying a specified number of statistics, we can better understand the system’s IO performance.
4. Linux Performance Monitoring Commands –netstat
4.1 Overview
The netstat
command is used to display information about network connections, routing tables, interface statistics, masquerade connections, multicast memberships, and more. It helps us monitor network connections and diagnose network issues.
4.2 Usage Examples
4.2.1 Displaying All Connections
By entering the netstat
command in the terminal, the system will display all network connections:
netstat
The output will be as follows:
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 192.168.1.100:22 192.168.1.1:12345 ESTABLISHED
tcp 0 0 192.168.1.100:80 192.168.1.2:54321 TIME_WAIT
4.2.2 Displaying Listening Ports
We can use the netstat
command to display all listening ports:
netstat -l
The output will be as follows:
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 0.0.0.0:80 0.0.0.0:* LISTEN
4.2.3 Displaying the Routing Table
We can use the netstat
command to display the system’s routing table:
netstat -r
The output will be as follows:
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
default 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
4.3 Summary
The netstat
command helps us monitor network connections, routing tables, interface statistics, etc., making it a powerful tool for diagnosing network problems. By displaying all connections, listening ports, and the routing table, we can gain a comprehensive understanding of the system’s network status.
5. Linux Performance Monitoring Commands –sar
5.1 Overview
The sar
command is a system activity report tool used to collect, report, and save system activity information. It can monitor the usage of CPU, memory, disk IO, network, and other resources, generating detailed reports.
5.2 Usage Examples
5.2.1 Basic Usage
By entering the sar
command in the terminal, the system will display the current system activity information:
sar
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
14:32:01 CPU %user %nice %system %iowait %steal %idle
14:32:01 all 1.23 0.00 0.45 0.12 0.00 98.20
5.2.2 Displaying CPU Usage
We can use the sar
command to display CPU usage:
sar -u
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
14:32:01 CPU %user %nice %system %iowait %steal %idle
14:32:01 all 1.23 0.00 0.45 0.12 0.00 98.20
5.2.3 Displaying Memory Usage
We can use the sar
command to display memory usage:
sar -r
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
14:32:01 kbmemfree kbavail kbmemused %memused kbbuffers kbcached kbcommit %commit kbactive kbinact kbdirty
14:32:01 1023456 1234567 2345678 50.00 123456 234567 345678 10.00 456789 567890 12345
5.2.4 Displaying Disk IO Usage
We can use the sar
command to display disk IO usage:
sar -b
The output will be as follows:
Linux 5.4.0-42-generic (hostname) 09/01/2021 _x86_64_ (4 CPU)
14:32:01 tps rtps wtps bread/s bwrtn/s
14:32:01 1.23 0.12 1.11 12.34 56.78
5.3 Summary
The sar
command is a very powerful system activity report tool that helps us comprehensively monitor system resources such as CPU, memory, and disk IO. By generating detailed reports, we can better analyze performance issues in the system.
Conclusion
In Linux systems, system monitoring and performance analysis are important means to ensure system stability and efficiency. This article introduced 5 powerful linux performance monitoring commands: top
, vmstat
, iostat
, netstat
, and sar
, and demonstrated their functionality through detailed usage examples. With these commands, we can gain a comprehensive understanding of the system’s operational state and promptly identify and resolve potential performance issues. We hope this article helps readers better master the skills of Linux system monitoring and performance analysis.