Do you know how to see all the network information flowing into and out of your Linux machine?
It doesn’t matter if you’re running a stand alone Linux machine or a Linux cloud of virtual machines. Networking information and statistics is critical to keeping your Linux distribution secure and running effectively.
In this article, we will show you how to install the Linux
netstat command and how to use it.
Let’s look at it more detail and the various benefits first of all.
netstat is short for network statistics and is used from the command line in Linux. It displays network TCP connections. routing tables, network interfaces and statistics about the network.
Why do you need to use the
But why should you spend your time and commitment learning a new Linux command. And especially one used only for networking.
Here are the main two reasons.
1. Discover the networking statistics of the Linux machine you are working on. If your Linux machine acts as a server of some sort it will have a lot network traffic that is of interest to you.
And, not all traffic may be what you want or expect. It may be of a malicious nature for instance. By using
netstat you can decipher and work this out.
2. It is used frequently to administer Linux and Unix networks – The
netstat command is one of the most widely used commands in Linux network administration. As such, it is essential to at least have a working knowledge of how it works.
How to Setup and Install
netstat in Linux
netstat command is actually part of a package called
net-tools rather than a standalone tool. You can see if it is already installed by executing it in the console as follows.
If you get an error then you need to install it according to your Linux distribution package manager setup.
For CentOS use:
yum install net-tools
For Ubuntu use:
apt install net-tools
If it installs without any errors then try calling it again to print out the version information.
How to Use the
netstat Command in Linux
To view the network routing table with
netstat we need to use the
-r flag as follows.
If we include
-n option too
netstat will print the IP addresses separated by dots.
We can also display the network interface statistics by using the
-i flag, while using the
-a option will print to the console all the system interfaces as follows.
We can print details of the network connections with
netstat easily as it will allow you to see the display active or passive sockets via the flags
-a as follows.
We can see the details of the network services too by using the following command to list services, the current state and ports.
If you work a lot on Linux, needing to investigate networking information and statistics is inevitable. But there’s no reason to try and do yourself manually. The Linux netstat command has everything in built you need.
netstat command strategies and techniques above to find out what you need to know about the networking properties of the Linux machine you are on.
netstat uses have you encountered and useful command parameters? Let me know in the comments.