- 1. Command Overview
- 🔍 1. top command: Real-time system monitoring
- 🌟 2. htop command: Enhanced system monitoring
- 💾 3. free command: Memory usage
- 💽 4. df command: Disk space check
- 📁 5. du command: Directory size analysis
- ⚡ 6. iostat command: I/O performance monitoring
- 📊 7. vmstat command: Virtual memory statistics
- 🌐 8. netstat command: Network connection monitoring
- 🛠️ 9. Comprehensive monitoring script example
- 2. 💡 Practical Troubleshooting Process
- CPU bottleneck troubleshooting
- Memory bottleneck troubleshooting
- Disk I/O bottleneck troubleshooting
- 3. 📈 Summary and Best Practices
Hello everyone, I am Kewei Zhou, and today I will share some resource analysis commands that I often use during middleware deployment and operation. Mastering these commands will help you easily navigate Linux performance analysis and identify online resource bottlenecks.
For more technical content, please follow the WeChat public account “Kewei Zhou’s AI Notes”~
❝
As a Linux system administrator or operations engineer, we often need to monitor server performance and troubleshoot issues. Today, I have compiled a detailed guide on Linux server resource analysis commands to help you quickly identify system bottlenecks.
1. Command Overview
| Command | Main Function | Key Parameters | Applicable Scenarios |
|---|---|---|---|
| top | Real-time system monitoring | -P (CPU sorting), -M (memory sorting) | Overall performance monitoring, process tracking |
| htop | Enhanced system monitoring | -t (tree display), -u (user filter) | More intuitive process monitoring and interactive operations |
| free | Memory usage analysis | -h (human-readable), -s (continuous monitoring) | Memory bottleneck diagnosis, cache analysis |
| df | Disk space usage | -h (human-readable), -T (filesystem type) | Disk capacity monitoring, partition usage rate |
| du | Directory space analysis | -sh (summary), –max-depth (directory depth) | Disk space cleanup, large file search |
| iostat | I/O performance monitoring | -x (extended statistics), -c (CPU) | Disk I/O bottleneck analysis |
| vmstat | Virtual memory statistics | Interval time, count | Comprehensive system bottleneck diagnosis |
| netstat | Network connection monitoring | -a (all), -t (TCP), -p (program name) | Network bandwidth bottleneck troubleshooting |
| ps | Process status | aux (full display) | Process snapshot viewing |
| lsof | Open file list | -i (network connection), -p (specific process) | File occupation issue troubleshooting |
🔍 1. top command: Real-time system monitoring
Basic Description
<span>top</span> command is the most commonly used performance analysis tool in Linux, capable of displaying real-time resource usage of various processes in the system, similar to the Windows Task Manager.
Output Explanation
top - 14:23:30 up 2 days, 3:17, 2 users, load average: 0.15, 0.20, 0.25
Tasks: 200 total, 1 running, 198 sleeping, 0 stopped, 1 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
MiB Mem : 15998.8 total, 1200.2 free, 7707.4 used, 7091.2 buff/cache
MiB Swap: 8192.0 total, 8192.0 free, 0.0 used. 7707.4 avail Mem
Key Field Analysis:
- Load Situation: load average shows the average load of the system over 1, 5, and 15 minutes
- Process Status: A high number of zombie processes may indicate an abnormal program
- CPU Usage: A high wa value indicates severe I/O wait, which may indicate a disk bottleneck
- Memory Usage: buff/cache is kernel cache, which can be reclaimed when memory is insufficient
Practical Tips
# Sort by CPU usage
top -o %CPU
# Sort by memory usage
top -o %MEM
# Monitor processes of a specific user
top -u username
# Set refresh interval to 2 seconds
top -d 2
# Only display specific processes
top -p 1234,5678
Interactive Commands (used while top is running):
<span>P</span>: Sort by CPU usage<span>M</span>: Sort by memory usage<span>z</span>: Toggle color/black and white display<span>1</span>: Display detailed status of all CPU cores<span>k</span>: Terminate a specified process<span>q</span>: Exit top
🌟 2. htop command: Enhanced system monitoring
Advantages
<span>htop</span> is an enhanced version of <span>top</span>, providing a more intuitive interface and richer interactive features, supporting mouse operations and vertical/horizontal scrolling.
Installation and Usage
# Ubuntu/Debian
sudo apt install htop
# CentOS/RHEL
sudo yum install htop
# Use htop
htop
Practical Cases
# Tree display of process relationships
htop -t
# Only display processes of a specific user
htop -u username
# Filter specific processes
htop -p 1234,5678
💾 3. free command: Memory usage
Command Description
<span>free</span> command is used to display the usage of physical memory and swap space in the system.
Output Interpretation
free -h
total used free shared buff/cache available
Mem: 7.7G 2.1G 3.2G 200M 2.4G 5.1G
Swap: 2.0G 0B 2.0G
Key Indicators:
- buff/cache: Memory used by the kernel for caching, which can be reclaimed when memory is tight
- available: Estimates the amount of memory available for starting new programs, more accurate than free
- Swap Usage: If swap is heavily used, it indicates insufficient physical memory
Practical Cases
# Display in human-readable format
free -h
# Refresh every 3 seconds
free -h -s 3
# Display detailed statistics
free -h -w
# Display total information
free -h -t
💽 4. df command: Disk space check
Command Description
<span>df</span> command is used to display the disk space usage of the filesystem.
Practical Cases
# Display in human-readable format
df -h
# Display filesystem type
df -hT
# Display inode usage
df -i
# Only display local filesystems
df -h -l
# Exclude specific filesystem types
df -h -x tmpfs -x devtmpfs
Output Example:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 28G 42% /
/dev/sda2 100G 50G 45G 53% /home
📁 5. du command: Directory size analysis
Command Description
<span>du</span> command is used to estimate the disk usage of files and directories, very suitable for finding large files and directories.
Practical Cases
# Check total size of current directory
du -sh
# Check size of specified directory
du -sh /var/log
# Check size of each subdirectory in the directory and sort
du -sh /* | sort -hr
# Check size of subdirectories with a depth of 2
du -h --max-depth=2 /path/to/dir
# Exclude certain file types
du -sh --exclude="*.log" /path/to/dir
Practical Script: Find Large Files
#!/bin/bash
# Find files larger than 100M
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
# Analyze directory size and display top 20
du -h --max-depth=1 / 2>/dev/null | sort -hr | head -20
⚡ 6. iostat command: I/O performance monitoring
Command Description
<span>iostat</span> command is used to monitor the disk I/O and CPU usage of the system, an important tool for diagnosing disk bottlenecks.
Installation and Usage
# Install sysstat package (includes iostat)
# Ubuntu/Debian
sudo apt install sysstat
# CentOS/RHEL
sudo yum install sysstat
# Use iostat
iostat [options] [interval] [count]
Practical Cases
# Display statistics for all devices once
iostat
# Refresh every 2 seconds, display 5 times
iostat 2 5
# Display extended disk statistics
iostat -x 1 3
# Display in MB
iostat -m
# Display detailed statistics for specified disk
iostat -x sda 1 3
Key Indicator Interpretation:
<span>%util</span>: Device utilization, close to 100% indicates I/O saturation<span>await</span>: Average I/O wait time, larger values indicate busier disks<span>svctm</span>: Average service time, reflects disk performance
📊 7. vmstat command: Virtual memory statistics
Command Description
<span>vmstat</span> command reports information about processes, memory, paging, block IO, traps, and CPU activity, an important tool for comprehensive performance analysis.
Output Field Analysis
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 1234567 89012 345678 0 0 12 21 100 23 3 1 96 0 0
- r: Number of processes waiting to run, greater than the number of CPUs indicates CPU shortage
- b: Number of processes in uninterruptible sleep, indicating processes waiting for I/O
- si/so: Amount of memory swapped in/out, consistently greater than 0 indicates memory shortage
- wa: Percentage of I/O wait time, exceeding 20% may indicate I/O bottlenecks
Practical Cases
# Display system status once
vmstat
# Refresh every 2 seconds, display 5 times
vmstat 2 5
# Display active and inactive memory
vmstat -a
# Display disk read/write statistics
vmstat -d
# Display detailed memory information
vmstat -s
🌐 8. netstat command: Network connection monitoring
Command Description
<span>netstat</span> command is used to display network connections, routing tables, interface statistics, and other information.
Practical Cases
# Display all listening ports
netstat -tuln
# Display TCP connection status statistics
netstat -nat | awk '{print $6}' | sort | uniq -c
# Display network connections used by processes
netstat -tulnp
# Continuously monitor network status
netstat -c
# Display network interface statistics
netstat -i
🛠️ 9. Comprehensive Monitoring Script Example
System Resource Monitoring Script
#!/bin/bash
# system_monitor.sh - System resource monitoring script
echo "=== System Resource Monitoring Report ==="
echo "Generated Time: $(date)"
echo
echo "1. System Load and Uptime:"
uptime
echo
echo "2. Memory Usage:"
free -h
echo
echo "3. Disk Space Usage:"
df -h
echo
echo "4. Top CPU-consuming Processes:"
ps aux --sort=-%cpu | head -10
echo
echo "5. Top Memory-consuming Processes:"
ps aux --sort=-%mem | head -10
echo
echo "6. Large Directory Analysis:"
du -sh /* 2>/dev/null | sort -hr | head -10
Real-time Monitoring Panel Script
#!/bin/bash
# realtime_monitor.sh - Real-time monitoring script
while true; do
clear
echo "=== Real-time System Monitoring $(date) ==="
echo
# CPU and memory usage
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | awk '{print "User: " $2 "% System: " $4 "% Idle: " $8 "%"}'
echo
echo "Memory Usage:"
free -h | grep -E "Mem:|Swap:" | awk '{print $1 " Total Size: " $2 " Used: " $3 " Available: " $4}'
echo
echo "Disk Usage:"
df -h | grep -E "^/dev/"
echo
sleep 5
done
2. 💡 Practical Troubleshooting Process
CPU Bottleneck Troubleshooting
# 1. Check system load
uptime
# 2. Check CPU usage details
top
# 3. Sort processes by CPU
ps aux --sort=-%cpu | head -10
# 4. Use vmstat to check CPU statistics
vmstat 2 5
Memory Bottleneck Troubleshooting
# 1. Check memory usage overview
free -h
# 2. Check processes with the highest memory usage
top -o %MEM
# 3. Check for memory leaks
vmstat -s
# 4. Check swap usage
swapon -s
Disk I/O Bottleneck Troubleshooting
# 1. Check disk space usage
df -h
# 2. Monitor disk I/O performance
iostat -x 1 3
# 3. Check which processes are using the disk
iotop
# 4. Check for large files
find / -type f -size +100M 2>/dev/null
3. 📈 Summary and Best Practices
By reasonably combining these commands, you can build a complete Linux server resource monitoring system:
- Establish Performance Baselines: Understand the normal range of system metrics to quickly identify anomalies
- Regular Health Checks: Incorporate key monitoring commands into daily maintenance processes
- Automated Monitoring: Write scripts to implement automated monitoring and alerts
- Comprehensive Diagnosis: A single command may not fully locate the problem; combine multiple tools for analysis
Recommended Learning Path: Start with <span>top</span> and <span>free</span> to master basic monitoring; then learn <span>df</span> and <span>du</span> for disk analysis; finally master <span>vmstat</span> and <span>iostat</span> for advanced performance diagnosis.
I hope this detailed guide helps you better understand and master Linux server resource analysis commands! If you have any questions or additions, feel free to discuss in the comments.
Note:This article is for technical learning and communication purposes only. Please ensure you understand the impact of using related commands in actual production environments.
[Reprint Notice]:Please indicate the original source and author information when reprinting