How to Install and Use netstat on Linux
How to Install and Use netstat on Linux
netstat is a powerful command-line utility that provides detailed information about network connections, routing tables, interface statistics, and more. Although newer tools like ss are available, netstat is still commonly used due to its simplicity and rich feature set. This guide will walk you through the installation and usage of netstat on Linux.
1. Installing netstat
On Ubuntu and Derivatives
The netstat command is part of the net-tools package, which is not installed by default on some modern Linux distributions. To install it, run the following commands:
sudo apt update
sudo apt install net-tools
On Arch Linux
For Arch Linux, the net-tools package includes netstat. Install it by running:
sudo pacman -S net-tools
On Fedora
To install netstat on Fedora, run the following command:
sudo dnf install net-tools
After installation, you can verify the installation by running:
netstat –version
2. Basic Usage of netstat
Once installed, you can start using netstat to analyze network connections, sockets, and more. Here are some common use cases.
2.1 Display All Connections
To list all active network connections, run the following command:
netstat -a
This will display both listening and non-listening sockets.
2.2 Show Listening Ports
To view only listening ports, use the -l option:
netstat -l
2.3 Show TCP Connections
If you are only interested in TCP connections, you can use:
netstat -at
For detailed information about each TCP connection, use the following:
netstat -tuln
Here:
-t stands for TCP connections,
-u stands for UDP connections,
-l shows listening ports,
-n shows addresses and port numbers in numeric form.
2.4 Display Interface Statistics
To display statistics for all network interfaces, run:
netstat -i
2.5 Show Routing Table
To see the kernel’s routing table, run the following command:
netstat -r
This shows the network routing table with details on the destination, gateway, and network interfaces in use.
2.6 Show Network Protocol Statistics
To display statistics for various network protocols like TCP, UDP, ICMP, and more, use:
netstat -s
3. Combining netstat with Other Commands
3.1 Filtering Output with grep
You can use grep to filter netstat’s output. For example, to find all connections on port 80:
netstat -an | grep ‘:80’
3.2 Using watch for Real-Time Monitoring
For real-time monitoring, combine netstat with the watch command:
watch -n 2 netstat -tuln
This will refresh the netstat output every 2 seconds.
4. Alternatives to netstat
While netstat is widely used, it’s worth noting that the tool is becoming deprecated in some distributions. The ss command, which is part of the iproute2 package, offers similar functionality but with better performance. For example, to display all TCP connections using ss, you can use:
ss -at
For a full comparison, consult the Linux Networking Documentation.
Thank you for visiting our site, you can check out our other related articles from the links below 🙂
How to Install iftop on Linux Servers
How to Install and Use TCPdump to Capture Packets on Linux
If you would like to improve yourself in server management, you can purchase a server from our site, experiment and improve yourself in an affordable and reliable environment. I wish you good luck.:)
Conclusion
netstat remains a valuable tool for network troubleshooting and monitoring on Linux systems. In this guide, you learned how to install and use netstat to view network connections, routing tables, interface statistics, and more. Even though tools like ss are recommended in newer distributions, netstat’s simplicity and familiarity make it a great utility to have in your toolkit.
For more detailed usage and options, check out the netstat man page by running:
man netstat