Linux htop Command
What is htop
htop is an interactive process viewer for Linux systems. It provides richer functionality and a more user-friendly interface than the traditional top command.
Main Features of htop
- Color display: Different colors distinguish different types of processes and system states
- Visual layout: Intuitively displays CPU, memory, and swap space usage
- Mouse support: Allows direct mouse click operations
- Process tree view: Allows viewing the hierarchical relationship of processes
- Custom sorting: Can sort processes by various metrics
- Process operations: Can directly kill processes, adjust priorities, etc.
Installing htop
On Debian/Ubuntu-based systems
sudo apt update sudo apt install htop
On RHEL/CentOS-based systems
sudo yum install epel-release # 先安装 EPEL 仓库 sudo yum install htop
On Arch Linux-based systems
sudo pacman -S htop
htop Basic Interface Explanation
To start htop, simply type in the terminal:
htop
The interface is mainly divided into the following parts:
Top area: System overview information
- CPU usage (displayed per core)
- Memory usage
- Swap space usage
- System uptime and average load
Middle area: Process list
- PID: Process ID
- USER: Process owner
- PRI: Process priority
- NI: Nice value
- VIRT: Virtual memory usage
- RES: Physical memory usage
- SHR: Shared memory size
- S: Process state (running, sleeping, etc.)
- CPU%: CPU usage
- MEM%: Memory usage rate
- TIME+: CPU time
- COMMAND: Command name
Bottom area: Function key hints
htop Common Shortcut Keys
| Shortcut key | Function description |
|---|---|
| F1 or h | Show help |
| F2 or S | Enter settings menu |
| F3 or / | Search processes |
| F4 or \ | Filter processes |
| F5 or t | Show process tree |
| F6 or < | Select sort field |
| F7 or [ | Decrease process priority |
| F8 or ] | Increase process priority |
| F9 or k | Kill process |
| F10 or q | Quit htop |
| Space key | Mark/unmark process |
| u | Show processes of a specific user |
| H | Show/hide user threads |
| K | Show/hide kernel threads |
| P | Sort by CPU usage |
| M | Sort by memory usage |
| T | Sort by run time |
htop Advanced Features
1. Process Tree View
pressF5ortYou can switch to the process tree view, which displays the parent-child relationship between processes.
2. Process Filtering
pressF4or\You can enter filter criteria to display only processes that match the criteria.
3. Multi-select Operations
After marking multiple processes with the space bar, you can perform batch operations (such as killing).
4. Custom Display
pressF2Enter the settings menu to:
- Adjust the displayed columns
- Change the color theme
- Set the refresh interval
- Configure other display options
5. System Monitoring
The top area of htop provides rich system monitoring information:
- CPU usage (per core)
- Memory and swap space usage
- System load averages
Comparison of htop and top Commands
| Feature | htop | top |
|---|---|---|
| Interface | Colorful, intuitive | Monochrome, basic |
| Mouse support | Yes | no |
| Process tree view | Supported | Not supported |
| Sorting method | Multiple, dynamically adjustable | Limited |
| Process operations | Richer | Basic |
| Customization | Highly configurable | Limited |
| Resource usage | Slightly higher | Lower |
Practical Application Examples
Example 1: Find and terminate processes with high CPU usage
- Start htop
- press
PSort by CPU usage - Find the target process
- press
F9Choose the termination signal (usually select 15 SIGTERM) - Press Enter to confirm
Example 2: Monitor a specific user's processes
- Start htop
- press
uKey - Select the username to monitor
- Show only that user's processes
Example 3: Set the refresh interval
- press
F2Enter settings - Select "Display options"
- Modify the "Delay" value (seconds)
- press
F10Save and exit
Frequently Asked Questions
Q1: Which is better, htop or top?
htop offers richer features and a friendlier interface, suitable for interactive use; top is more lightweight and suitable for use in scripts.
Q2: Why are some processes displayed in green/red?
Green usually indicates processes running normally; red may indicate high resource usage or zombie processes.
Q3: How do I view the full command line?
pressF2Enter settings and adjust the "Show program path" option under "Display options".
Q4: What do the memory information displayed by htop mean?
- VIRT: Total virtual memory used by the process
- RES: Physical memory used by the process
- SHR: Shared memory used by the process
Linux Command Encyclopedia