Posted on
Software

netstat: Display network connections

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Unlocking the Power of netstat: A Guide to Monitoring Network Connections in Linux

One of the most powerful tools in the networking toolbox for Linux users is netstat. This command-line utility is indispensable for administrators and enthusiasts alike, providing valuable insights into the status of network connections, routing tables, interface statistics, masquerade connections, multicast memberships, and more. Whether you are troubleshooting network issues or simply monitoring your network interfaces, netstat makes these tasks manageable. In this post, we'll explore how to use netstat on your Linux system and provide installation instructions to make sure you have this essential tool at your disposal.

What is netstat?

netstat (network statistics) is a command-line tool that displays incoming and outgoing network connections (both TCP and UDP), routing tables, and a number of network interface and network protocol statistics. It helps users to check on various network-related functionality and issues.

Key Features of netstat

  • Network Connections: List all active connections to the system.

  • Routing Table Information: Show content of the routing table.

  • Interface Statistics: Display statistics for different network interfaces.

  • Display PID and Program Name: Shows the program name and process ID for each socket.

  • Supports UNIX, IPv4/IPv6: Shows information relevant to these standards.

Installation Instructions

Before diving into the actual usage, you need to ensure that netstat is installed on your system. The tool is available in the net-tools package, which can be installed using various package managers depending on your distribution.

Debian, Ubuntu, and derivatives:

For systems that use the Advanced Packaging Tool (apt), such as Debian and Ubuntu:

sudo apt update
sudo apt install net-tools

Fedora, CentOS, and derivatives:

For systems that use dnf, such as Fedora:

sudo dnf install net-tools

openSUSE:

openSUSE users can install net-tools using zypper:

sudo zypper install net-tools

After installation, you can type netstat in your console to start using it.

How to Use netstat

Here are some common examples of how to use netstat:

  • List all ports (both listening and non-listening ports):

    netstat -a
    
  • List only listening ports:

    netstat -l
    
  • Show statistics by protocol (TCP, UDP, ICMP, etc.):

    netstat -s
    
  • Find which process/program is listening on a specific port:

    netstat -tulnp | grep ':<port>'
    

(-t for TCP, -u for UDP, -l for listening, -n for showing numerical addresses, and -p to show the program name responsible for the socket.)

Conclusion

netstat is a versatile tool that provides valuable insights during network troubleshooting and monitoring. By familiarizing yourself with its capabilities, you can better understand and manage your network's inner workings. Ensure that you have netstat installed on your system, and begin exploring the various features it offers. Whether you are a seasoned system administrator or a curious hobbyist, netstat is a great addition to your networking toolkit. Happy monitoring!