Netstat is a command-line tool used by system administrators to evaluate network configuration and activity. The term Netstat is results from network and statistics. It shows open ports on the host device and their corresponding addresses, the routing table, and masquerade connections.

The purpose of this post is to show you how to install and configure different Netstat commands in Linux.

Installing Net-tools on Linux:

Netstat is part of a package named net-tools. You can get the net-tools package on Ubuntu with the command:

$ sudo apt install net-tools

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage1 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in.png" data-lazy- height="214" src="data:image/svg xml,” width=”942″>

Check Version of Netstat:

Upon installation, check the installed version of Netstat:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage3 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-1.png" data-lazy- height="474" src="data:image/svg xml,” width=”835″>

Netstat command in Linux:

Display Routing table:

Netstat command shows the routing table detail on the terminal. If you wish to see the routing table, use the –nr flag with Netstat; it shows the kernel routing table in the same way that route does. Use the below command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-499.png605bece03b8f3.jpg" data-lazy- height="118" src="data:image/svg xml,” width=”847″>

Instead of using symbolic address names, the -nr option allows Netstat to print addresses divided by dots

Display interface statistics:

Using the ‘-i’ flag or option with Netstat will show statistics for the currently configured network interfaces.

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage5 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-2.png" data-lazy- height="126" src="data:image/svg xml,” width=”838″>

If the “–a” flag is also used with “-i”, the command prints all of the kernel interfaces.

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage4 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-3.png" data-lazy- height="142" src="data:image/svg xml,” width=”843″>

Display Network connection:

To view active or passive sockets, Netstat has a range of options. Active TCP, UDP, RAW, and Unix socket connections are specified by the –t, –u, –w, and –x options, respectively.

Type on your terminal:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage7 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-4.png" data-lazy- height="334" src="data:image/svg xml,” width=”843″>

Display Network Services:

Run the following command to see a list of networks, their current states, and their associated ports:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage6 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-5.png" data-lazy- height="24" src="data:image/svg xml,” width=”850″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-500.png605bece2b4618.jpg" data-lazy- height="460" src="data:image/svg xml,” width=”839″>

Display all the listening port of TCP and UDP connection:

You can see all TCP and UDP ports by using the command:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage8 fial.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-6.png" data-lazy- height="592" src="data:image/svg xml,” width=”948″>

Display TCP Port Connection:

To get the list of only TCP (Transmission Control Protocols) port connection, use the following command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-501.png605bece3f1877.jpg" data-lazy- height="309" src="data:image/svg xml,” width=”836″>

Display UDP Port Connection:

To see UDP (User Diagrams Protocols) port connection, use the command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-502.png605bece487b8e.jpg" data-lazy- height="232" src="data:image/svg xml,” width=”830″>

Display all Listening Connection:

List all the active connection by using the “-l” flag with Netstat:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-503.png" data-lazy- height="501" src="data:image/svg xml,” width=”841″>

Display all TCP Listening port:

We can get the list of all active listening TCP ports by using the –‘lt’ flag as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-504.png" data-lazy- height="242" src="data:image/svg xml,” width=”828″>

Display all UDP listening port:

We can get the list of all active listening UDP ports by using ‘–lu’ option:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-505.png" data-lazy- height="228" src="data:image/svg xml,” width=”828″>

Display all Unix Listening port:

Display the list of all active listening UNIX ports by using –lx:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-506.png" data-lazy- height="320" src="data:image/svg xml,” width=”831″>

Showing Statistics by Protocol:

Displays protocol-specific statistics. The TCP, UDP, ICMP, and IP protocol statistics are displayed by default. A set of protocols can be defined using the -s option:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage15 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-7.png" data-lazy- height="522" src="data:image/svg xml,” width=”830″>

Showing Statistics by TCP Protocol:

Display the list of only TCP protocols by using the ‘–st’ option with Netstat:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/word-image-507.png" data-lazy- height="514" src="data:image/svg xml,” width=”826″>

Showing Statistics by UDP Protocol

Display the list of only UDP protocols by using the ‘–su’ option with Netstat:

<img alt="D:AqsaNestat Command in LinuxNestat Command in Linuximagesimage17 final.png" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/03/echo/d-aqsa-nestat-command-in-linux-nestat-command-in-8.png" data-lazy- height="499" src="data:image/svg xml,” width=”832″>

Conclusion:

Netstat is a valuable method for tracking network activity and configuration. It is, in reality, a jumbled-up collection of several resources. In this post, we have demonstrated that how to manage network connections using the Netstat command.

About the author

<img alt="Avatar" data-lazy-src="https://secure.gravatar.com/avatar/8f95e977d73b699bef395de0f3578272?s=112&r=g" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Maqbool

As a Software engineer, I am passionate to write about various IT related


articles but have deep interest in Linux. I spend most of my time reading Linux related blogs and IT related books. I want to serve the world with my writing skills.