Linux nethogs Command
nethogs is an open-source Linux command-line tool used to monitor network bandwidth usage in real time by process. Unlike traditional network monitoring tools (such as iftop), nethogs does not measure traffic based on ports or protocols; instead, it directly displays the network activity of each process.
Why Use nethogs
On Linux systems, when network anomalies occur, administrators typically need to quickly identify which process is consuming a large amount of bandwidth. nethogs offers the following advantages:
- Process-level monitoring: directly displays the network traffic of each process
- Real-time updates: refreshes data every second by default
- No configuration required: works out of the box, no complex setup needed
- Lightweight: low resource usage, suitable for production environments
Install nethogs
Ubuntu/Debian Systems
Example
sudo apt-get install nethogs
CentOS/RHEL Systems
Example
sudo yum install nethogs
Compile and Install from Source
Example
cd nethogs
make && sudo make install
Basic Usage
Monitor All Network Interfaces
sudo nethogs
Example output:
PID USER PROGRAM DEV SENT RECEIVED 1234 root /usr/bin/ssh eth0 12.456 5.678 KB/sec 5678 bob /usr/bin/firefox wlan0 45.789 32.123 KB/sec
Monitor a Specific Network Interface
sudo nethogs eth0
Refresh Interval Setting (Seconds)
sudo nethogs -d 5 # 每5秒刷新一次
Common Command Options
| Option | Description |
|---|---|
-d |
Set the refresh interval time (seconds) |
-c |
Exit after refreshing a specified number of times |
-t |
Trace mode (displays cumulative traffic) |
-p |
Promiscuous mode (listens to all traffic) |
-V |
Display version information |
-h |
Display help information |
Advanced Usage
1. Track a Specific Process
sudo nethogs -p | grep "进程名"
2. Save Monitoring Results to a File
sudo nethogs -t > network_log.txt
3. Monitor a Specific User's Processes
sudo nethogs -u username
4. Combine Options
sudo nethogs -d 10 -c 6 eth0 # 每10秒刷新一次,共刷新6次后退出
Output Field Explanation
nethogs output includes the following key information:
- PID: Process ID
- USER: The user running the process
- PROGRAM: Process name or command line
- DEV: Network interface used
- SENT: Data sending rate of the process (KB/sec)
- RECEIVED: Data receiving rate of the process (KB/sec)
Practical Application Scenarios
Scenario 1: Diagnosing Slow Network Issues
sudo nethogs
Quickly identify the source of the problem by checking which process occupies the most bandwidth.
Scenario 2: Monitoring Abnormal Server Traffic
sudo nethogs -d 30 -c 12 > /var/log/nethogs.log
Record once every 30 seconds, capturing a total of 6 minutes of network activity.
Scenario 3: Limiting Bandwidth for a Specific Process
Example
sudo nethogs
# Then use tc or wondershaper to limit bandwidth
sudo wondershaper eth0 1024 512 # Limit upload to 1024Kbps, download to 512Kbps
Common Problem Solutions
Problem 1: Insufficient Permissions
Error: opening device eth0: Operation not permitted
Solution: Run nethogs with sudo or as the root user.
Problem 2: Unable to Identify Process Name
PID USER PROGRAM DEV SENT RECEIVED 1234 - - eth0 12.456 5.678 KB/sec
Solution: The process may have ended, or nethogs does not have sufficient permissions to obtain process information.
Problem 3: Specific Network Interface Not Supported
Error: No such device exists (device_name)
Solution: Use theifconfigorip acommand to confirm the correct interface name.
Alternative Tool Comparison
| Tool | Features | Applicable Scenarios |
|---|---|---|
| nethogs | Displays traffic by process | Quickly locate problematic processes |
| iftop | Displays traffic by connection | Analyze specific network connections |
| nload | Displays total traffic by interface | Monitor overall bandwidth usage |
| bmon | Graphical interface | Visually view traffic trends |
Summary
nethogs is an indispensable network diagnostic tool in a Linux system administrator's toolbox, especially suitable for:
- Quickly identifying processes with abnormal network activity
- Monitoring the bandwidth usage of each service on a server
- Troubleshooting network performance issues
With the basic usage and advanced techniques introduced in this article, you should be able to use nethogs proficiently to monitor and manage system network traffic.
Other Extensions
Linux Command Encyclopedia