Linux netstat command

Updated: 11/06/2021 by Computer Hope
netstat command

On Unix-like operating systems, the netstat command prints information about network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

This page covers the Linux version of netstat.

Description

netstat ("network statistics") is a command-line tool that displays network connections (both incoming and outgoing), routing tables, and many network interface (network interface controller or software-defined network interface) and network protocol statistics. It is available on Unix-like operating systems, including OS X, Linux, Solaris, and BSD, and on Windows NT-based operating systems, including Windows XP, Windows Vista, Windows 7 and Windows 8.

It is used for finding problems in the network and to determine the amount of traffic on the network as a performance measurement.

Syntax

netstat [address_family_options] [--tcp|-t] [--udp|-u] [--raw|-w] 
        [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts] 
        [--numeric-ports] [--numeric-users] [--symbolic|-N] 
        [--extend|-e[--extend|-e]] [--timers|-o] [--program|-p] 
        [--verbose|-v] [--continuous|-c]
netstat {--route|-r} [address_family_options] [--extend|-e[--extend|-e]] 
        [--verbose|-v] [--numeric|-n] [--numeric-hosts] [--numeric-ports] 
        [--numeric-users] [--continuous|-c]
netstat {--interfaces|-i} [--all|-a] [--extend|-e[--extend|-e]] [--verbose|-v] 
        [--program|-p] [--numeric|-n] [--numeric-hosts] [--numeric-ports] 
        [--numeric-users] [--continuous|-c]
netstat {--groups|-g} [--numeric|-n] [--numeric-hosts] [--numeric-ports] 
        [--numeric-users] [--continuous|-c]
netstat {--masquerade|-M} [--extend|-e] [--numeric|-n] [--numeric-hosts] 
        [--numeric-ports] [--numeric-users] [--continuous|-c]
netstat {--statistics|-s} [--tcp|-t] [--udp|-u] [--raw|-w]
netstat {--version|-V}
netstat {--help|-h}

In the syntax above, address_family_options may be any combination of the following options:

[-4] [-6] [--protocol={inet,unix,ipx,ax25,netrom,ddp}[,...]] [--unix|-x] 
[--inet|--ip] [--ax25] [--ipx] [--netrom] [--ddp]

Options

The type of information printed by netstat is controlled by the first argument, which is one of the following:

(none) By default, netstat displays a list of open sockets. If you don't specify any address families, then the active sockets of all configured address families are printed.
--route, -r Display the kernel routing tables. See the description in route for details. netstat -r and route -e produce the same output.
--groups, -g Display multicast group membership information for IPv4 and IPv6.
--interfaces, -i Display a table of all network interfaces.
--masquerade, -M Display a list of masqueraded connections.
--statistics, -s Display summary statistics for each protocol.

After the first argument, the following options specify the reporting behavior of netstat:

--verbose, -v Tell the user what is going on by operating verbosely. Especially print some useful information about unconfigured address families.
--wide, -W Do not truncate IP addresses using output as wide as needed. This is optional for now to not break existing scripts.
--numeric, -n Show numerical addresses instead of trying to determine symbolic host, port or user names.
--numeric-hosts Shows numerical host addresses, but does not affect the resolution of port or user names.
--numeric-ports Shows numerical port numbers, but does not affect the resolution of host or user names.
--numeric-users Shows numerical user IDs, but does not affect the resolution of host or port names.
--protocol=family, -A Specifies the "address families" (low-level protocols) for which connections are to be shown. The family is a comma-separated list of address family keywords like inet, unix, ipx, ax25, netrom, and ddp. This has the same effect as using the --inet, --unix (-x), --ipx, --ax25, --netrom, and --ddp options.

The address family inet includes raw, udp, and tcp protocol sockets.
-c, --continuous Cause netstat to print the selected information every second continuously.
-e, --extend Display additional information. Use this option twice for maximum detail.
-o, --timers Include information related to networking timers.
-p, --program Show the PID (process identifier) and name of the program that each socket belongs.
-l, --listening Show only listening sockets. (These are omitted by default.)
-a, --all Show both listening and non-listening sockets. With the --interfaces option, show interfaces that are not up.
-F Print routing information from the FIB. (This is the default.)
-C Print routing information from the route cache.

Output: Internet connections

Information about Active Internet Connections (TCP, UDP, raw) falls under the following categories:

Proto The protocol (tcp, udp, raw) used by the socket.
Recv-Q The count of bytes not copied by the user program connected to this socket.
Send-Q The count of bytes not acknowledged by the remote host.
Local Address Address and port number of the local end of the socket. Unless the --numeric (-n) option is specified, the socket address is resolved to its FQDN (canonical hostname), and the port number is translated into the corresponding service name.
Foreign Address Address and port number of the remote end of the socket; analogous to "Local Address."
State The state of the socket. Since there are no states in raw mode and usually no states used in UDP, this column may be left blank. Normally this can be one of several values:

