How to use the command nethogs (with examples)
- Linux
- December 25, 2023
NetHogs is a command-line tool that allows you to monitor the bandwidth usage per process on your Linux system. It provides real-time information about the processes and programs that are using your network resources. This can be useful for troubleshooting network performance issues, identifying bandwidth-hungry applications, and optimizing network usage.
Use case 1: Start NetHogs as root (default device is eth0
)
Code:
sudo nethogs
Motivation: Starting NetHogs as root allows you to monitor the network usage of all processes running on your system. This can help you identify any processes that are using excessive bandwidth and taking up network resources.
Explanation:
sudo
: Runs the command as superuser (root) to ensure access to network data.nethogs
: The command itself.- No device argument: By default, NetHogs monitors the
eth0
network device.
Example output:
Refreshing: 2
NetHogs version 0.8.5-1
PID USER PROGRAM DEV SENT RECEIVED
1001 john chrome eth0 1.23MB 4.56MB
2002 jane firefox eth0 0.83MB 2.37MB
3003 alice spotify eth0 0.12MB 0.03MB
In this example, NetHogs is monitoring the eth0
network device. It displays the PID (Process ID), USER (username), PROGRAM (name of the program/process), DEV (network device), and the amount of data SENT and RECEIVED by each process. The output is refreshed every 2 seconds.
Use case 2: Monitor bandwidth on specific device
Code:
sudo nethogs device
Motivation: Sometimes, you may have multiple network devices on your system, and you want to monitor the bandwidth usage on a specific device. By specifying the device, you can focus on the network activity happening through that device.
Explanation:
sudo
: Runs the command as superuser (root) to ensure access to network data.nethogs
: The command itself.device
: Specifies the network device you want to monitor (e.g., eth0, wlan0).
Example output:
Refreshing: 2
NetHogs version 0.8.5-1
PID USER PROGRAM DEV SENT RECEIVED
1001 john chrome eth0 1.23MB 4.56MB
2002 jane firefox eth0 0.83MB 2.37MB
In this example, NetHogs is monitoring the network activity specifically on the eth0
device. It displays the same information as before but only for the processes using that particular device.
Use case 3: Monitor bandwidth on multiple devices
Code:
sudo nethogs device1 device2
Motivation: In complex network setups, you may want to monitor the bandwidth usage on multiple devices simultaneously. By specifying multiple devices, you can get an overview of the network activity happening across different interfaces.
Explanation:
sudo
: Runs the command as superuser (root) to ensure access to network data.nethogs
: The command itself.device1 device2
: Specifies the multiple network devices you want to monitor (e.g., eth0, wlan0).
Example output:
Refreshing: 2
NetHogs version 0.8.5-1
PID USER PROGRAM DEV SENT RECEIVED
1001 john chrome eth0 1.23MB 4.56MB
2002 jane firefox eth0 0.83MB 2.37MB
3003 alice spotify wlan0 0.12MB 0.03MB
4004 bob ssh wlan0 0.01MB 0.01MB
In this example, NetHogs is monitoring the network activity on both the eth0
and wlan0
devices. It displays the same information as before but for processes using different devices.
Use case 4: Specify refresh rate
Code:
sudo nethogs -t seconds
Motivation: Changing the refresh rate allows you to control how frequently NetHogs updates the bandwidth usage information. This can be helpful if you want more or less frequent updates, depending on your specific monitoring needs.
Explanation:
sudo
: Runs the command as superuser (root) to ensure access to network data.nethogs
: The command itself.-t seconds
: Specifies the refresh rate in seconds (e.g., 5, 10).
Example output:
Refreshing: 5
NetHogs version 0.8.5-1
PID USER PROGRAM DEV SENT RECEIVED
1001 john chrome eth0 1.23MB 4.56MB
2002 jane firefox eth0 0.83MB 2.37MB
In this example, NetHogs is monitoring the network activity with a refresh rate of 5 seconds. The output is updated every 5 seconds, providing a real-time view of the bandwidth usage per process.
Conclusion:
NetHogs is a powerful command-line tool for monitoring bandwidth usage per process. By using the various options and arguments, you can customize the monitoring experience to suit your needs. Whether you want to identify bandwidth-hogging applications, optimize network usage, or troubleshoot network performance issues, NetHogs provides the information you need to analyze and manage network activity effectively.