netstat is a command-line tool used to monitor network statistics. It enables you to view network data such as the ports in use, active connections, packets that have been transferred, etc.

It’s interesting to note that this very utility is available on Windows Server editions and is very similar in usage to that on Linux.

Follow through this article to see how you can use the netstat command on your Linux system.

Structuring netstat Commands

There are two ways in which you can use the netstat command: either run the command alone or run it with options. If you choose to run netstat without any options indicated, it will display all active connections on your system.

        netstat
    
netstat command in linux

When running netstat with options, you need to specify a hyphen (-) before each flag.

        netstat [-option1] [-option2] …
    

netstat has a number of flags, and in the next section, you'll see some examples in use.

If you have many options to include in your command, it can be disturbing to add a hyphen every time. Fortunately, netstat provides a simpler way of doing so.

You can simply use one hyphen and then consecutively list your options without any spaces between them. For example, instead of typing netstat -p -n -t, you can simply run netstat -pnt.

You’ll find this summative format very convenient when you have to run multiple netstat commands with many options:

        netstat -[option1][option2][option3]...
    

Related: Why You Should Use Linux Networking Tool in Windows With WSL

netstat Commands You Should Know Of

If you would like to know the statistics for the UDP, TCP, ICMP, and IP protocols, use the following command:

        netstat -s
    

To list all TCP (Transmission Control Protocol) connections, run the command below:

        netstat -at
    

Similarly, for UDP connections, issue the following command:

        netstat -au
    

To list all servers/ports that are actively running (listening) on your system, run:

        netstat -plnt
    

It’s important to note that this command may require sudo privileges. This is because the root user may be the owner of some of the services.

This particular command is very important as it even shows you the program IDs of the running servers. You can use this information to stop servers that you don’t want running on your system.

ss: A Successor to the netstat Command

Although netstat is a good tool for monitoring network connections, it’s not as detailed as the ss utility. You can use the ss command to monitor network sockets on Linux. It's faster than netstat and gives more detailed information.