Home  >  Article  >  System Tutorial  >  How to monitor network usage of processes on Linux

How to monitor network usage of processes on Linux

WBOY
WBOYforward
2024-02-05 16:45:021312browse

In computers, access to the Internet is absolutely essential. However, you might be interested in knowing which Linux processes on your computer are using the connections most frequently. Fortunately, monitoring processes using bandwidth is very easy with the help of some common Linux tools. The following is an introduction to several tools:

  1. nethogs

nethogs is a program similar to htop or top that provides CPU and memory usage in terms of internet connections. It provides a quick view of which processes are using a network connection.

Similar to top, htop, or atop, nethogs is a full-screen program that updates every few seconds to show the network connections established by the current process.

You can easily install nethogs through your package manager.

For example, on Debian and Ubuntu:

linuxmi@linuxmi ~/www.linuxmi.com
 % sudo apt install nethogs
如何监控 Linux 上进程的网络使用情况

On Arch Linux:

sudo pacman -S nethogs

On the Red Hat series:

sudo dnf install nethogs

To run nethogs you need to run as root:

linuxmi@linuxmi ~/www.linuxmi.com
 % sudo nethogs
如何监控 Linux 上进程的网络使用情况

You can set it to run nethogs as a normal user with the following 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 it using which command:

which nethogs

Related:

  • How to use htop to monitor Linux system processes https://www.linuxmi.com/htop.html
  • The most important Linux commands no one teaches you https://www.linuxmi.com/linux-commands-important.html

2、lsof

如何监控 Linux 上进程的网络使用情况

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

To view the current internet connection, use the following command:

linuxmi@linuxmi ~/www.linuxmi.com
 % lsof -i

lsof displays the name, PID, file descriptor, internet connection type, size, protocol, and the connection's official file name for any command that has an open Internet connection.

Using the -i4 and -i6 options, you can view connections using IPv4 or IPv6.

Most likely you already have lsof installed. If you don't have it installed, it's easy to install on major Linux distributions.

On Debian and Ubuntu, enter:

sudo apt install lsof

And on Arch:

sudo pacman -S lsof

On Red Hat series distributions:

sudo dnf install lsof

3、netstat

netstat is a powerful program that allows you to view network connections on your system. But it doesn't show which processes the network connection is attached to. Like lsof, you can use command line options to view this information.

netstat is part of the net-tools package. You can install it on most Linux distributions 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 on the command line. By default, it displays the connection's protocol, address, and status, but the -p option adds a column showing the process ID and command name.

linuxmi@linuxmi ~/www.linuxmi.com
 % netstat -p
如何监控 Linux 上进程的网络使用情况

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

linuxmi@linuxmi ~/www.linuxmi.com
 % netstat -pc
如何监控 Linux 上进程的网络使用情况

这与使用像 nethogs 这样的面向屏幕的程序类似,但以这种方式进行的优点是您可以将输出管道传输到另一个程序,如 grep 或分页程序,以便检查它:

linuxmi@linuxmi ~/www.linuxmi.com
 % netstat -p | grep 'systemd'
如何监控 Linux 上进程的网络使用情况

要查看系统上具有网络连接的所有进程,您可能需要以 root 身份运行 netstat:

linuxmi@linuxmi ~/www.linuxmi.com
 % sudo netstat
如何监控 Linux 上进程的网络使用情况

相关:Linux 下 12 个监视网络连接的 ss 命令示例 https://www.linuxmi.com/linux-ss-command.html

现在您可以看到哪些 Linux 应用程序在占用您的带宽

与许多现代操作系统一样,Linux 与互联网紧密连接。有时候很难追踪哪些进程在使用您的带宽。通过使用 nethogs、lsof 和 netstat 等工具,您可以找到具有打开连接的进程。

有时进程会失控,即使有连接。在 Linux 上,您可以轻松终止任何恶意进程。

The above is the detailed content of How to monitor network usage of processes on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete