in

Troubleshooting: CentOS 7 – netstat command not found

default image

As a Linux administrator, one of my most frequently used commands for diagnosing network issues is netstat. It provides invaluable insight into the status of network connections, open ports, routing tables, and more. So you can imagine my surprise when setting up a new CentOS 7 server and running into the dreaded "netstat: command not found" error!

In this comprehensive guide, we‘ll cover everything you need to know to get netstat working again on CentOS 7.

An Introduction to Netstat

For those less familiar, netstat is a powerful networking utility that‘s been around since the early days of UNIX. It allows you to:

  • List out all active TCP and UDP connections
  • See which ports are open and listening for connections
  • Identify the processes/programs utilizing each connection
  • View routing tables and network interface statistics
  • Monitor performance and errors at the IP/TCP protocol layer

As a network analyst and Linux enthusiast, I can‘t emphasize enough how valuable netstat is for troubleshooting all sorts of connectivity and performance issues.

Over the many years I‘ve spent managing Linux servers, netstat has helped me track down everything from port conflicts to routing misconfigurations. I consider it an essential tool in every sysadmin‘s toolbox.

So why is this staple utility suddenly missing from CentOS 7?

The Case of the Missing Netstat

Starting with CentOS 7, the classic net-tools package containing netstat and other networking commands like ifconfig is no longer installed by default.

This is because CentOS has shifted to using the newer iproute2 suite for network configuration and diagnostics. The iproute2 tools like ip and ss do offer similar functionality to netstat and ifconfig, but have redesigned internals and syntax.

However, many Linux veterans still prefer the simplicity and familiarity of the tried-and-true netstat. The good news is you can easily get netstat back on CentOS 7, it just takes a quick package install.

Installing Netstat on CentOS 7

To install netstat on CentOS 7 along with the other classic net-tools utilities, just run:

sudo yum install net-tools

This will pull the net-tools package from the CentOS repositories and install netstat and friends onto your system.

Below we can see the full output from my CentOS 7 server installing net-tools:

[root@centos7 ~]# yum install net-tools
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.24.20160912git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================
 Package        Arch         Version               Repository          Size
======================================================================================
Installing:
 net-tools      x86_64       2.0-0.24.20160912git.el7   base           304 k

Transaction Summary
======================================================================================
Install  1 Package

Total download size: 304 k
Installed size: 909 k
Is this ok [y/d/N]: y 
Downloading packages:
net-tools-2.0-0.24.20160912git.el7.x86_64.rpm               | 304 kB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.24.20160912git.el7.x86_64       1/1 
  Verifying  : net-tools-2.0-0.24.20160912git.el7.x86_64       1/1 

Installed:
  net-tools.x86_64 0:2.0-0.24.20160912git.el7                   

Complete!

Once the install finishes, netstat and friends will be ready to use! Next let‘s go over some ways to verify netstat is working as expected.

Verifying Netstat is Working

After installing net-tools, you can confirm netstat is available with a quick status check:

[root@centos7 ~]# netstat -an | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0     36 192.168.56.3:22         192.168.56.1:50116      ESTABLISHED
...

The output above shows netstat running properly, listing out active TCP connections.

Some other handy validation commands include:

  • netstat -lntu – View listening UDP/TCP ports
  • netstat -s – Print network statistics by protocol
  • netstat -ie – Show interface stats

You can also verify the package install itself by querying yum:

[root@centos7 ~]# yum list installed | grep net-tools
net-tools.x86_64                  2.0-0.24.20160912git.el7    @base

As long as net-tools shows up in installed packages, you should be good to go!

Netstat Usage and Examples

Now for the good stuff: practical examples of using netstat for network diagnostics and troubleshooting.

Here are just a few of the many scenarios where netstat shines:

Finding the Process Bound to a Port

[root@centos7 ~]# netstat -anp | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      25312/mysqld

This shows the mysqld process listening on TCP port 3306.

Checking Active Connections to a Specific IP

[root@centos7 ~]# netstat -an | grep 192.168.1.100
tcp        0     64 192.168.1.120:22       192.168.1.100:56592     ESTABLISHED
tcp        0      0 192.168.1.120:80       192.168.1.100:57942     TIME_WAIT  

Here we see all connections to/from 192.168.1.100. Useful for Diagnosing traffic flow issues.

Monitoring Total Transmission Rates

[root@centos7 ~]# netstat -s | grep -i segments
  Segments retransmitted: 1
  Segments received: 38165
  Segments sent: 15535 

Segment counts indicate volume of traffic transmitted and retransmitted.

Reviewing the Routing Table

[root@centos7 ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         10.109.0.1      0.0.0.0         UG        0 0          0 eth0
10.109.0.0      0.0.0.0         255.255.252.0   U         0 0          0 eth0

The routing table is critical for identifying issues sending packets.

This just scratches the surface of what‘s possible with netstat! The key is flexibility – with the right filters and flags you can zero in on exactly the data you need.

Conclusion

While replacing netstat with more modern tools like ss may be the future direction, this time-tested utility remains invaluable for many Linux admins and engineers. Getting netstat working again takes just a quick yum install on CentOS 7.

I highly recommend installing net-tools and keeping netstat handy in your troubleshooting toolkit. Its simple, flexible and fast output can help pinpoint the most obscure network and connectivity issues. And you never know when in-depth traffic analysis with netstat could save the day!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.