netstat is very useful tool which provides a lot of information about the network of operating system.
netstat command can list ip addreass, route, port, connections etc. More detailed information about the
netstat command can be found in the following tutorial.
List All Listening Ports
We can use
-l options in order to list all listening ports. This will list both TCP and UDP ports with IPv4 and IPv6 . But also Unix domain sockets will be printed in the end of the list after TCP and UDP ports.
$ netstat -l
List Listening TCP Ports
TCP is reliable protocol which provides non data loss. Applications generally prefers and uses TCP protocol for network connections and data transfer. We can use
-t option in order to only list TCP ports.
$ netstat -l -t
List Listening UDP Ports
We have also have the ability to only list UDP ports. We will use
-u option in order to only list UDP ports.
$ netstat -l -u
List Established Connections
We can also list only established connections by removing
-l option which is used in previous examples.
-l was used to list only listening ports.
Filter The Port List
Now the most funny part. If we are running netstat in a busy server or system we will get a lot of output. In this situations we should filter printed list. We will use
grep command where detailed information can be get from following tutorial.
Filter SSH Port
$ netstat -l | grep ssh
Filter HTTP Port
$ netstat -l | grep http
Filter RDP Port
$ netstat -l | grep rdp
Filter Telnet Port
$ netstat -l | grep telnet
Filter Multiple Ports In Single Command
If we need to filter multiple ports in a single command we should use
grep or logic. In this example we will filter both
telnet ports in single command.
$ netstat -l | grep "ssh|telnet"