Monitoring resource usage on a Linux server is crucial for maintaining performance and troubleshooting issues. Various tools are available to help you track CPU, memory, disk, and network usage. Below are some common methods and commands to monitor system resources.
1. Check CPU Usage
Using top
The top
command provides a dynamic, real-time view of system processes, including CPU usage.
$ top
You will see a list of running processes, and at the top of the screen, you’ll find CPU usage details under the “%CPU” column. The CPU usage is broken down into user processes, system processes, and idle time.
Example output:
top - 15:25:01 up 3 days, 4:14, 1 user, load average: 0.13, 0.12, 0.10Tasks: 204 total, 1 running, 203 sleeping, 0 stopped, 0 zombie %Cpu(s): 5.2 us, 2.1 sy, 0.0 ni, 92.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 8032.8 total, 3094.0 free, 1634.4 used, 4304.4 buff/cache MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 6112.8 avail Mem
2. Monitor Memory Usage
Using free
The free
command shows memory usage, including total, used, and free memory, as well as swap usage.
$ free -h
The -h
flag displays the information in a human-readable format.
Example output:
total used free shared buff/cache availableMem: 7.8Gi 1.6Gi 3.0Gi 155Mi 3.2Gi 5.7Gi Swap: 2.0Gi 0B 2.0Gi
3. Disk Usage
Using df
The df
command shows the amount of disk space used and available on all mounted filesystems.
$ df -h
The -h
flag ensures the output is human-readable (e.g., in GiB or MiB).
Example output:
Filesystem Size Used Avail Use% Mounted on/dev/sda1 50G 30G 18G 63% / tmpfs 16G 1.6G 15G 10% /dev/shm /dev/sdb1 100G 25G 70G 27% /data
4. Monitor Network Usage
Using iftop
The iftop
command shows network usage for the active connections in real-time.
$ sudo iftop
This will display a list of active network connections and show their incoming and outgoing bandwidth.
Example output:
TX: 15.3 KB 22.1 KB Total 60.1 KBRX: 10.1 KB 12.3 KB IP address Port Pkts In Pkts Out Total In Total Out 192.168.1.2 22 200 150 25.5 KB 13.4 KB 192.168.1.5 80 85 120 5.2 KB 9.1 KB
Using netstat
netstat
shows network connections, routing tables, and interface statistics.
$ netstat -tuln
This displays listening ports and the corresponding services using them.
Example output:
Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp6 0 0 :::80 :::* LISTEN
5. Check System Load
Using uptime
The uptime
command provides a quick overview of the system’s load averages over 1, 5, and 15 minutes.
$ uptime
Example output:
15:30:02 up 3 days, 4:21, 1 user, load average: 0.10, 0.12, 0.08
6. Monitor Process Usage
Using ps
The ps
command allows you to check the resource usage of specific processes.
$ ps aux --sort=-%cpu | head
This command sorts processes by CPU usage and shows the top processes.
Example output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMANDroot 1123 10.5 3.1 112340 31092 ? S 15:10 0:12 /usr/bin/python3 user 2345 7.8 1.8 82344 18640 ? S 15:15 0:09 /usr/bin/nginx
7. Advanced Monitoring Tools
Using htop
htop
is an interactive process viewer that provides a more user-friendly way to monitor system resources.
$ sudo htop
It allows you to sort processes by CPU, memory usage, and more, with a visually appealing display.