Netstat command in Linux operating system – Explained with examples

In today’s article we will go through about the netstat command in Linux operating system. To determine which computers or networks a PC is linked to, we use netstat, which offers statistics about all active connections. The network utility is conveniently usable via the command line on even on Windows & macOS apart from the Linux.

If you want some guidance to be familiar with other Linux commands you can refer to other posts related to the linux commands via the Cheat Sheet

What is netstat ?

The name Netstat, which combines the words “network” and “statistics,” refers to a program that runs under command line instructions. It provides users with basic information about all network activity, including which ports and addresses the associated TCP and UDP connections are using, as well as which ports are available for tasks.

The first version of the Berkeley Software Distribution (BSD), a Unix derivative, enabled the TCP/IP Internet protocol family in version 4.2 when netstat was first added in 1983. Since its introduction in Linux in 1991, netstat has been a part of the operating system. It was first included in Windows in version 3.11 (1993), and with the aid of extensions, it was also able to interact via TCP/IP.

Why netstat ?

Gaining an understanding of your computer’s or server’s inbound and outgoing connections will greatly help you combat harmful software and excessive traffic. The corresponding network address, which among other things indicates which port was previously opened for data exchange, is used to establish these connections.

The main issue with these open ports is that they give outside parties the chance to infiltrate malware into your computer. Furthermore, it’s possible that an existing Trojan on your system will create a “backdoor” and open a related port in the process. You should so routinely verify the ports that your system has opened. Netstat is a great tool for this.

How Netstat works ?

Using the netstat commands we can displays protocol statistics and network status. Routing table data, interface data, and the status of TCP and UDP endpoints can all be shown in tabular form.

Netstat shows different kinds of network information based on the command line option that is chosen. For system administration, these are the most beneficial displays. This form’s syntax is as follows:

netstat [-m] [-n] [-s] [-i | -r] [-f address_family]

The most frequently used options for determining network status are: sr, and i.

Examples using CMD line

Let us go through few examples of netstat commands and their expected outputs.

Listing all the ports (TCP & UDP)

To List all the port (TCP & UDP) use the command “netstat -a

Listing only TCP/UDP port connection

Listing all Listening connections

If you want to list only the listening connections associated with TCP you can run : netstat -lt

While for listing the Listening connections with UDP you can use : netstat -lu

Listing the UNIX listening ports

Listing services names with their PID

The above will display the services running along with their PID which can be helpful during troubleshooting issues related to application. Please note that you need to have the root privileges to list all the process.

Kernel IP routing

This is quite a popular commands to check the routing table for the system

Checking the Open ports for programs

If you want to check how many listening programs is running on the ports you can do so with below commands

Checking network Statistics

Conclusion

In this post we covered the major netstat command in Linux operating system. It’s a kind of unique utility that lets you check how your machine is connected to the other servers in the network. It functions similarly to a window providing you with data pathways, connections, and technical specifics. This information aids users in troubleshooting network problems and improving their comprehension of their system’s online activity. To learn more about the netstat command and its options, use the man netstat command or consult the manual documents netstat(8) – Linux manual page

Leave a comment