CommandsLinux

Netstat Command in Linux with Examples

Netstat Command on Linux

In this tutorial, we will discuss your mastering the netstat command on Linux. Netstat is one of the most versatile and powerful tools in a Linux administrator’s arsenal. This command line utility provides valuable insight into the network connections and communication happening on a Linux system. Whether you’re a seasoned system administrator or just starting out, understanding the netstat command is an essential part of your Linux education. In this post, we’ll explore the many uses of netstat, from displaying active network connections to monitoring the health of your network interfaces. By the end, you’ll have a complete understanding of this essential Linux tool and how it can help you troubleshoot network problems and optimize your system’s performance.

What is Netstat Command?

The Netstat command displays various network-related information such as network connections, routing tables, interface statistics, masquerade connections, Multicast memberships, etc. in a human-readable format.

Understanding and using Netstat effectively allows Linux administrators to stay on top of their systems‘ network health, analyze performance, and troubleshoot problems before they cause outages. It’s one of the most versatile networking commands in a Linux admin‘s toolkit.

Installing netstat on Linux

Before diving into installation, it’s prudent to ensure netstat is not only installed but functioning optimally. Use the following commands to check:

  • For Debian/Ubuntu systems:
dpkg -l | grep net-tools
sudo apt update
sudo apt install net-tools
  • For Red Hat/CentOS systems:
rpm -qa | grep net-tools
sudo yum install net-tools

Basic Syntax and Options of Netstat Command

The basic syntax of the netstat a command is as follows:

netstat [options]

The following are some of the most commonly used Netstat options:

Option Description
-a Shows all active connections and the status of these connections.
-t Shows only TCP connections.
-u Shows only UDP connections.
-l Shows only listening connections.
-p Shows the process ID and name of the process associated with each connection.
-n Displays addresses and port numbers in numerical format, rather than resolving them to hostnames or port names.

Here are some examples of how you can use the netstat command to gather information about your network:

  • To display all active TCP connections, run the following command:
netstat -t
  • To display all active UDP connections, run the following command:
netstat -u
  • To display all active connections, including TCP and UDP connections, run the following command:
netstat -a
  • To display all listening connections, run the following command:
netstat -l
  • To display the process ID and name of the process associated with each connection, run the following command:
netstat -p
  • To display addresses and port numbers in numerical format, run the following command:
netstat -n
  • To view the active outgoing connections on a computer, you can use the following command:
netstat -n | grep ESTABLISHED
  • To view the connections that are listening for incoming connections, you can use the following command:
netstat -an | grep LISTEN
  • To display only the TCP connections on a computer, you can use the following command:
netstat -atn
  • To display only the UDP connections on a computer, you can use the following command:
netstat -aun
  • To display the network connections at regular intervals, you can use the following command:
netstat -atn | grep ESTABLISHED | watch -n 5

Best Practices When Using Netstat

Here are some tips for using Netstat effectively based on real-world troubleshooting experience:

  • Check Netstat output occasionally to baseline normal network behavior
  • Monitor network traffic with Netstat after making firewall/routing changes
  • Sort Netstat output by port number or state to quickly identify anomalies
  • Always check listening ports after installing new services
  • Look for connections in the TIME_WAIT state to identify potential resource leaks
  • Analyze routing table data if troubleshooting Internet connectivity issues
  • Combine Netstat with tcpdump when diagnosing connectivity problems to client/server applications.

Conclusion

In conclusion, netstat a command is a powerful tool that can provide valuable information about your network. By using the various options and examples outlined in this article, you can quickly and easily monitor network connections, identify potential issues, and make informed decisions. Whether you are a network administrator or simply someone who wants to monitor their network, the netstat a command is an essential tool to have in your toolkit.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button