Cyberithub

How to Install and Use netstat command in Linux(v2.10) with Best Examples

Advertisements

In this tutorial, I will take you through the steps to install and use netstat command in Linux. It is basically used to Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

Install and Use Netstat Command

Before you go and use netstat command you need to make sure it is installed in your system. Below are the steps to install net-tools package in RedHat/CentOS/Ubuntu and best examples of how to use netstat command during any troubleshooting.

How to Install and Use netstat command in Linux(v2.10) with Best Examples 1

Install net-tools

To install netstat tool in your system, you need to install net-tools package using below given command.

Installation on CentOS/RedHat

You need to use yum install net-tools command to install in CentOS/RedHat.

[root@localhost ~]# yum install net-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* extras: centos.excellmedia.net
* updates: centos.excellmedia.net
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution

...................................................................................................................................................................

Installation on Ubuntu

Here you need to use apt-get install net-tools command to install in Ubuntu.

root@localhost:~# apt-get install net-tools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
dh-python libpython3-dev libpython3.6-dev python3-dev python3-wheel python3.6-dev
Use 'apt autoremove' to remove them.
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 106 not upgraded.
Need to get 194 kB of archives.
After this operation, 803 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 net-tools amd64 1.60+git20161116.90da8a0-1ubuntu1 [194 kB]
Fetched 194 kB in 0s (587 kB/s)
Selecting previously unselected package net-tools.
(Reading database ... 189416 files and directories currently installed.)
............................................................................

Netstat Examples

Check the version

If you want to check the version of the netstat installed, you can use -V switch.

[root@localhost ~]# netstat -V
net-tools 2.10-alpha
Fred Baumgarten, Alan Cox, Bernd Eckenfels, Phil Blundell, Tuan Hoang, Brian Micek and others
+NEW_ADDRT +RTF_IRTT +RTF_REJECT +FW_MASQUERADE +I18N +SELINUX
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE -BLUETOOTH
HW: +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64

Check TCP Connection

If you want to check all active tcp connections, you can use -ant switch.

[root@localhost ~]# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address      Foreign Address       State
tcp     0       0    127.0.0.1:25          0.0.0.0:*         LISTEN
tcp     0       0    0.0.0.0:22            0.0.0.0:*         LISTEN
tcp     0      64    192.168.0.105:22  192.168.0.101:53705 ESTABLISHED
tcp6    0       0       ::1:25               :::*            LISTEN
tcp6    0       0       :::22                :::*            LISTEN

Check UDP Connections

If you want to check all active udp connections, you can use -anu switch.

[root@localhost ~]# netstat -anu
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp     0      0    127.0.0.1:323    0.0.0.0:*
udp6    0      0      ::1:323          :::*

Check Running Services and its Ports

If you want to check all the current running services along its process ID and port number,you can use below command.

[root@localhost ~]# netstat -pnltu
Active Internet connections (only servers)
Proto Recv-Q Send-Q   Local Address Foreign Address State PID/Program name
tcp     0      0    127.0.0.1:25       0.0.0.0:*    LISTEN 1323/master
tcp     0      0    0.0.0.0:22         0.0.0.0:*    LISTEN 1132/sshd
tcp6    0      0     ::1:25              :::*       LISTEN 1323/master
tcp6    0      0     :::22               :::*       LISTEN 1132/sshd
udp     0      0    127.0.0.1:323      0.0.0.0:*            713/chronyd
udp6    0      0     ::1:323             :::*               713/chronyd

Check Routing Table

If you want to check the current routing table information, you need to use -nr switch with netstat command.

[root@localhost ~]# netstat -nr
Kernel IP routing table
Destination Gateway     Genmask      Flags  MSS Window irtt  Iface
0.0.0.0     192.168.0.1 0.0.0.0        UG    0    0      0   enp0s3
172.17.0.0  0.0.0.0     255.255.0.0    U     0    0      0   docker0
192.168.0.0 0.0.0.0     255.255.255.0  U     0    0      0   enp0s

Check Network Interface Stats

If you want to check your overall stats of all the network interfaces, use -ai switch with netstat command.

[root@localhost ~]# netstat -ai
Kernel Interface table
Iface    MTU  RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
docker0  1500   0     0      0       0     0     0       0     0    BMU
enp0s3   1500 76837   0      0       0    99053  0       0     0    BMRU
lo       65536 2902   0      0       0    2902   0       0     0    LRU

Also Read:  Top 10 Ping command in Linux

Reference: Netstat Documentation

Leave a Comment