Before diving into specific tools, it’s important to understand some fundamental concepts related to Linux system monitoring.
top
is a classic Linux command - line tool that provides a real - time view of the system’s running processes. It displays information such as CPU usage, memory usage, process IDs (PIDs), and user names.
To start top
, simply open a terminal and type:
top
When top
is running, you can interact with it using various commands:
q
: Quit top
.P
: Sort processes by CPU usage.M
: Sort processes by memory usage.Here is an example of the output of top
:
top - 14:30:22 up 2 days, 1:23, 2 users, load average: 0.01, 0.02, 0.05
Tasks: 123 total, 1 running, 122 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.2 sy, 0.0 ni, 99.5 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 8077400 total, 2345600 free, 3456700 used, 2275100 buff/cache
KiB Swap: 2097152 total, 2097152 free, 0 used. 5323400 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1234 user1 20 0 1024m 512m 256m S 5.0 6.3 1:23.45 program1
2345 user2 20 0 512m 256m 128m S 3.0 3.1 0:45.67 program2
Regularly check top
to identify processes that are consuming a large amount of CPU or memory. If a process is using an excessive amount of resources, you can investigate further to determine if it is a legitimate process or a potential security threat.
vmstat
provides information about virtual memory statistics, including CPU, memory, swap, and I/O activity. It gives a snapshot of the system’s resource usage over a specified interval.
To use vmstat
, open a terminal and type:
vmstat
This will display a single snapshot of the system’s statistics. To get continuous updates at a specified interval (e.g., every 2 seconds), you can use:
vmstat 2
Here is an example of the output of vmstat
:
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 8077400 204800 3456000 0 0 0 0 1 0 0 0 100 0 0
The columns represent different aspects of the system:
r
: The number of processes waiting for CPU time.b
: The number of processes in an uninterruptible sleep state.swpd
: The amount of virtual memory used.free
: The amount of free physical memory.Use vmstat
to monitor the system’s memory and swap usage. If the swpd
value is constantly increasing, it may indicate that the system is running out of physical memory and is relying heavily on swap space.
iostat
is used to monitor the input/output (I/O) statistics of the system’s storage devices. It provides information about device utilization, transfer rates, and I/O requests.
To use iostat
, open a terminal and type:
iostat
To get detailed information about all devices and continuous updates every 3 seconds, you can use:
iostat -x 3
Here is an example of the output of iostat -x
:
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq - sz avgqu - sz await r_await w_await svctm %util
sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
The columns represent different I/O - related metrics:
r/s
and w/s
: The number of read and write requests per second.rkB/s
and wkB/s
: The number of kilobytes read and written per second.%util
: The percentage of time the device is busy handling I/O requests.Monitor the %util
value of storage devices. If a device has a high %util
value for an extended period, it may indicate a disk I/O bottleneck.
netstat
is used to display network connections, routing tables, interface statistics, and more. It helps in monitoring network activity and troubleshooting network - related issues.
To list all active TCP connections, open a terminal and type:
netstat -t
To show both TCP and UDP connections and their associated processes, use:
netstat -tuap
Here is an example of the output of netstat -tuap
:
Active Internet connections (servers and established)
Proto Recv - Q Send - Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1234/sshd
udp 0 0 0.0.0.0:68 0.0.0.0:* 2345/dhclient
The output shows information about network connections, including the protocol (tcp
or udp
), local and foreign addresses, connection state, and the associated process ID and program name.
Regularly check netstat
to identify unusual network connections. If you see connections to unknown IP addresses or ports, it may indicate a security breach.
top
every 10 minutes and send an email alert if a process is using more than a certain percentage of CPU or memory.In this blog post, we have explored some of the essential command - line tools for Linux system monitoring, including top
, vmstat
, iostat
, and netstat
. These tools provide valuable insights into the system’s CPU, memory, disk I/O, and network usage. By understanding their fundamental concepts, usage methods, and following common and best practices, you can effectively monitor your Linux systems, detect issues early, and ensure their smooth operation.
top
, vmstat
, iostat
, and netstat
. You can access them by typing man top
, man vmstat
, man iostat
, and man netstat
in a Linux terminal.