Updated April 17, 2023
Introduction to Linux netstat
Network Statistics (Netstat) is a command-line utility tool for auditing network connections for incoming and outgoing connections and also to view interface statistics, routing tables, and many more. Netstat is available in Linux, Unix, and Windows operating systems as well. To handle critical network-related issues or to check connection statistics, netstat is a powerful command-line tool. It also helps in resolving all network-related connection problems in the system.
Syntax:
Below are different ways to use netstat command in Linux
usage: netstat [-vWeenNcCF] [<Af>] -r
netstat {-V|--version|-h|--help}
netstat [-vWnNcaeol] [<Socket> ...]
netstat { [-vWeenNac] -i | [-cnNe] -M | -s [-6tuw] }
How does Netstat command work in Linux?
Below are the options that we can use along with netstat command in linux to solve different issues related to Linux. It also helps in displaying the statistical report of all the active networks connected on the system.
Option | Description | |
-r, –route | To display info routing table | |
-i, –interfaces | To print the interface table | |
-g, –groups | To print multicast group membership details | |
-s, –statistics | To print network statistics (like SNMP) | |
-M, –masquerade | To print masqueraded connection | |
-v, –verbose | To print be verbose related address | |
-W, –wide | Not to truncate IP addresses details | |
-n, –numeric | Not to resolve names | |
–numeric-hosts | Not to resolve hostnames | |
–numeric-ports | Not to resolve port names | |
–numeric-users | Not to resolve user names | |
-N, –symbolic | To resolve hardware names on the system | |
-e, –extend | To print any more details of the network | |
-p, –programs | To print PID/Process ID name for packets | |
-o, –timers | To print timers | |
-c, –continuous | To list the statistics continuously | |
-l, –listening | To print listening server sockets information | |
-a, –all | To print all sockets details | |
-F, –fib | To print Forwarding Information Base | |
-C, –cache | To print routing cache | |
-Z, –context | To print SELinux security information for sockets running on the system. |
Advantages of netstat command in Linux
Netstat command in Linux is advantageous because of the following reasons:
- It helps to print all the active network connections.
- Information regarding routing tables.
- Lists TCP and UDP Ports for the networks running on the system.
- It also lists out statistics of all packets like TCP, ICMP, UDP, and IP.
- Also helps in displaying kernel route information
- Displays Ipv4 and Ipv6 related information
Examples to Implement Linux netstat
Below are the examples mentioned :
Example #1. Option -a
To display all the current connections that are listing both listening and non-listening connections, we can use option a. The syntax for this option is given below.
Code:
netstat -a
Output:
Example #2. Option -au
To display all the connection statistics along with UDP ports, we can use the option. Syntax for this option is given below.
Code:
netstat -au
Output:
Example #3. Option -l
To display all the active list of listening port connections. We can use the option. The syntax for this option is given below.
Code:
netstat -l
Output:
Example #4. Option -lt
To display only the active listening TCP ports. The syntax for this option is given below.
Code:
netstat -lt
Output:
Example #5. Option -ant
By default, netstat prints both the hostname and IP Address in the connection when we do a reverse DNS lookup. To just display the IP address and cut off the hostname from the output as it might slow down the speed of fetching the output, we can use this option. The syntax for this option is given below.
Code:
netstat -ant
Output:
Example #6. Option -r
This option is used to display the kernel routing information. This is also similar to the output displayed for route command. The syntax for this option is given below.
Code:
netstat -r
Output:
Example #7. Option -i
Netstat command helps to display the information regarding the network interfaces. The syntax to get information on interfaces is given as below:
Code:
netstat -i
Output:
Example #8. Option -c
To fetch the details of TCP connection information continuously that are running on the system, we can use the option ‘c’. Syntax to use this option is given below.
Code:
netstat -c
Output:
Example #9. Option -g
To get information regarding IPv4 and IPv6, we can use this option. It also gives the multi cast group information. Syntax for this option is given below.
Code:
netstat -g
Output:
Example #10. Option -lx
Netstat command in Linux will help to display all the active UNIX port connections. We can list them out by using option ‘lx’. Syntax for this option is given below.
Code:
netstat -lx
Output:
Example #11. Option -s
To get the statistics reports of networks like TCP, ICMP, UDP and IP Protocols, we can use the option ‘s’ in netstat command. Syntax for this option is given below.
Code:
netstat -s
Output:
Example #12. Option –verbose
To search for a nonsupportive address in the system, netstat command is useful with –verbose option. We can use below syntax. To get the details of non-supportive addresses.
Code:
netstat --verbose
Output:
Example #13. Option -nlpt
To check if any HTTP server is running on the server and to get details of their process id/user id details, we can use this option.
Code:
netstat -nlpt
Output:
Conclusion
Netstat command in Linux is a very commonly used command-line tool by admins to monitor any network related issues. This command is a powerful tool in handling critical issues like network connection failover or to check any statistical reports over the network. Netstat is available in Linux, Unix, and Windows operating systems as well.
Recommended Articles
We hope that this EDUCBA information on “Linux netstat” was beneficial to you. You can view EDUCBA’s recommended articles for more information.