SoFunction
Updated on 2025-03-04

Summary of the method to view the current number of network connections in Linux system

introduction

In daily operation and maintenance and development, monitoring of network connections is a very important indicator. When you need to troubleshoot network problems or optimize system performance, checking the number of connections is a critical step. However, different Linux environments and distributions may require different methods to obtain connection information.

This article will start with the most common tools and gradually explore how to view the current number of network connections in Linux systems and solve possible problems.

1. Basic command to view the number of connections

1. Use netstat to view the number of connections

netstatIt is a classic network management tool that can display network connections, routing tables, interface statistics and other information. To view the number of network connections to the current system, you can use the following command:

View all connections

netstat -an | grep ESTABLISHED | wc -l

This command will:

  • usenetstat -anList all connections.
  • usegrep ESTABLISHEDFilter out connections with status ESTABLISHED.
  • usewc -lCount the number of connections.

Check the number of connections for a specific port

If you just want to see the number of connections for a certain port (e.g. 8080), you can run:

netstat -an | grep ':8080' | wc -l

Real-time monitoring of connections

If you need to monitor the number of connections in real time, you can combine it withwatchOrder:

watch -n 1 'netstat -an | grep ESTABLISHED | wc -l'

This command refreshes the current number of connections every second.

2. Solutions to the unavailability of netstat command

In some systems, executenetstatThe following error may appear when  :

bash: netstat: command not found

This is because the netstat command belongs to the net-tools toolkit, which is not installed by default in modern Linux distributions such as CentOS 8 and Ubuntu 18.04 and later.

1. Install net-tools

Install in CentOS

Install net-tools using the following command:

yum install -y net-tools

Install in Ubuntu/Debian system

If it is a Debian-based distribution, you can run:

apt update
apt install -y net-tools

After the installation is completed, run againnetstatJust order.

3. Use ss instead of netstat

ssis a more efficient network management tool in modern Linux systems, which can providenetstatSimilar information, but better performance. Most Linux systems are already installed by defaultss

Use ss to view the number of connections

View all connections

ss -ant | grep ESTABLISHED | wc -l

The commands here andnetstatSimilarly, the main difference is:

  • ss -antList all TCP connections.
  • grep ESTABLISHEDandwc -lThe function remains unchanged.

Check the number of connections for a specific port

If you only need to count the number of connections to a certain port (such as 8080), you can run:

ss -ant | grep ':8080' | wc -l

View connection details

If more detailed information is needed (such as the process ID of the connection), you can use:

ss -antp

4. Deal with the problem of minimalist environment

In some minimalist Linux environments, such as Docker containers, the following situations may occur:

  • NoyumoraptPackage manager.
  • Not installed by defaultnet-toolsorss

In this case, the solution needs to be selected according to the specific environment.

1. Check the container environment

First, check the container basic image used. For example, run the following command to confirm:

cat /etc/os-release

2. Install the necessary tools

For Alpine-based containers

Alpine containers are usually usedapkPackage manager, can be installednet-toolsoriproute2

apk add net-tools
apk add iproute2

For Debian/Ubuntu-based containers

The following commands can be run:

apt update
apt install -y net-tools iproute2

5. Practical skills

1. Check the number of connections for each process

If you need to count the number of connections for each process, you can use:

netstat -anp | grep ESTABLISHED | awk '{print $7}' | cut -d'/' -f1 | sort | uniq -c

This counts the number of connections for each process in all connections.

2. View all connection status statistics

Can be used directlyssStatistical functions provided:

ss -s

This outputs the number of connections in the current system for various states (such as ESTABLISHED, TIME_WAIT).

3. Combined with watch real-time monitoring

For frequently changing network connections, combined withwatchIt is very practical:

watch -n 1 'ss -ant | grep ESTABLISHED | wc -l'

6. Summary

Through this article, you should be able to use netstat and ss to view the number of network connections to your Linux system and quickly find alternatives when tools are missing or commands are unavailable.

In modern systems, it is recommended to give priority to using ss, which not only performs better, but also provides more detailed network information. For containerized environments, you can select the appropriate package management tool according to the image type to install the required commands.

This is the article about how to view the current network connection number in Linux system. For more related content on Linux to view connection number, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!