Netstat Command Examples in Linux
Netstat is one of the most common networking commands in Linux. Learn some useful examples of netstat in this tutorial.
|
|
|
|
|
|
|
|
The netstat is one of the most popular utilities to monitor connections over your network.
It allows you to easily monitor incoming and outgoing connections, listening ports, statistics, and more.
In this tutorial, I will show you some of the most examples of the netstat command on Linux.
To find all the ports (TCP and UDP), you will have to append the -l
flag with the netstat command:
netstat -l
If you want to get a list of available sockets on your system, you can use the -a
flag with the netstat command:
netstat -a
Now, let's get to more specific ones.
If you want to list ports using TCP protocol and in the listening state, you will have to use -l
flag for listening and -t
flag for TCP connections:
netstat -lt
To list every listening UDP port on your system, you will have to append -l
and -u
flag with the netstat command:
netstat -lu
If you want to list every socket using a TCP connection including listening and non-listening, use the -at
flag with the netstat command:
netstat -at
Want to know the difference between listening and an established state?
LISTENING
means it is listening for incoming connections.ESTABLISHED
indicates that the socket has an established connection.If you want to list every socket utilizing the UDP, you can use the combination of -a
and -u
flag:
netstat -au
This is one of the handiest features of netstat which allows you to find the number of connections established, the number of messages sent and received, and a lot more.
To get a summary of each protocol, all you need to do is append the -s
flag:
netstat -s
But what if you want statistics on specific protocols? Here's how you do it.
Let's start with the TCP.
To get the statistics of TCP connections, all you need to do is use the -s
and -t
flag with the netstat command:
netstat -st
Similarly, if you want the same for UDP, you will have to use the -su
flag:
netstat -su
If you are looking for raw data rather than filtered one, it can easily be produced using the -s
(for statistics) and --raw
(for raw):
netstat -s --raw
If you are into troubleshooting, getting the PID of the service can be very handy. To get PID, all you need to do is use the -p
flag:
sudo netstat -p
To find a specific listening, you can use the grep command which makes a killer combination while troubleshooting.
So let's suppose, I want to look for an HTTPS service on listening state which can be done through the following command:
sudo netstat -apl | grep -w https
Want to know how to get more out of grep? you can refer to our detailed guide on that topic:
The netstat utility can also be used to list available network interfaces and to get transactions of each one.
For that, all you need to do is append the -i
flag to the netstat:
netstat -i
If you want to monitor the network continuously, you can do it with -c
the option:
netstat -c
You can use appropriate flags such as -lt
with -c
and it will look for listening TCP connections continuously:
netstat -ltc
Pretty handy. Right?
If you have just started your carries or studies on networking, we have a detailed guide on most basic networking commands:
Want to know more about ports? We got you covered on that too:
That was it from my side. And if you have any doubts or have tips for beginners, you can share your precious knowledge through the comments.
|
|
|
|