
netstat, or network statistics, is a command-line tool for diagnosing network issues and gathering network statistics. The Netstat Command in Linux displays active connections and listening sockets for TCP, UDP, and UNIX domain sockets, lists associated ports, provides basic network interface statistics, and shows the kernel’s routing table. You can monitor connections, identify open ports, and troubleshoot network issues.
This article will show you how to install and use netstat for monitoring and diagnosing networks in Linux systems.
netstat in LinuxThe netstat command is part of the net-tools package and is available by default in the package repository of popular Linux distributions. Install net-tools to use the netstat command on your Linux system.
To install the net-tools package:
On Debian/Ubuntu distributions, use:
On RHEL distributions, use:
On Arch Linux, use:
On SUSE Linux, use:
On Alpine Linux, use:
Verify the netstat installation.
Output:
netstat Command SyntaxThe command uses the following syntax:
The options modify the command's behavior. Without specifying any, the netstat command displays a list of open sockets.
netstat Command Output AnatomyExecute the netstat command to view all the active connections on your system.
Output:
The output has two sections:
hostname and remote hosts such as example.com.Below is the explanation for each column from the above output:
tcp, udp, or unix socket.ESTABLISHED denotes established connections.STREAM, DGRAM, SEQPACKET, and RAW.unix socket.unix socket.The netstat command without any options shows only the non-listening sockets.
netstat OptionsThe most commonly used options are:
--listening or -l: Displays listening sockets.--all or -a: Displays both the established and non-established connections.--tcp or -t: Displays sockets that use TCP protocol.--udp or -u: Displays sockets that use UDP protocol.--numeric or -n: Displays the port number instead of port name and IP address instead of DNS or hostname.--program or -p: Displays an additional PID/Program name column in the output and shows the program/process associated with that socket. You need to use sudo with this flag to see systemwide processes.The following sub-sections cover various use cases of these and other flags, some in combination with others.
netstat does not list the listening sockets without the -l or -a option.-p flag shows the process owned by the current user. To see all processes (including non-owned ones), use sudo with the netstat command.Execute netstat with the -p or --program option to display open connections with their associated PID (Process ID) or program name.
Output:
The PID/Program name column shows the PID and program name associated with your connection.
Execute the netstat with the -ltup option to view your system's open ports for TCP and UDP protocols.
Output:
Use the -r or --route option with the netstat command to display the kernel routing table and check the default gateway on your system.
Output:
In the above output, the following is the description of the columns:
The following is the description of the last entry of the output:
0.0.0.0 gets routed through the gateway wlp4s0.wlp4s0.U means the route is UP, and G means the route is a gateway.Execute netstat with the -i or --interface to display network interface statistics on your system.
Output:
From the above output, you can see the following:
lo interface with the default MTU 65536 has received and transmitted 6111 packets. The lo is a loopback interface that is up and running.wlp4s0 interface with the default MTU 1500 has received packets 1164542 bytes, transmitted 822802 bytes, and dropped 38 bytes. The wlp4s0 is up and running with the broadcast set and multicast enabled.The explanation of each column for the network interface statistics:
Execute netstat with the -apn option to view all active connections on your system.
Expected output:
Below is the explanation for each option in the above command:
-a or --all: Display all listening and non-listening connections.-p or --program: Display the PID (Process ID) and program name.-n or --numeric: Display the output in the numeric format.Add the -t or --tcp option to show all active connections for the TCP protocol.
Output:
Add the -u or --udp option to show all active connections for the UDP protocol.
Output:
The -o or --timer option displays the network timers. Use this option to diagnose network connection timeouts.
Output:
Notice the Timer column that displays the timer data for the socket.
The -l or --listening option displays listening sockets. To check sockets for TCP and UDP protocols with the numerical address, use:
Output:
In this article, you learned how to use the netstat command in Linux for monitoring, diagnosing, and gathering network information and statistics. You can now use netstat to gain insight into your network performance and troubleshoot issues. To learn more about netstat, execute the command man 8 netstat or visit Netstat documentation.
0 Comments
Be the first to comment and share your perspective with the community.