How to Install netstat Command in Linux

Netstat, short for “network statistics,” is a command-line network utility tool used in various operating systems, including Windows, Linux, and macOS. It provides information and statistics about network connections, routing tables, interface statistics, masquerade connections, and more. Netstat is particularly useful for diagnosing network-related issues and monitoring network activities.

Some common uses of the netstat command include:

  1. Viewing Active Network Connections: You can use netstat to see a list of all active network connections on your computer, including the local and remote IP addresses, port numbers, and the current state of the connections.
  2. Displaying Network Routing Tables: Netstat can show you the routing table for your computer, which is important for understanding how data is routed within your network.
  3. Monitoring Network Interfaces: You can use netstat to view statistics about network interfaces, such as the number of packets sent and received, error counts, and more.
  4. Identifying Listening Ports: Netstat can help you identify which network ports are being listened to by your computer. This is particularly useful for understanding which services or applications are currently running and listening for incoming network connections.
  5. Checking for Network Services and Open Ports: You can use netstat to check which network services are running and which ports are open on your computer. This can be essential for security and firewall configuration.
  6. Monitoring Network Performance: Netstat can provide information on network performance, including packet and byte counts, packet errors, and more.

How to Install netstat Command in Linux

The package that contains netstat is called net-tools. On modern systems, the netstat utility comes pre-installed and there’s no need to install it.

On older systems, however, you are likely to bump into an error when you run the netstat command.

bash: netstat: command not found

Therefore, to install netstat on Linux distributions, run the command.

$ sudo apt install net-tools         [On Debian, Ubuntu and Mint]
$ sudo yum install net-tools         [On RHEL/CentOS/Fedora and Rocky/AlmaLinux]
$ sudo emerge -a sys-apps/net-tools  [On Gentoo Linux]
$ sudo apk add net-tools            [On Alpine Linux]
$ sudo pacman -S net-tools           [On Arch Linux]
$ sudo zypper install net-tools      [On OpenSUSE]    

Once installed, run the command below to check the version of netstat installed.

# netstat -v

How to Use netstat Command in Linux

You can invoke the netstat command on any of the Linux distributions to get different statistics on your network.

1. Viewing the Network Routing Table

You use the -r flag to show the network routing table to get something similar to the output below.

# netstat -nr
List Network Routing Table

The -n option forces netstat to print addresses separated by dots instead of using symbolic network names. The option is useful for avoiding address lookups over a network.

2. Display Network Interface Statistics

Use the -i flag to get an output of statistics of a network interface that is configured. The -a option prints all present interfaces in the kernel.

# netstat -ai
List Network Interface Statistics

3. Show Network Connections

The netstat command utility supports options that display active or passive sockets using the options -t-n, and -a. The flags show RAW, UDP, TCP, or UNIX connection sockets. Adding the -a option, it will sow sockets ready for connection.

# netstat -ant
List Network Connections

4. Show Network Services

To list services, their current state, and their corresponding ports, run the command.

# netstat -pnltu

We will be happy to hear your thoughts

Leave a reply

Unix
Logo