Struggling to understand the Linux ‘netstat‘ command? This guide will provide an overview of the functionality of ‘netstat‘, as well as detailed instructions on how to use it correctly. With a few simple commands, you’ll be able to troubleshoot networking issues and gain valuable insight into your system’s performance.
Netstat is the most used networking tool used for monitoring network connections both incoming and outgoing as well as viewing routing tables, interface statistics, viewing details of listening ports etc. in this article we will be showing how to use the netstat command in system administration tasks. Netstat command provides detailed information about network connections and associated processes making it an essential tool for system administrators who need to manage and monitor their networks effectively.
Installing netstat on linux
netstat is not installed by default on all systems. So initially if you try to run netstat command it will show netstat command is not found
On a Debian-based system (such as Ubuntu), you can use the following command to install
sudo apt-get update
sudo apt-get install net-tools
On a Red Hat-based system (such as CentOS or Fedora), you can use the following command:
sudo yum install net-tools
netstat is installed, you should be able to use it by typing
netstat at the command prompt.
Some Practical use cases of netstat command in Linux
1: Netstat command used to list all the network interfaces of a system.
2: Netstat command used to list all the listening and non-listening sockets of both TCP and UDP.
netstat -a | more
If you want to list only the TCP ports use Netstat -at and if you want to display only the UDP ports use Netstat -au.
3: Netstat command used to list only the listening connections.
similarly, if you want to view only the listening TCP ports use the command netstat -lt and for UDP ports use the command netstat -lu.
4: Netstat command used to display the process id of all connections.
Also, you can use netstat -pt command to display the process id of all TCP connections and netstat -tu command to display the process id of all UDP connections.
5: Netstat command used to display the list of networks, their current states, and their associated ports.
Similarly you can use the grep command along with netstat command to see the status of a particular port and which service is listening on that port.
“netstat -pnltu | grep 53”
6: How to use Netstat command to check all the listening connections.
netstat -an | grep ‘LISTEN’
netstat command piped with grep command can be used to check all the listening connections in the system
7: How to see the kernel routing table information using Netstat command.
Other alternative tools for netstat
Other popular networking tools include ss, iptraf, and tcpdump.
SS is an alternative to netstat and provides more detailed information about sockets and network connections. It’s also faster and more efficient than netstat, making it an ideal tool for large-scale monitoring and analysis.
Iptraf is a command-line tool that provides real-time monitoring of network traffic and bandwidth utilization. It offers a variety of display modes and filtering options, making it easy to analyze and troubleshoot network performance issues.
Tcpdump is a packet sniffer and analyzer that captures and displays network packets in real time. It’s a powerful tool for network troubleshooting, as it allows administrators to inspect packets and diagnose problems at the packet level.
Netstat vs. Nslookup: What’s the Difference?
Netstat and nslookup are both command-line tools used in Linux. Netstat is used to display active network connections and their status, while nslookup is used to query DNS servers for information about domain names and IP addresses. In short, netstat shows you what connections are currently active on your system, while nslookup helps you find information about specific domain names or IP addresses.
Netstat is a powerful tool for networking and debugging network-related issues. In this article, we have covered the important options of the Netstat command. please refer the man page of netstat command for more details https://man7.org/linux/man-pages/man8/netstat.8.html .