Home > System Tutorial > LINUX > body text

How to monitor network usage of processes on Linux

WBOY
Release: 2024-02-05 16:45:02
forward
1318 people have browsed it

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
Copy after login
如何监控 Linux 上进程的网络使用情况

On Arch Linux:

sudo pacman -S nethogs
Copy after login

On the Red Hat series:

sudo dnf install nethogs
Copy after login

To run nethogs you need to run as root:

linuxmi@linuxmi ~/www.linuxmi.com
 % sudo nethogs
Copy after login
如何监控 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
Copy after login

You should replace "/path/to/nethogs" with the absolute pathname of nethogs. You can find it using which command:

which nethogs
Copy after login

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
Copy after login

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
Copy after login

And on Arch:

sudo pacman -S lsof
Copy after login

On Red Hat series distributions:

sudo dnf install lsof
Copy after login

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
Copy after login

On Arch Linux:

sudo pacman -S net-tools
Copy after login

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

sudo dnf install net-tools
Copy after login

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
Copy after login
如何监控 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
Copy after login
如何监控 Linux 上进程的网络使用情况

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

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

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

linuxmi@linuxmi ~/www.linuxmi.com
 % sudo netstat
Copy after login
如何监控 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!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!