How to Use the Command 'nethogs' (with Examples)
- Linux
- December 17, 2024
NetHogs is a command-line network utility that provides real-time monitoring of bandwidth usage on a per-process basis. Unlike other network monitoring tools that break down traffic per network interface or protocol, NetHogs identifies which process is responsible for generating network traffic, making it especially useful for pinpointing the source of unexpected network usage or monitoring the bandwidth consumed by individual applications. More information can be found on its GitHub page .
Use Case 1: Start NetHogs as Root (Default Device eth0
)
Code:
sudo nethogs
Motivation:
Starting NetHogs with root privileges on the default device eth0
allows you to monitor network traffic from a global perspective without specifying any particular network interface. This is especially useful for quickly diagnosing bandwidth-heavy processes on systems where eth0
is the main network interface.
Explanation:
sudo
: Runs the NetHogs command with administrative privileges, which is necessary because monitoring network packets requires elevated permissions.nethogs
: Invokes the NetHogs tool to start monitoring network activity.
Example Output:
PID USER PROGRAM DEV SENT RECEIVED
1234 user process1 eth0 120KB 400KB
5678 user process2 eth0 80KB 250KB
The output shows individual processes (process1
, process2
) and their respective bandwidth usage on eth0
.
Use Case 2: Monitor Bandwidth on Specific Device
Code:
sudo nethogs wlan0
Motivation:
Specifying a particular network device like wlan0
is necessary when monitoring a wireless interface instead of the default Ethernet interface (eth0
). This is useful in laptops or devices using Wi-Fi, where the traffic may be routed through a different interface.
Explanation:
sudo
: Ensures the command is executed with the required administrative privileges.nethogs wlan0
: Starts NetHogs to monitor network traffic on the specified devicewlan0
.
Example Output:
PID USER PROGRAM DEV SENT RECEIVED
4321 user process3 wlan0 60KB 300KB
8765 user process4 wlan0 20KB 150KB
The output displays processes and their network usage on the wireless device wlan0
.
Use Case 3: Monitor Bandwidth on Multiple Devices
Code:
sudo nethogs eth0 wlan0
Motivation:
Monitoring multiple devices simultaneously can be essential in scenarios where network traffic is distributed across several interfaces, such as desktops with both Ethernet and Wi-Fi connections active, or servers handling multiple network cards. This gives a comprehensive view of the entire network activity across all specified devices.
Explanation:
sudo
: Provides the necessary permissions to monitor network traffic.nethogs eth0 wlan0
: Runs NetHogs on botheth0
andwlan0
interfaces simultaneously.
Example Output:
PID USER PROGRAM DEV SENT RECEIVED
1234 user process1 eth0 130KB 410KB
5678 user process2 wlan0 90KB 260KB
Processes are shown alongside their respective network devices (eth0
and wlan0
), detailing their bandwidth usage.
Use Case 4: Specify Refresh Rate
Code:
sudo nethogs -t 5
Motivation:
Adjusting the refresh rate is useful when you need to monitor bandwidth over different time intervals. A slower refresh rate, such as every 5 seconds, can provide a less crowded view and is helpful for long-term observations or less intensive bandwidth analysis.
Explanation:
sudo
: Grants the command the necessary access levels.nethogs -t 5
: The-t
flag sets the refresh interval to 5 seconds.
Example Output:
PID USER PROGRAM DEV SENT RECEIVED
4567 user process5 eth0 100KB 500KB
8901 user process6 eth0 200KB 400KB
The output updates every 5 seconds, showing processes and their bandwidth consumption over the eth0
device.
Conclusion:
NetHogs is a versatile and efficient tool for monitoring network traffic on a per-process basis. By using the appropriate command syntax and flags, users can tailor its operation to meet various monitoring needs, whether it’s observing a specific device, multiple interfaces, or modifying the refresh rate to balance performance with data detail. Understanding how to leverage NetHogs can be invaluable for network diagnostics and ensuring efficient bandwidth usage on your systems.