netstat command in Linux 1

How to Use netstat Command in Linux

Employing netstat command in Linux is indeed a valuable method for tracking and managing network connections. You can effortlessly run the command and evaluate the network, display open ports, addresses, routing tables, and more.

With netstat, we’re looking at a command-line tool. Coming out as a combining result of two words, network and statistics, the netstat command is usually used by system administrators for assessing the network configuration in the most promising manner. 

Besides accessing and revealing the network status of your system, the netstat commands in Linux can display the ports that are open on the respective host device. Also, the process of keeping the network connections under tracking is made seamless with this command.

Installing Net-tools to Use netstat Command in Linux

The netstat utility is a part of a tool known as net-tools. So in order to use the netstat commands in Linux, getting the net-tools installed is evident. Thankfully, the process is pretty straightforward. However, it is vital to keep in mind that you’ll have to have sudo privileges for getting net-tools on Ubuntu.

Launch the Terminal hitting the “Ctrl+Alt+T” key combination.

Run the following command:

$ sudo apt install net-tools
install netstat command in Linux

Using netstat Command in Linux

Once you have the net-tools installed on your system, you’re pretty much ready to use netstat commands and seamlessly evaluate the network configuration and its various activities.

netstat Command in Linux for Displaying Routing Table

netstat command in Linux is a great utility to display the details of the routing table. All you need to do is use the -nr flag and ask the system to come up with the kernel routing table. It displays the details divided by dots instead of using the general symbolic addresses’ names.

$ netstat -nr
netstat command in linux routing table

Using netstat Command in Linux for Display Network Connection

In case you want to view the active or passive sockets, the netstat commands in Linux are of great utility. The various socket connections, including the TCP, UDP, RAW, and Unix, are specified by the respective flags –t, –u, –w, and –x.

To make your system display the network connection, open the Terminal and run the following command:

$ netstat -ta
netstat command in Linux network connection

Displaying Network Statistics with netstat command in Linux

Simply inputting the -i flag alongside the netstat command in Linux will display the detailed statistics of the network interfaces that are currently configured.

You can also use the “–a” flag together with “-i” for printing all the kernel interfaces.

$ netstat -i
netstat command in Linux
$ netstat -ai
using netstat command in Linux

netstat Command in Linux for Displaying Network Services

Do you need to see the list of networks quite too often? Well then, the approach is straightforward. Use the netstat command with -pnltu flag. The command also helps in gathering details about the network’s current states and also the associated ports.

$ netstat -pnltu
Display network services using netstat command

Displaying Statistics by Protocol

Employing netstat command in Linux is a great way to display statistics by specific protocols. By default, the command prints TCP, ICMP, UDP, and IP protocol statistics. 

$ netstat -s
netstat command for displaying statistics

Displaying UDP Protocol Statistics

Run the following command and instruct your system to show the statistics by the UDP protocol.

$ netstat -su
netstat command in Linux UDP protocol

Displaying TCP Protocol Statistics

Pretty similar to the UDP protocol statistics, you can use the netstat command and display the statistics by TCP protocol. Simply run the following command:

$ netstat -st
TCP protocol statistics command in Linux

Displaying All Listening Connection

Those who are looking to have their system print details about all the active listening connections needs to use the netstat command with the -l flag.

$ netstat -l
listening connection

Using netstat Command in Linux to Display Unix Listening Port

All the active Unix listening ports in your system can be easily displayed using the netstat command followed by the –lx flag.

$ netstat -lx
listening port

Similarly, you can use the -lt and -lu flags to display the TCP and UDP listening ports, respectively.

$  netstat -lt
$ netstat -lu

Using netstat Command in Linux to Display All the Listening Port of TCP and UDP Connection

You can easily see all the listening ports associated with the TCP and UDP connection by running the following command:

$ netstat -a | more
displaying listening port of TCP and UDP Connection

Display TCP Port Connection

If you only need the list of port connections that are associated with Transmission Control Protocols or TCP, run the following command:

$ netstat -at
display TCP port connection in Linux

Display UDP Port Connection

In case it is the list of port connections associated with User Datagram Protocol, or UDP is what you’re after, open the Terminal and run the following command:

$ netstat -au

Other Uses of netstat Command in Linux:

-ie: lists all active network interfaces

#netstat - antc: Monitors all TCP connections

-u: Monitors all UDP connections

#netstat-atep | grep | ssh: It displays SSH connections together with username, PID, and program.

-s -w: Displays all the raw network statistics

#netstat-atnep | grep 443: Running it will display the connections on port 443 together with username, PID, and program.

Checking the Version of netstat Command in Linux

At times, it might be necessary for you to learn about the version of the netstat command that your system is currently using. Run the following command, and check the installed version.

$ netstat -v

And with that, we’re done talking about the netstat commands in Linux. Walking through this post will make you learn the best ways to use the netstat command alongside various flags and manage network connections. For queries, don’t hesitate to leave a comment below.

If this guide helped you, please share it.

Leave a Reply
Related Posts