10 Examples of netstat command in Linux and UNIX

Hello guys, if you are wondering how to check which application is listening to a particular port or wondering what are active UDP and TCP post on your machine then don't worry, you can use the "netstat" command in Linux to get these details. Along with lsof and curl, netstat is another important utility command which I use on almost daily basis as a server side Java developer. Since most of the application I create follow client server model and most of them run on Linux, I often use netstat command during post implementation checks and troubleshooting during any production issue.

 In the past, I have shared examples of ls command, df, du, ps, mailx, find, grep, crontab, and even kill command and In this article, You will be learning the netstat command and provide 10 netstat examples and different usage of netstat command.

The good thing about netstat command is that it not only work on Linux and UNIX but also work on Windows. You can run the netstat command in Windows 10 by just opening a command line window as shown in this article, Before fully diving into that. I will explain the netstat command itself.


What is netstat command and what does it means?

The full meaning of netstat is Network statistics, This is a functionality that enables you to monitor or handle your network. All operating system uses netstat and it is good for network troubleshooting.

However, there is  an update in netstat command, it is now ss command. Which is stronger and more efficient than netstat. That by the way!. netstat gives an overview of network information and as well displays port that are available or the ones that are connected

You can use following commands as replacement of netstat command if you are working in newer version:

ss, ip route (for netstat-r), ip -s link (for netstat -i), ip maddr (for netstat-g)



 

10 ways to use netstat command in Linux

First of all,to get started with netstat you need to have it installed on your command line. Now let us check the 10 examples and usages of netstat command. Let's go

 

1. netstat -a

You can use this netstat command to display all network related information. This is by using the -a option or -all option. Using this with netstat shows all the established or available connections. If you don't want to call it connections then you can call it socket. 
 
Here is the command to use and it lists all active connection.
 

$ netstat -a or netstat -all

More often than not, its also used with other options like netstat -nap or netstat -nao where a is for all and n is for numeric

10 Examples of netstat command in Linux and UNIX


2. netstat -r example to display the routing table

Why do I need to display the routing table? First of all, a routing table is a manual, or let me say it contains a set of rules which is viewed in a table format, this tells us or is used to determine where data sent over the internet are directed. I hope this explains why you need to display the routing table. There could be times or instances that you may have to do that.

$ netstat -r

Here is the output of the command, you can see the routing table below:

netstat -r example to display the routing table


3. netstat -au command to display the UDP information

 And you do that with two different options the -au and -a --udp.

$ "netstat -au" or "netstat -a --udp"

What is UDP? UDP stands for user datagram protocol. It is such a protocol for communicating throughout the internet. You use this command when you need information about the communication going through the internet.

netstat -au command to display the UDP information


4. Use netstat -l to display all server connections

$ "netstat -l"

By using the command above with the option -l this lists out all the listening connections. Displays protocol statistics and current TCP/IP network connections. What does that mean? A listening connection is a state, it only means that a service is listening for or to connections on that port.

Use netstat -l to display all server connections




5. netstat -s to display the network statistics simply do the below

$ netstat -s

The command above would print the summary statistics for each protocol such as TCP, UDP, etc. There could be times you may need to see the network statistics--

netstat -s to display the network statistics simply do the below



6. netstat -tcp to get the TCP port information

$ "netstat --tcp"

There could be times you may want to see the TCP port information

netstat -tcp to get the TCP port information



7. netstat - i to Show network interface connection

$ netstat -i

This shows the network interface packet transactions both at the transferring and the receiving packets with the MTU size as well, You may be wondering what an MTU means, it stands for maximum transmission units.





8. netstat -n to show the network addresses as numbers

$ netstat -n

The command shows the addresses of the networks as numbers. netstat would only display addresses as symbols by default but the option -n enables us to display it in any format. Showing network addresses as numbers is more readable than symbols. As you can see the foreign address column 

netstat -n to show the network addresses as numbers


9. netstat -p to show the name of  the program

$ netstat -p

The command shows or tells us the name of the program a socket or network belongs to. This is very useful because there could be times when you may want to see the program associated with ports and their addresses.

Though, if you want IP address of foreign host then you can also combine -p with -n and run a command like netstat -nap

netstat -p to show the name of  the program




10. netstat -c to continuously print netstat information

$ netstat -c

This above command will print netstat information continuously

netstat -c to display netstat information repeat



11. netstat -g to display multicast group

You can also use command netstat -g to display which multicast group your machine are connected to. 

$ netstat - g

netstat -g to display multicast group



That's all about different ways to use netstat command in Linux. In this article, you learned what netstat means which is used for analyzing network statistics. The 10 different examples were provided which are, to display all information, display the routing table, display the UDP information, display the listening connection, show the network statistics, get the TCP port information, show network addresses as numbers, show the name of the program and to continuously print netstat information.

These were the examples provided. However, that is not all about the netstat command and it contains even more gems which you can discover by yourself. Though, if you find an interesting one then don't forget to share with us, keep learning.

 Other Linux Articles and Resources you may like

  • 5 Example of kill commands in Unix and Linux (example)
  • 10 examples of Networking commands in Unix (nslookup)
  • How to set up cron jobs in Linux (Crontab example)
  • Top 5 Courses to learn Vim Editor (online courses)
  • 10 Examples of chmod command in Linux (chmod)
  • VI Editor examples and tips for beginners (vi examples)
  • 10 Linux command line courses for Beginners (courses)
  • How does the nslookup command work in UNIX? (answer)
  • 10 examples of lsof command in Linux? (examples)
  • 7 Best Linux Courses for DevOps Engineers (Linux courses)
  • How to use the netstat command to find which process is listening on a port? (example)
  • Linux find + du + grep example (example)
  • 10 Examples of curl command in Linux (cURL)

Thanks for reading this article so far. If you like these Linux tutorial about using lsof command and my examples  then please share it with your friends and colleagues.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.