How to Use netstat on Linux

July 2024 · 15 minute read

Quick Links

Key Takeaways

The Linux netstat command gives you a treasure-trove of information about your network connections, the ports that are in use, and the processes using them. Learn how to use it.

Ports, Processes, and Protocols

Network sockets can either be connected or waiting for a connection. The connections use networking protocols like Transport Control Protocol (TCP) or User Datagram Protocol UDP. They use Internet Protocol addresses and network ports to establish connections.

The word sockets might conjure up images of a physical connection point for a lead or cable, but in this context, a socket is a software construct used to handle one end of a network data connection.

Sockets have two main states: They are either connected and facilitating an ongoing network communication, or they are waiting for an incoming connection to connect to them. There are other states, such as the state when a socket is midway through establishing a connection on a remote device, but putting transient states aside, you can think of a socket as either being connected or waiting (which is often called listening).

The listening socket is called the server, and the socket that requests a connection with the listening socket is called a client. These names have nothing to do with hardware or computer roles. They simply define the role of each socket at each end of the connection.

The netstat command lets you discover which sockets are connected and which sockets are listening. Meaning, it tells you which ports are in use and which processes are using them. It can show you routing tables and statistics about your network interfaces and multicast connections.

The functionality of netstat has been replicated over time in different Linux utilities, such as ip and ss. It's still worth knowing this granddaddy of all network analysis commands, because it is available on all Linux and Unix-like operating systems, and even on Windows and Mac.

Here's how to use it, complete with example commands.

Listing All Sockets with netstat

The -a (all) option makes netstat show all the connected and waiting sockets. This command is liable to produce a long listing, so we pipe it into less.

netstat -a | less

The listing includes TCP (IP), TCP6 (IPv6), and UDP sockets.

The wrap-around in the terminal window makes it a little difficult to see what is going on. Here's a couple of sections from that listing:

Active Internet connections (servers and established)

Proto

Recv-Q

Send-Q

Local Address

Foreign Address

State

TCP

0

0

localhost: domain

0.0.0.0:*

LISTEN

TCP

0

0

0.0.0.0:ssh

0.0.0.0:*

LISTEN

TCP

0

0

localhost:ipp

0.0.0.0:*

LISTEN

TCP

0

0

localhost:smtp

0.0.0.0:*

LISTEN

TCP6

0

0

[::]:ssh

[::]:*

LISTEN

TCP6

0

0

ip6-localhost:ipp

[::]:*

LISTEN

Active UNIX domain sockets (servers and established)

Proto

RefCnt

Flags

Type

State

I-Node

Path

unix

24

[ ]

DGRAM

12831

/run/systemd/journal/dev-log

unix

2

[ ACC ]

STREAM

LISTENING

24747

@/tmp/dbus-zH6clYmvw8

unix

2

[ ]

DGRAM

26372

/run/user/1000/system/notify

unix

2

[ ]

DGRAM

23382

/run/user/121/system/notify

unix

2

[ ACC ]

SEQPACKET

LISTENING

12839

/run/udev/control

The "Active Internet" section lists the connected external connections and local sockets listening for remote connection requests. That is, it lists the network connections that are (or will be) established to external devices.

The "UNIX domain" section lists the connected and listening internal connections. In other words, it lists the connections that have been established within your computer between different applications, processes, and elements of the operating system.

The "Active Internet" columns are:

For TCP connections, the state value can be one of the following :

The "Unix domain" columns are:

The Unix domain socket type can be one of the following:

The Unix domain socket state can be one of the following:

Wow, that's a lot of information! Many of the netstat options refine the results in one way or another, but they don't change the content too much. Let's take a look.

Listing Sockets by Type Using the netstat Command

The netstat -a command can provide more information than you need to see. If you only want or need to see the TCP sockets, you can use the -t (TCP) option to restrict the display to only show TCP sockets.

netstat -at | less

The display out is greatly reduced. The few sockets that are listed are all TCP sockets.

The -u (UDP) and -x (UNIX) options behave in a similar way, restricting the results to the type of socket specified on the command line. Here's the -u (UDP) option in use:

netstat -au | less

Only UDP sockets are listed.

Listing Sockets by State

To see the sockets that are in the listening or waiting state, use the -l (listening) option.

netstat -l | less

The sockets that are listed are those that are in the listening state.

