Linux Commands

Check network usage per process in Linux

Slow network connections are very annoying and can lead to huge downtime. Linux administrators should keep an eye on what is running on their network and taking much network bandwidth. Monitoring network usage can help to identify the issue and keep network traffic running.

In this article, we will explain a few tools that will allow you to check network usage per process in your Linux system. These tools include IPTraf, Iftop and Nethog. With the help of these tools, you can identify which process or port number is draining much of the network bandwidth.

Note that we have explained the procedure on Ubuntu 18.04 LTS system.

IPTraf

IPTraf is an open-source command-line network monitoring tool that allows monitoring of various network statistics such as TCP, UDP, Ethernet load, ICMP, etc. We can use it to view network usage of a processes running in Linux.

Installation

IPTraf is included in the official repositories of the Ubuntu system. Therefore, we can install it using the apt or apt-get command.

Open the Terminal by using Ctrl+Alt+T shortcut and then run this command as sudo to install IPTraf:

$ sudo apt-get install iptraf -y

During installation, the system might ask for confirmation by providing with Y/n option. Hit y to confirm and then wait for a while until the IPTraf is installed on your system.

Usage

To use IPTraf, run this command in Terminal:

$ sudo iptraf

When you run the above command, you will see the output similar to below. The output shows the network statistics against the source IP address and port number. However, it does not show the specific process which is using the bandwidth.

For some common ports, you can easily guess the process name, such as FTP port number 21, telnet port number 23 and so on. However for some less known ports, to find out a process running against it, use the following netstat command:

$ sudo netstat -tunp | grep <port_number>

For instance, we want to find out the process name against the port number 443. In that case, the command would be:

$ sudo netstat -tunp | grep 433

The output shows the process name “firefox” running against the port number 443.

To view network statistics for a particular interface, use –i followed by interface name:

$ iptraf -i eth0

To view more information about IPTraf, see the man page or use help as follows:

$ sudo iptraf -h

Iftop

Iftop is similar to the top command except it is used for monitoring network usage instead of processor usage. It is an open-source command-line tool that displays current bandwidth usage of a specific interface. Similar to the IPTraf, it also does not displays the process which is using the bandwidth.

Installation

Iftop is available in the official repositories of Ubuntu. Therefore, we can install it simply using the apt or apt-get command.

In the Terminal, run this command as sudo to install Iftop:

$ sudo apt install iftop

During installation, the system might ask for confirmation by providing with Y/n option. Hit y to confirm and then wait for a while until the Iftop is installed on your system.

Usage

To use Iftop, run this command in Terminal:

$ sudo iftop

It displays the sent and received bytes within three different time intervals. At the end, it displays cumulative sent and receive bytes.

To view port number in the display, use –p option as follows:

$ sudo iftop -p

To view information about the specific interface, use –i option as follows:

$ sudo iftop –I <interface_name>

To view more information about iftop, see the man page or use help as follows:

$ sudo iftop -h

Nethogs

Nethogs is an open-source command-line utility that allows viewing network usage of a process running in the Linux system. Unlike the above IPTraf and Iftop tools, Nethogs sorts the output by process and bandwidth usage. Therefore, it comes handy when you need to find which process is using more bandwidth.

Installation

It is also available in the official Ubuntu repositories. Therefore to install it, use the apt or apt-get command as follows:

$ sudo apt-get install nethogs

Wait for a while until the installation of Nethogs is completed.

Usage

To use Nethog for checking network usage, simply type nethogs in the Terminal:

$ sudo nethogs

The output displays the sent and received traffic used by different processes. In the end, it displays the total traffic sent and received by all processes.

To view network usage on a specific interface, use the Nethog command as follows:

$ sudo nethogs <interface_name>

To specify refresh interval, use –d option. For instance, to set 7 seconds as refresh interval, run this command:

$ sudo nethogs –d 7

That is all there is to it! In this article, we have learned how to install and use IPTraf, Iftop, and Nethog in Linux. Through these tools, you can easily check the network usage per process in your Linux system.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.