ESTABLISHED The socket has an established connection.
SYN_SENT The socket is actively attempting to establish a connection.
SYN_RECV A connection request was received from the network.
FIN_WAIT1 The socket is closed, and the connection is shutting down.
FIN_WAIT2 Connection is closed, and the socket is waiting for a shutdown from the remote end.
TIME_WAIT The socket is waiting after close to handle packets still in the network.
CLOSE The socket is not being used.
CLOSE_WAIT The remote end has shut down, waiting for the socket to close.
LAST_ACK The remote end has shut down, and the socket is closed. Waiting for acknowledgement.
LISTEN The socket is listening for incoming connections. Such sockets are not included in the output unless you specify the --listening (-l) or --all (-a) option.
CLOSING Both sockets are shut down, but we still don't have all our data sent.
UNKNOWN The state of the socket is unknown.
User The username or the user id (UID) of the owner of the socket.
PID/Program name Slash-separated pair of the process id (PID) and process name of the process that owns the socket. --program causes this column to be included. You need superuser privileges to see this information on sockets you don't own. This identification information is not yet available for IPX sockets.

Output: Unix domain sockets

Information about Active Unix Domain Sockets falls under the following categories:

Proto The protocol (usually unix) used by the socket.
RefCnt The reference count (i.e., attached processes via this socket).
Flags The flags displayed are SO_ACCEPTON (displayed as ACC), SO_WAITDATA (W) or SO_NOSPACE (N). SO_ACCECPTON is used on unconnected sockets if their corresponding processes are waiting for a connect request. The other flags are not of normal interest.
Type There are several types of socket access:

SOCK_DGRAM The socket is used in datagram (connectionless) mode.
SOCK_STREAM This is a stream (connection) socket.
SOCK_RAW The socket is used as a raw socket.
SOCK_RDM This one serves reliably-delivered messages.
SOCK_SEQPACKET This is a sequential packet socket.
SOCK_PACKET Raw interface access socket.
State This field contains one of the following keywords:

FREE The socket is not allocated.
LISTENING The socket is listening for a connection request. Such sockets are only included in the output if you specify the --listening (-l) or --all (-a) option.
CONNECTING The socket is about to establish a connection.
CONNECTED The socket is connected.
DISCONNECTING The socket is disconnecting.
(empty) The socket is not connected to another one.
PID/Program name Process ID (PID) and process name of the process with the socket open. More info available in Active Internet Connections section written above.
Path This is the path name as which the corresponding processes attached to the socket.
Active IPX sockets A list of active IPX sockets.
Active NET/ROM sockets A list of active NET/ROM sockets.
Active AX.25 sockets A list of active AX.25 sockets.

Files

netstat makes use of the following files:

/etc/services The services translation file.
/proc Mount point for the proc filesystem, which gives access to kernel status information as a file hierarchy.
/proc/net/dev Device information file.
/proc/net/raw Raw socket information.
/proc/net/tcp TCP socket information.
/proc/net/udp UDP socket information.
/proc/net/igmp IGMP multicast information.
/proc/net/unix Unix domain socket information.
/proc/net/ipx IPX socket information.
/proc/net/ax25 AX25 socket information.
/proc/net/appletalk DDP (appletalk) socket information.
/proc/net/nr NET/ROM socket information.
/proc/net/route IP routing information.
/proc/net/ax25_route AX25 routing information.
/proc/net/ipx_route IPX routing information.
/proc/net/nr_nodes NET/ROM nodelist.
/proc/net/nr_neigh NET/ROM neighbours.
/proc/net/ip_masquerade Masqueraded connections.
/proc/net/snmp Statistics.

Examples

netstat

Displays generic statistics about the network activity of the local system.

netstat -an

Shows information about all active connections to the server, including the source and destination IP addresses and ports, if you have proper permissions.

netstat -rn

Displays the routing table for all IP addresses bound to the server.

netstat -an |grep :80 | wc -l

Collects statistics about the amount of active connections on port 80, and pipes this data to the wc command, which displays the number of connections by counting the lines of the original netstat output.

netstat -natp

Display statistics about active Internet connections.

ac — Print statistics about the time users were connected.
arp — Manipulate the system ARP cache.
ifconfig — View or modify the configuration of network interfaces.
ip — Display and manipulate information about routing, devices, policy routing and tunnels.
route — Display and manipulate the IP routing table.
rpcinfo — Report information about remote procedure calls.