Network Status (netstat) displays information on the IP configuration of a host but goes further to show its connections, including ports, protocols, and metrics on the communications. The netstat command, known as network statistics, is a versatile command with many features. This command prints network connection details, kernel routing table information, interface statistics, open ports, and so on. In this section, we restrict ourselves to using the netstat command to fetch the routing table information only.
Following are examples to illustrate the usage of the netstat command.
– Use the netstat command to display the kernel routing table information with the -r option as shown here:
$ netstat -r
– Use the netstat command to display the kernel routing table information without resolving the hostname or port names as shown here:
$ netstat -anr
If you encounter below error while running the netstat command:
netstat: command not found
you may try installing the below package as per your choice of distribution.
Distribution | Command |
---|---|
Debian | apt-get install net-tools |
Ubuntu | apt-get install net-tools |
Alpine | apk add net-tools |
Arch Linux | pacman -S net-tools |
Kali Linux | apt-get install net-tools |
CentOS | yum install net-tools |
Fedora | dnf install net-tools |
Raspbian | apt-get install net-tools |
netstat Command Examples
1. To display the kernel routing table:
# netstat -r # netstat --route
2. To display multicast group membership information
# netstat -g # netstat --groups
3. To display the kernel interface table:
# netstat -i # netstat --interfaces # netstat --interfaces=eth0
4. To display a list of masqueraded connections:
# netstat -M # netstat --masquerade
5. To display summary statistics of each protocol:
# netstat -s # netstat --statistics
6. To set to verbose :
# netstat -v # netstat --verbose
7. To show numerical addresses instead of trying to determine symbolic host:
# netstat -n # netstat --numeric
8. To numerical host addresses but does not affect the resolution:
# netstat --numeric-hosts
9. To numerical port numbers but does not affect the resolution:
# netstat --numeric-ports
10. To show numerical user IDs but does not affect the resolution:
# netstat --numeric-users
11. To print the selected information every second continuously:
# netstat -c # netstat --continuous
12. To display the additional information:
# netstat -e # netstat --extend
13. To include information related to networking timers:
# netstat -o # netstat --timers
14. To show the PID and name of the program to which each socket belongs:
# netstat -p # netstat --program
15. To show only listening sockets:
# netstat -l # netstat --listening
16. To show both listening and non-listening sockets:
# netstat -a # netstat --all
17. To print routing information from the FIB:
# netstat -F
18. To print routing information from the route cach:
# netstat -C
19. To print SELinux context:
# netstat -Z # netstat --context
20. To stop trimming long addresses:
# netstat -T # netstat --notrim
21. To set delay:
# netstat delay 2
22. To get the help:
# netstat -h # netstat --help
23. To get the version:
# netstat --version
netstat examples with multiple options combined
1. To see the currently active routing table:
# netstat -rnv
2. To see all the socket information:
# netstat -anp
3. To see all the listening ports:
# netstat -anp | grep LISTEN
4. To see all the process/services running and listening on ports:
# netstat -lnptu
5. To see the protocol specific statistics (e.g. for TCP):
# netstat -pt
6. To see the interface activity stats:
# netstat -i -t 2 # netstat -it 2
7. To see the summary stat for a particular port:
# netstat -st (For TCP) # netstat -su (For UDP)
8. View the list of network services that are listening for a connection:
# netstat -lp -A inet # netstat -lpn -A inet
9. View the list of established connections:
# netstat -p -A inet # netstat -pn -A inet
Note
ping, traceroute, and netstat are old command-line utilities used to test the reachability of hosts, provide routing information, and give information about network connections. They often are the first tools used by network technicians when testing networks. netstat is one of the few commands that is available by default on Unix-like operating systems and Windows operating systems as well. We can use the netstat command to troubleshoot a number of different issues.