lsof to view running services and ports
Now you want to know what services are running on the Linux server and on which ports. You have netstat coming as the first choice of command for this job. Good !!
But you can also use “lsof” command for the purpose.
lsof -Pnl +M -i4
for checking IPv4 ports, and
lsof -Pnl +M -i6
for checking IPv6 ports
-P – will stop the conversion of port numbers to port names for network files. This will make lsof run a little faster.
-n – will stop conversion of network numbers to host names for network files. Useful where host name lookup is not working properly.
-l – will stop conversion of user ID numbers to login names.
+M – reports portmapper registrations for local TCP and UDP ports.
-i4 – IPv4 listing only
-i6 – IPv6 listing only
You can use the lsof command without -Pnl and +M option to see the difference in the output.
Here is a sample output from my system.