In Linux operations, mastering key commands can significantly enhance troubleshooting and system management efficiency. Whether it’s server lag, network anomalies, or log analysis and performance optimization, the right commands can help you quickly identify and resolve issues.
This article compiles 20 of the most practical Linux commands, covering system monitoring, network diagnostics, log analysis, and file management scenarios, teaching you how to use them to handle unexpected failures.
1. System Monitoring and Performance Analysis
1. <span><span>top</span></span> / <span><span>htop</span></span> —— Real-time Process Monitoring
Function: View CPU, memory, and process usage.
Emergency Scenario: Server lag, quickly locate high-load processes.
Example:
top -c # Display full command
htop # More user-friendly interactive interface (requires installation)
Key Output Columns:
-
<span><span>%CPU</span></span>: Process CPU usage -
<span><span>RES</span></span>: Memory usage -
<span><span>COMMAND</span></span>: Process name
2. <span><span>vmstat</span></span> —— System Resource Statistics
Function: View overall CPU, memory, I/O, and context switching status.
Emergency Scenario: Investigate system bottlenecks (e.g., CPU waiting for I/O).
Example:
vmstat 1 5 # Once every second, for a total of 5 times
Key Indicators:
-
<span><span>r</span></span>: Length of the run queue (greater than the number of CPU cores indicates overload) -
<span><span>wa</span></span>: Percentage of time waiting for I/O (high indicates disk bottleneck)
<span><span>3. iostat</span></span> —— Disk I/O Monitoring
Function: Analyze disk read/write performance.
Emergency Scenario: Database slow, suspect disk bottleneck.
Example:
iostat -x 1 # Display extended statistics, refresh every second
Key Indicators:
-
<span><span>%util</span></span>: Disk utilization (greater than 80% indicates busy) -
<span><span>await</span></span>: Average wait time for I/O (in milliseconds)
4. <span><span>free -h</span></span> —— Memory Usage
-
Function: View memory and swap usage.
-
Emergency Scenario: Service crash, suspect memory exhaustion.
-
Example:
free -h
Key Indicators:
-
<span><span>available</span></span>: Available memory (including cache and buffers)
5. <span><span>df -h</span></span> / <span><span>du -sh</span></span> —— Disk Space Analysis
Function: <span><span>df</span></span> to check remaining disk space, <span><span>du</span></span> to calculate directory size.
Emergency Scenario: Logs filling up disk causing service anomalies.
Example:
df -h / # Check root partition usage
du -sh /var/log # Calculate log directory size
2. Network Diagnostics
1. <span><span>ping</span></span> / <span><span>traceroute</span></span> —— Basic Connectivity Test
Function: Check network latency and routing path.
Emergency Scenario: Users report website is inaccessible.
Example:
ping example.com
traceroute example.com
2. <span><span>netstat</span></span> / <span><span>ss</span></span> —— Network Connections and Port Listening
Function: View current network connections and listening ports.
Emergency Scenario: Service port not started or connection count maxed out.
Example:
netstat -tulnp # Traditional method
ss -tulnp # Faster alternative
3. <span><span>tcpdump</span></span> —— Packet Capture Analysis
-
Function: Capture network packets to troubleshoot protocol issues.
-
Emergency Scenario: API interface anomalies, suspect network packet loss.
-
Example:
tcpdump -i eth0 port 80 -w capture.pcap
4. iftop / <span><span>nethogs</span></span> —— Real-time Traffic Monitoring
-
Function: View network bandwidth usage by process or IP.
-
Emergency Scenario: Server traffic surge, locate abnormal connections.
-
Example:
iftop -i eth0 # Traffic statistics by IP
nethogs eth0 # Traffic statistics by process (requires installation)
3. Log and Text Processing
1. <span><span>grep</span></span> —— Text Search
-
Function: Quickly filter key logs.
-
Emergency Scenario: Find error messages from massive logs.
-
Example:
grep "ERROR" /var/log/syslog
grep -A 3 -B 2 "panic" app.log # Show matching lines with surrounding content
2. <span><span>tail -f</span></span> —— Real-time Log Tracking
-
Function: Dynamically view log updates.
-
Emergency Scenario: Debugging service startup issues.
-
Example:
tail -f /var/log/nginx/access.log
<span><span>3. awk</span></span> / <span><span>sed</span></span> —— Advanced Text Processing
-
Function: Extract, replace, and count text content.
-
Emergency Scenario: Analyze logs to generate reports.
-
Example:
awk '{print $1}' access.log | sort | uniq -c # Count IP access times
sed -i 's/old/new/g' file.conf # Batch replace text
4. File and Permission Management
1. <span><span>find</span></span> —— File Search
-
Function: Find files by name, time, or size.
-
Emergency Scenario: Clean up expired logs or temporary files.
-
Example:
find /var/log -name "*.log" -mtime +30 -delete
<span><span>2. chmod</span></span> / <span><span>chown</span></span> —— Permission Management
-
Function: Modify file permissions and ownership.
-
Emergency Scenario: Service cannot start due to permission issues.
-
Example:
chmod 755 script.sh
chown -R nginx:nginx /var/www
5. System Management
1. <span><span>systemctl</span></span> —— Service Management
-
Function: Start, stop, and restart services.
-
Emergency Scenario: Service crash requires quick recovery.
-
Example:
systemctl restart nginx
systemctl status docker
2. <span><span>journalctl</span></span> —— View System Logs
-
Function: Query logs managed by systemd services.
-
Emergency Scenario: Troubleshoot service startup failures.
-
Example:
journalctl -u nginx --since "1 hour ago"
Summary
These 20 commands cover the most common failure scenarios in Linux operations, and it is recommended to bookmark and become proficient in using them. In actual work, scripts can be combined for automation to further enhance efficiency.
What other lifesaving commands do you have in your toolbox? Feel free to add! 🛠️
End of the article, if you found it helpful, please give a “like” or “view”, and feel free to share the article in your circle and technical groups.
—– Learning ResourcesRecommended —–
Online Tutorial Column


—————— END ——————
Follow our public account for more exciting content
