Linux nethogs command
nethogs is an open-source Linux command-line tool for monitoring network bandwidth usage by process in real time. Unlike traditional network monitoring tools (such as iftop), nethogs does not count traffic based on ports or protocols but directly displays the network activity of each process.
Why use nethogs
On Linux systems, when network problems occur, administrators usually need to quickly identify which process is consuming a large amount of bandwidth. nethogs offers the following advantages:
- Process-level monitoring: Directly display the network traffic of each process
- Real-time updates: Refresh data once per second by default
- No configuration needed: Works out of the box, no complex setup required
- Lightweight: Low resource usage, suitable for production environments
Install nethogs
Ubuntu/Debian systems
Example
sudo
apt-get update
sudo
apt-get install
nethogs
CentOS/RHEL systems
Example
sudo
yum install
epel-release
sudo
yum install
nethogs
Install by compiling from source
Example
git clone
https:
//
github.com
/
raboof
/
nethogs
cd
nethogs
make
&
amp;
&
amp;
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
Set the refresh interval (seconds)
sudo nethogs -d 5 # 每5秒刷新一次
Common command options
| Option | Description |
|---|---|
-d |
Set the refresh interval in seconds |
-c |
Exit after a specified number of refreshes |
-t |
Trace mode (display cumulative traffic) |
-p |
Promiscuous mode (listen to all traffic) |
-V |
Display version information |
-h |
Display help information |
Advanced usage
1. Trace a specific process
sudo nethogs -p | grep "进程名"
2. Save monitoring results to a file
sudo nethogs -t > network_log.txt
3. Monitor processes of a specific user
sudo nethogs -u username
4. Combine options
sudo nethogs -d 10 -c 6 eth0 # 每10秒刷新一次,共刷新6次后退出
Explanation of output fields
The output of nethogs contains the following key information:
- PID: Process ID
- USER: The user running the process
- PROGRAM: Process name or command line
- DEV: The network interface in use
- SENT: The data transmission rate of the process (KB/sec)
- RECEIVED: The data reception rate of the process (KB/sec)
Practical Application Scenarios
Scenario 1: Diagnose slow network issues
sudo nethogs
Quickly locate the source of the problem by seeing which process uses the most bandwidth.
Scenario 2: Monitor abnormal server traffic
sudo nethogs -d 30 -c 12 > /var/log/nethogs.log
Record once every 30 seconds for a total of 6 minutes of network activity.
Scenario 3: Limit the bandwidth of a specific process
Example
First use nethogs to find the high-traffic process
sudo
nethogs
Then use tc or wondershaper to limit the bandwidth
sudo
wondershaper eth0
1024
512
Limit upload to 1024Kbps and download to 512Kbps
Common Problem Solutions
Problem 1: Insufficient permissions
Error: opening device eth0: Operation not permitted
Solution: Use sudo or run nethogs as the root user.
Problem 2: Cannot identify the process name
PID USER PROGRAM DEV SENT RECEIVED
1234 - - eth0 12.456 5.678 KB/sec
Solution: The process may have ended, or nethogs may not have sufficient permissions to obtain process information.
Problem 3: A specific network interface is not supported
Error: No such device exists (device_name)
Solution: When usingifconfigorip aUse the command to confirm the correct interface name.
Comparison of alternative tools
| Tool | Feature | Applicable scenarios |
|---|---|---|
| nethogs | Display traffic by process | Quickly locate the problematic process |
| iftop | Display traffic by connection | Analyze specific network connections |
| nload | Display 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 various services on the server
- Troubleshooting network performance issues
Through the basic usage and advanced techniques introduced in this article, you should be able to use nethogs skillfully to monitor and manage system network traffic.
Linux Command Reference