When debugging networks problems on a Linux server, ping and traceroute are often helpful, but you may need to have further network details on hand to help track down an issue and get it fixed. One such command is netstat, which can offer you details on the networks sockets as well as other helpful information. As with ping and traceroute, you can simply use netstat from the command line and get results quickly.
What is Netstat?
The netstat command in Linux is a very useful tool when dealing with networking issues.
Source: IBM developerWorks
Netstat, short for the phrase “network statistics”, is a tool Linux (as well as other operating systems such as Windows and OS X) can make use of in order to display incoming and outgoing network connections. In addition to this, it can be used to get information on network statistics, protocol statistics, and routing tables.
You can use netstat to find network problems and measure the amount of network traffic, so it can be a really useful tool to help you gather the information you need to solve any outage, slow down, or bottleneck issues on your network.
For a basic listing of all the current connections, you would simply call netstat with the -a option.
> netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp *:* LISTEN tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN udp 0 0 *:bootpc *:* udp 0 0 localhost:ntp *:* udp 0 0 *:ntp *:* udp6 0 0 ip6-localhost:ntp [::]:* udp6 0 0 [::]:ntp [::]:* udp6 0 0 [::]:mdns [::]:* Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 1 [ ACC ] STREAM LISTENING 11091 @/tmp/XX unix 1 [ ACC ] STREAM LISTENING 39045 @/tmp/Cx
This provides some basic information on connections from different types of protocols like TCP and UDP, as well as active Unix domain sockets. However, netstat allows you to get more specific information that can be more helpful in debugging.
Filter by Connection Type
Sometimes filtering the results based on the connection type can be useful when trying to find the information you need. For example, if you want to see only the TCP connections, you can add the “t” option in addition to the “a” option, as shown below:
> netstat -at Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 host:domain *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 host.local:45789 host-:http ESTABLISHED
Similarly, by using netstat -au, you can list only the UDP connections.
Filter by Listening Connections
If you want to only see the connections that are listening, you can do so by using the “l” option and remove the “a” option. Here is an example of this:
> netstat -l Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:80 0.0.0.0:* LISTEN tcp 0 0 localhost:443 0.0.0.0:* LISTEN
As with the “a” option, you can use netstat -lt and netstat -lu in order to further filter and to get only the listening TCP or UDP connections. In this way, you can easily see if a particular port is open and listening and determine whether a website or app is able to be up and running as expected.
See Network Statistics
> netstat -s Ip: 73419 total packets received 0 forwarded 0 incoming packets discarded 73419 incoming packets delivered 37098 requests sent out 45 outgoing packets dropped Icmp: 119 ICMP messages received 0 input ICMP message failed. ICMP input histogram: destination unreachable: 119 102 ICMP messages sent 0 ICMP messages failed ICMP output histogram: destination unreachable: 102 ... OUTPUT TRUNCATED ...
As you can see, this offers some statistics that may be useful to you while debugging, such as total, incoming, and outgoing packets as well as ICMP messages that were received, sent, and failed.