How to Install and Use netstat on Linux


Introduction

When managing a Linux server or troubleshooting network issues, it’s crucial to have the right tools to check open ports and active connections. One of the most common tools for this is netstat. However, netstat is not always installed by default on modern Linux distributions. In this blog post, we’ll guide you on how to install netstat and discuss an alternative tool called ss.

Installing netstat on Linux

netstat is part of the net-tools package, which contains a set of networking tools for Linux. This package is often not installed by default on newer Linux distributions, but you can install it easily.

Step 1: Install net-tools

To install netstat, you’ll need to install the net-tools package. The installation command differs depending on your distribution.

For Debian/Ubuntu-based systems, use the following commands:

sudo apt-get update
sudo apt-get install net-tools

For Red Hat/CentOS-based systems, use the following commands:

sudo yum install net-tools    # For older systems
sudo dnf install net-tools    # For newer systems

Step 2: Use netstat

Once net-tools is installed, you can use netstat to check open ports and active connections:

netstat -tuln

Here’s a quick explanation of the options:

  • -t: Show TCP connections.
  • -u: Show UDP connections.
  • -l: Show only listening ports.
  • -n: Show numerical addresses instead of resolving hostnames.

This will show you all the listening ports on your system, along with the protocol and IP addresses.

The ss Command: A Modern Alternative

While netstat has been around for a long time, it’s considered somewhat outdated. Modern Linux systems often prefer the ss (Socket Statictics) command, which is faster and more efficient than netstat.

Using ss

If netstat isn’t available or you prefer using the more modern alternative, you can use ss. It provides similar functionality with improved performance.

Here’s the equivalent command using ss:

ss -tuln

Just like netstat, it shows active connections and open ports. The options work the same way:

  • -t: Show TCP connections.
  • -u: Show UDP connections.
  • -l: Show only listening ports.
  • -n: Show numerical addresses.

Why Use ss Over netstat?

  • Faster Performance: ss can display information much faster than netstat, especially on systems with many open connections.
  • More Features: ss provides more detailed statistics and is better optimized for modern Linux systems.

Conclusion

Both netstat and ss are invaluable tools for system administrators to monitor network activity on Linux systems. While netstat is still widely used, we recommend using ss for faster and more efficient performance.

If you need to install netstat, it’s as simple as installing the net-tools package. However, consider adopting ss as your go-to networking tool for more modern and performance-oriented solutions.


Leave a Comment