Linux System Resource Monitoring with htop
Linux System Resource Monitoring with htop
🎯 Summary
htop is a powerful tool for real-time monitoring of Linux system CPU, memory, and process status. It provides more intuitive and detailed information than the basic top
command.
Ready-to-Use Commands
# Run htop (most basic usage)
htop
# View processes for specific user only
htop -u username
# Run with specific PIDs highlighted
htop -p 1234,5678
# View process hierarchy in tree view
htop -t
Quick Installation
# Ubuntu/Debian
sudo apt update && sudo apt install htop
# CentOS/RHEL/Rocky Linux
sudo yum install epel-release -y && sudo yum install htop -y
# Fedora
sudo dnf install htop
# Arch Linux
sudo pacman -S htop
Essential Shortcuts (while htop is running)
F9
ork
: Kill processF6
or>
: Change sort criteriaF4
or\
: Filter processesSpace
: Tag/untag processt
: Toggle tree view
📚 Detailed Explanation
Background and Necessity
When managing Linux systems, there are frequent situations where you need to check system resource usage in real-time. While the traditional top
command is useful, htop provides the following advantages:
- Colorful interface: Easy visual distinction of information
- Mouse support: Click to select/manipulate processes
- Horizontal scrolling: View long commands completely
- Tree view: Easy understanding of parent-child relationships between processes
Detailed Installation Guide
Ubuntu/Debian Family
# Update package list
sudo apt update
# Install htop
sudo apt install htop
# Verify installation
htop --version
CentOS/RHEL/Rocky Linux Family
# Enable EPEL repository (htop is included in EPEL)
sudo yum install epel-release -y
# Update system
sudo yum update -y
# Install htop
sudo yum install htop -y
Fedora
# Install htop
sudo dnf install htop
Understanding htop Interface
When you run htop, you can check the following information:
Top System Information Panel
CPU Usage: [|||||||||||||||| 45.2%]
Memory: [||||||||||||||||||||| 67.8%/7.7G]
Swap: [ 0K/2.0G]
- CPU bar: Usage rate for each CPU core (distinguished by color)
- Memory bar: Physical memory usage
- Swap bar: Swap memory usage
Process List Column Meanings
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
1234 apache 20 0 180M 45M 12M S 5.2 0.6 1:23.45 httpd
- PID: Process ID
- USER: Process owner
- PRI/NI: Priority/Nice value
- VIRT: Virtual memory usage
- RES: Actual memory usage
- SHR: Shared memory
- S: Process state (S: Sleeping, R: Running, etc.)
Real-World Use Cases
1. Memory Leak Detection
# Run sorted by memory usage
htop
# Press F6 key within htop to sort by PERCENT_MEM
# Displays processes with high memory usage first
2. Finding CPU-Intensive Processes
# htop runs sorted by CPU usage by default
# Verify PERCENT_CPU sort with F6
htop
3. Monitor Specific User Processes Only
# Check only web server user (apache/nginx) processes
htop -u apache
# Or press F4 key after running htop for filtering
4. System Load Root Cause Analysis
# Check process hierarchy structure in tree view
htop -t
# Understand parent-child process relationships
# Identify which services create many child processes
Useful Advanced Usage
Configuration File Customization
htop settings are saved in the ~/.config/htop/htoprc
file:
# Check configuration file location
ls -la ~/.config/htop/
# Backup configuration
cp ~/.config/htop/htoprc ~/.config/htop/htoprc.backup
Log Collection in Batch Mode
# Save system status to file every 5 seconds
htop -d 50 > system_monitor.log 2>&1 &
# Or combine with watch command
watch -n 5 'htop -b -n 1 | head -20'
Troubleshooting and Error Handling
When htop Installation Fails
# When package cannot be found on Ubuntu
sudo apt update
sudo apt upgrade
sudo apt install htop
# When EPEL repository issues occur on CentOS
sudo yum clean all
sudo yum install epel-release -y
sudo yum makecache
sudo yum install htop
Permission-Related Issues
# Root privileges required to see all processes
sudo htop
# Regular users can only manipulate their own processes
Conclusion
htop is an essential tool for Linux system administrators and developers. It provides much more intuitive and powerful features than the basic top command, enabling efficient system resource monitoring and performance analysis.
Key Insights:
- htop provides an intuitive interface similar to Windows Task Manager
- Quick identification of performance bottlenecks through real-time monitoring
- Efficient process management using mouse and keyboard shortcuts
Next Steps:
- Learn disk I/O monitoring with
iotop
- Analyze network usage with
nethogs
- Write and automate system monitoring scripts
Master htop to completely understand and optimize your Linux system performance!