May 8, 2024 4 min read

netstat command in Linux with Examples

In this tutorial, we will explain how to use the netstat command, which is used to display network connections and statistics in Linux.

netstat command in Linux
netstat command in Linux
Table of Contents

Introduction

Before we discuss netstat command in Linux, let's briefly understand - What is netstat command ?

The netstat command is a useful tool in computer networking that displays network connections and network statistics. It provides valuable information such as active connections, open ports, routing tables, and more.

By using netstat, users can troubleshoot network issues, identify network connections, and monitor network traffic efficiently. Understanding how to interpret the output of the netstat command can help in diagnosing connectivity problems and optimizing network performance.

In this tutorial, you will understand netstat command in Linux. We will also address a few FAQs on netstat command in Linux.

Basic Syntax of netstat Command in Linux

The netstat command's general syntax is shown below:

netstat [options]

Let's look at a few of the most well-liked options and give instances:

Some Practical Examples of netstat Commands in Linux:

1) Show Both Listening and Non-listening Sockets Using netstat Command in Linux

-a, -all : Show sockets that are both listening and non-listening. Show interfaces that are not up by using the –interfaces option.

netstat -a | more 

2) List All TCP Ports Using netstat Command in Linux

This command provides you with a detailed list of all TCP ports and the TCP connections that your system is using.

netstat -at

3) List All UDP Ports Using netstat Command in Linux

This command, like the previous example, reveals information about UDP connections by concentrating on UDP ports.

netstat -au

4) List Only Listening Ports Using netstat Command in Linux

This option allows you to view only the ports that are open and waiting for connections to arrive.

netstat -l

Simply listing the ports that are open.

5) List Only Listening TCP Ports Using netstat Command in Linux

To be even more precise, this command lists the TCP ports that are open and available for use.

netstat -lt

To only display the TCP ports that are open.

6) List Only Listening UDP Ports Using netstat Command in Linux

In a similar vein, the purpose of this command is to show only the UDP ports that are in use.

netstat -lu

To only display the UDP ports that are open.

7) List Only Listening UNIX Ports Using netstat Command in Linux

This option only displays UNIX ports that are in a listening state for users of UNIX systems.

netstat -lx

To list just the UNIX ports that are open.

8) List Statistics for All Ports Using netstat Command in Linux

With the help of this command, you can get statistical data for every port and gain insight into network activity.

netstat -s 

To compile a list of all ports' statistics.

9) List Statistics for TCP Ports Using netstat Command in Linux

This command provides a more detailed breakdown by only displaying statistics for TCP ports.

netstat -st

To provide a list of TCP port statistics.

10) List Statistics for UDP Ports Using netstat Command in Linux

In the same way, this command concentrates on UDP port statistics.

netstat -su

Provide a summary of UDP port statistics.

11) Display PID and Program Names Using netstat Command in Linux

By showing the Process ID (PID) and program names linked to network connections, this option enhances the output.

netstat -pt

To show the program names and PID.

12) Print Netstat Information Continuously Using netstat Command in Linux

When this command is run, netstat data is continuously printed and updated to give real-time insights.

netstat -c

To consistently print the netstat data.

13) Get Non-supportive Address Families Using netstat Command in Linux

Use this command for a thorough summary of the system's non-supportive address families.

netstat --verbose

To enter the system and obtain the non-supportive Address families.

We have something similar to this at the conclusion.

14) Get Kernel Routing Information Using netstat Command in Linux

This command displays Destination Addresses, Gateways, and Interface details after retrieving kernel routing information.

netstat -r

In order to obtain the kernel routing data.

15) Get Port on Which a Program is Running Using netstat Command in Linux

Use this command to determine the port that a particular program—in this case, SSH—is running on.

netstat -ap | grep ssh

To find the port that a software is operating on.

16) Identify Process Using a Particular Port Using netstat Command in Linux

This command aids in locating the process linked to a specific port, in this case port 80.

netstat -an | grep ':80'

To obtain the process that is utilizing the specified port.

17) Get List of Network Interfaces Using netstat Command in Linux

To get a list of network interfaces with information about the activities of each interface, use this command.

netstat -i

To obtain the network interfaces list

18) Display Extended Information on Interfaces Using netstat Command in Linux

Use this option to obtain detailed information about interfaces in a format similar to what the ifconfig command produces.

In order to show more details on the interfaces.

FAQs on netstat command in Linux

How can I list all open ports using netstat?

Use the command netstat -tuln to display all open ports along with their associated services and the corresponding process IDs.

Can netstat show active network connections?

Yes, netstat can show active network connections by using the command netstat -nat

Is it possible to resolve IP addresses to hostnames with netstat?

Yes, you can resolve IP addresses to hostnames by adding the -n flag to the netstat command to prevent hostname resolution.

Can netstat display real-time network statistics?

No, netstat cannot display real-time network statistics. Consider using tools like iftop for real-time monitoring.

How can I identify listening ports using netstat?

To identify listening ports, run the command netstat -tuln | grep LISTEN.

Does netstat help in troubleshooting network connectivity issues?

Yes, netstat can help troubleshoot network issues by providing information on active connections, routing tables, and more.

How do I display the program name associated with each network connection using netstat?

To display the program name, add the -p flag to the netstat command.

Conclusion

We hope this tutorial helped you understand how to use netstat command in Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.