close
The Wayback Machine - https://web.archive.org/web/20240808073940/https://www.geeksforgeeks.org/netstat-command-linux/
Open In App

Netstat command in Linux

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

The netstat command is like a special tool in Linux that helps you understand and check things about how your computer connects to the internet. It can tell you about the connections your computer is making, the paths it uses to send information, and even some technical details like how many packets of data are being sent or received. In simple terms, it’s like a window that shows you what’s happening with your computer and the internet. This article will help you learn how to use netstat, exploring different ways to get specific information and giving you a better idea of what’s going on behind the scenes.

Overview of Netstat Command in Linux

`netstat` stands for network statistics. It allows users to display network-related information and diagnose various networking issues. The command has several options that can be combined to retrieve specific details.

Basic Syntax of `netstat`Command in Linux

Below is the general syntax of the netstat command:

netstat [options]

Let’s explore some of the most commonly used options along with examples:

Some Practical Examples of netstat Commands in Linux:

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

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

netstat -a | more 




BERJAYABERJAYA

2) List All TCP Ports Using netstat Command in Linux

This command specifically lists all TCP ports, giving you information about the TCP connections your system is engaged in.

netstat -at



BERJAYA

3) List All UDP Ports Using netstat Command in Linux

Similar to the previous example, this command focuses on UDP ports, revealing details about UDP connections.

netstat -au




BERJAYA

4) List Only Listening Ports Using netstat Command in Linux

By using this option, you can see only the ports that are actively listening for incoming connections

netstat -l



To list only the listening ports.

BERJAYA

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

Narrowing it down further, this command specifically lists the TCP ports that are in a listening state.

netstat -lt




To list only the listening tcp ports.

BERJAYA

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

Similarly, this command focuses on displaying only the UDP ports that are actively listening.

netstat -lu




To list only the listening udp ports.

BERJAYA

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

For those working with UNIX systems, this option shows only the UNIX ports that are in a listening state.

netstat -lx




To list only the listening UNIX ports.

BERJAYA

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

This command provides statistical information for all ports, offering insights into network activity.

netstat -s 




To list the statistics for all ports.

BERJAYA

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

For a more specific breakdown, this command displays statistics exclusively for TCP ports.

netstat -st




To list the statistics for TCP ports.

BERJAYA

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

Similarly, this command focuses on the statistical information related to UDP ports.

netstat -su




List the statistics for UDP ports.

BERJAYA

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

This option enriches the output by displaying Process ID (PID) and program names associated with network connections.

netstat -pt




To display the PID and program names.

BERJAYA

12) Print Netstat Information Continuously Using netstat Command in Linux

Executing this command continuously prints netstat information, updating at regular intervals to provide real-time insights.

netstat -c




To print the netstat information continuously.

BERJAYA

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

To identify non-supportive address families on the system, use this command for a detailed overview.

netstat --verbose




To get the non-supportive address families in the system.

BERJAYA

At the end, we have something like this.

BERJAYA

14) Get Kernel Routing Information Using netstat Command in Linux

This command retrieves kernel routing information, displaying destination addresses, gateways, and interface details.

netstat -r




To get the kernel routing information.

BERJAYA

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

To find the port on which a specific program, in this case, SSH, is running, use this command.

netstat -ap | grep ssh




To get the port on which a program is running.

BERJAYA

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

This command helps identify the process associated with a given port, such as port 80 in this example.

netstat -an | grep ':80'




To get the process which is using the given port.

BERJAYA

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

Use this command to obtain a list of network interfaces, providing details about each interface’s activities.

netstat -i




To get the list of network interfaces.

BERJAYA

Display Extended Information on Interfaces Using netstat Command in Linux

For extended information on interfaces, similar to the output of the ifconfig command, use this option to gain comprehensive insights.

netstat -ie




To display extended information on the interfaces

BERJAYA

Netstat command in Linux – FAQs

What is the netstat command in Linux used for?

The netstat command in Linux is a powerful networking tool used to display a variety of information related to network connections, routing tables, interface statistics, and more. It helps users diagnose network issues and gain insights into the current state of network activities on a system.

How do I view all active connections using netstat?

To view all active connections using netstat, you can use the following command:

netstat -a

This command displays both listening and non-listening sockets, providing a comprehensive list of active network connections.

Can netstat show the processes associated with network connections?

Yes, netstat can show the processes associated with network connections. By using the `-p` option, you can include the Process ID (PID) and program names in the output. For example:

netstat -p

This command will display the processes along with their PIDs that are using network resources.

How do I monitor network activity in real-time with netstat?

To monitor network activity in real-time using netstat, you can use the `-c` option. This option continuously updates the netstat information at regular intervals.

For example:

netstat -c

Executing this command will provide ongoing updates on network statistics.

What is the difference between netstat and ss command in Linux?

The netstat and ss commands in Linux both provide information about network connections, but they differ in terms of features and efficiency. ss is considered a more modern and efficient replacement for netstat. It provides faster output, supports more filters, and is generally recommended for users who want a more streamlined and up-to-date tool for viewing network statistics. However, netstat remains widely used and available on many systems.

How to show listening network ports with netstat in Linux?

To display listening network ports using netstat in Linux, you can use the following command:

netstat -tuln

Here’s what each option means:

  • -t: Show TCP connections.
  • -u: Show UDP connections.
  • -l: Display only listening sockets.
  • -n: Show numerical addresses instead of resolving them to hostnames.

This command will provide a list of all listening TCP and UDP ports along with their respective numerical addresses

How to display all network connections with netstat in Linux?

To show all network connections using netstat in Linux, you can use the following command:

netstat -a

Here’s the breakdown of the options:

  • -a: Display all sockets, including listening and non-listening sockets.

This command will show a comprehensive list of all established connections, listening sockets, and other network-related information. Additionally, you can use the -n option to display numerical addresses instead of resolving them to hostnames:

netstat -an

These commands will help you monitor and analyze network connections on your Linux system.

Conclusion

In this article we discussed the netstat command in Linux which is like a special tool that helps you see how your computer connects to the internet. It’s like a window showing you information about connections, data paths, and technical details. This article covers practical examples of netstat commands, from displaying active connections to listing specific types of ports and getting detailed statistics. Whether you’re a beginner or more advanced, netstat offers versatile options. Common questions are answered, making it clear what netstat does and how it differs from other commands like ss in Linux. This knowledge helps users diagnose network issues and understand their system’s internet activities better.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
join Command in Linux
The join command in UNIX is a command line utility for joining lines of two files on a common field. Suppose you have two files and there is a need to combine these two files in a way that the output makes even more sense.For example, there could be a file containing names and the other containing ID's and the requirement is to combine both files i
7 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
help Command in Linux with examples
If you are new to LINUX operating system and having trouble dealing with the command-line utilities provided by LINUX then you really need to know first of all about the help command which as its name says help you to learn about any built-in command. help command as told before just displays information about shell built-in commands. Here's the sy
3 min read
three90RightbarBannerImg