This can be combined with the -t (TCP, -u (UDP) and -x (UNIX) options to further home in on the sockets of interest. Let's look for listening TCP sockets:

netstat -lt | less

Now, we see only TCP listening sockets.

Network Statistics by Protocol

To see statistics for a protocol, use the -s (statistics) option and pass in the -t (TCP), -u (UDP), or -x (UNIX) options. If you just use the -s (statistics) option on its own, you'll see statistics for all protocols. Let's check the statistics for the TCP protocol.

netstat -st | less

A collection of statistics for the TCP connections is displayed in less.

Showing Process Names and PIDs

It can be useful to see the process ID (PID) of the process using a socket, together with the name of that process. The -p (program) option does just that. Let's see what the PIDs and process names are for the processes using a TCP socket that is in the listening state. We use sudo to make sure we receive all of the information that is available, including any information that would normally require root permissions.

sudo netstat -p -at

Here's that output in a formatted table:

Active Internet connections (servers and established)

Proto

Recv-Q

Send-Q

Local Address

Foreign Address

State

PID/Program Name

tcp

0

0

localhost: domain

0.0.0.0:*

LISTEN

6927/systemd-resolv

tcp

0

0

0.0.0.0:ssh

0.0.0.0:*

LISTEN

751/sshd

tcp

0

0

localhost:ipp

0.0.0.0:*

LISTEN

7687/cupsd

tcp

0

0

localhost:smtp

0.0.0.0:*

LISTEN

1176/master

tcp6

0

0

[::]:ssh

[::]:*

LISTEN

751/sshd

tcp6

0

0

ip6-localhost:ipp

[::]:*

LISTEN

7687/cupsd

tcp6

0

0

ip6-localhost:smtp

[::]:*

LISTEN

1176/master

We've got an extra column called "PID/program name." This column lists the PID and name of the process using each of the sockets.

Listing Numeric Addresses

Another step we can take to remove some ambiguity is to display the local and remote addresses as IP addresses instead of their resolved domain and hostnames. If we use the -n (numeric) option, the IPv4 addresses are shown in dotted-decimal format:

sudo netstat -an | less

The IP addresses are shown as numeric values. The port numbers are also shown, separated by a colon " : " from the IP Address.

An IP address of 127.0.0.1 shows that the socket is bound to the loopback address of the local computer. You can think of an IP address of 0.0.0.0 as meaning the "default route" for local addresses, and "any IP address" for foreign addresses. IPv6 addresses shown as "::" are also all zero addresses.

The ports that are listed can be easily checked to see what their usual purpose is:

Displaying the Routing Table

The -r (route) option displays the kernel routing table.

sudo netstat -r

Here's that output in a neat table:

Kernel IP routing table

Destination

Gateway

Genmask

Flags

MSS

Window

irtt

Iface

default

Vigor.router

0.0.0.0

UG

0

0

0

enp0s3

link-local

0.0.0.0

255.255.0.0

U

0

0

0

enp0s3

192.168.4.0

0.0.0.0

255.255.255.0

U

0

0

0

enp0s3

And, here's what the columns mean:

The flags value can be one of:

Finding the Port Used by a Process

If we pipe the output of netstat through grep, we can search for a process by name and identify the port it is using. We use the -a (all), -n (numeric) and -p (program) options used previously, and search for "sshd."

sudo netstat -anp | grep "sshd"

grep finds the target string, and we see that the sshd daemon is using port 22.

Of course, we can also do this in reverse. If we search for ":22", we can find out which process is using that port, if any.

sudo netstat -anp | grep ":22"

This time grep finds the ":22" target string, and we see that the process using this port is the sshd daemon, process ID 751.

List the Network Interfaces

The -i (interfaces) option will display a table of the network interfaces that netstat can discover.

sudo netstat -i

Here's the output in a more legible fashion:

Kernel Interface table

Iface

MTU

RX-OK

RX-ERR

RX-DRP

RX-OVR

TX-OK

TX-ERR

TX-DRP

TX-OVR

Flg

enp0s3

1500

4520671

0

0

0

4779773

0

0

0

BMRU

lo

65536

30175

0

0

0

30175

0

0

0

LRU

This is what the columns mean:

The flags represent the following:

List Multicast Group Memberships

Simply put, a multicast transmission enables a packet to be sent only once, regardless of the number of recipients. For services such as video streaming, for example, this increases the efficiency from the sender's point of view by a tremendous amount.

The -g (groups) option makes netstat list the multicast group membership of sockets on each interface.

sudo netstat -g

The columns are quite simple:

The New Kids on the Block

The route, ip, ifconfig, and ss commands can provide a lot of what netstat is capable of showing you. They're all great commands and worth checking out.

We've focused on netstat because it is universally available, regardless of which Unix-like operating system you're working on, even the obscure ones.

Linux Commands

Files

tar·pv·cat·tac·chmod·grep ·diff·sed·ar·man·pushd·popd·fsck·testdisk·seq·fd·pandoc·cd·$PATH·awk·join·jq·fold·uniq·journalctl·tail·stat·ls·fstab·echo·less·chgrp·chown·rev·look·strings·type·rename·zip·unzip·mount·umount·install·fdisk·mkfs·rm·rmdir·rsync·df·gpg·vi·nano·mkdir·du·ln·patch·convert·rclone·shred·srm·scp·gzip·chattr·cut·find·umask·wc· tr

Processes

alias·screen·top·nice·renice·progress·strace·systemd·tmux·chsh·history·at·batch·free·which·dmesg·chfn·usermod·ps·chroot·xargs·tty·pinky·lsof·vmstat·timeout·wall·yes·kill·sleep·sudo·su·time·groupadd·usermod·groups·lshw·shutdown·reboot·halt·poweroff·passwd·lscpu·crontab·date·bg·fg·pidof·nohup·pmap

Networking

netstat·ping·traceroute·ip·ss·whois·fail2ban·bmon·dig·finger·nmap·ftp·curl·wget·who·whoami·w·iptables·ssh-keygen·ufw·arping·firewalld

ncG1vNJzZmivp6x7qbvWraagnZWge6S7zGhsamtgZYBwtM6wZK2nXarApnnNnqusrJGperC6jKWgp62oZA%3D%3D