How to Monitor Network Usage for Processes on Linux

https://static1.makeuseofimages.com/wordpress/wp-content/uploads/2023/08/network-switch-cables.jpg

Internet access is essential, but you may wonder which Linux processes use your connection the most on your computer. Fortunately, with some common Linux utilities, monitoring which processes use your bandwidth is easy. Here are some of them:

1. nethogs

nethogs is a program that does for internet connections what htop or top does for CPU and memory usage. It shows you a snapshot of which processes are accessing the network.

Like top, htop, or atop, nethogs is a full-screen program that updates after a few seconds to show you the current network connections by processes.

Installing nethogs is simple. You just go through your package manager.

For example, on Debian and Ubuntu:

 sudo apt install nethogs 

And on Arch Linux:

 sudo pacman -S nethogs 

On the Red Hat family:

 sudo dnf install nethogs 

To run nethogs, you’ll need to be root:

 sudo nethogs 

It’s possible to set it so that you can run nethogs as a regular user using this command:

 sudo setcap "cap_net_admin,cap_net_raw+pe" /path/to/nethogs 

You should replace “/path/to/nethogs” with the absolute pathname of nethogs. You can find this with the which command:

 which nethogs 

2. lsof

While lsof is a utility for listing open files, it can also list open network connections. The -i option lists internet connections attached to running processes on the system. On Linux, everything is a file, after all.

To see current internet connections, use this command:

 lsof -i 

lsof will show you the name of any commands with open internet connections, the PID, the file descriptor, the type of internet connection, the size, the protocol, and the formal file name of the connection.

Using the -i4 and -i6 options allows you to view connections using IPv4 or IPv6.

There’s a good chance you have lsof installed already. It’s also easy to install on major Linux distros if it isn’t.

On Debian and Ubuntu, type:

 sudo apt install lsof 

And on Arch:

 sudo pacman -S lsof 

On the Red Hat family of distros:

 sudo dnf install lsof 

3. netstat

netstat is a powerful program on its own, letting you see network connections on your system. It doesn’t show you which processes the network connections are attached to. As with lsof, you can see this with a command-line option.

netstat is part of the net-tools package. You can install it on most Linux distros using the default package manager.

For example, on Debian or Ubuntu:

 sudo apt install net-tools

On Arch Linux:

 sudo pacman -S net-tools 

To install netstat on Fedora, CentOS, and RHEL, run:

 sudo dnf install net-tools 

You can run netstat at the command line. By default, it will show you information such as the protocol, the address, and the state of the connection, but the -p option adds a column that shows the process ID and the command name.

 netstat -p 

When you run it, netstat will just list all the network connections and then exit. With the -c option, you can see a continually updated list of connections:

 netstat -pc 

This would be similar to using a screen-oriented program like nethogs, but the advantage of doing it this way is that you can pipe the output into another program like grep or a pager to examine it:

 netstat -p | grep 'systemd' 

To see all of the processes with network connections on your system, you may have to run netstat as root:

 sudo netstat  

Now You Can See Which Linux Apps Are Gobbling Up Your Bandwidth

Linux, like many modern OSes, is intimately connected to the internet. It can be difficult at times to track down which processes are using your bandwidth. With tools like nethogs, lsof, and netstat, you can track down processes that have open connections.

Processes sometimes go haywire, even with connections. On Linux, you can easily terminate any rogue processes.

MakeUseOf