There are various commands for both Linux and UNIX, to find out what TCP / UDP ports are able to listen or open. To check this you can use the netstat command which will show us the network connections, routing tables, interface statistics, masquerade connections, and multicast membership, etc. Another is the lsof command, which shows a list of open files and ports on Linux, FreeBSD, Solaris.
Will now show how to use these two commands:
NETSTAT
- To view open ports and established TCP connections
$ netstat -vatn
- To view open ports and established connections UDP
$ netstat -vaun
- To see the full details on the host, it must remove a flag -n
$ netstat -vat
LSOF
- To see a list of open ports
$ lsof -i
- To view all open connections to IPv4 , which identifies the process PID 9255
$ lsof -i 4 -a -p 9255
For UNIX users
On Unix systems we use for this purpose sockstat command . The following shows an example of usage:
- To view all open connections
# sockstat
- To view all open connections for IPv4
$ sockstat -4 -l
- To view all open connections for IPv6
$ sockstat -6 -l