On my system, if I run the netstat for a port, it returns:
$ netstat -nat | grep "60964"
tcp 0 0 192.0.0.1:60964 0.0.0.0:* LISTEN
tcp 59 0 192.0.0.1:60964 192.0.0.6:46962 ESTABLISHED
If I run lsof:
$ lsof -i4 | grep "60964"
process_x 2585 root 189u IPv4 12708 0t0 TCP 192.0.0.1:60964 (LISTEN)
Why is there a difference in the output here? Why isn't lsof detecting the "established" connection.
Thanks!
Edit: I should mention I am the running the above commands as root.
Best Answer
Unlike
netstat
,lsof
requires root privileges in order to print all open ports on system. Althoughlsof
manpage recommendslsof
to be installed setuid root on Linux and setgid on BSD and many other Unixes, in fact most installations choose not to do so. (Whether those permissions should be turned on is another question.)Therefore
lsof
displays connection for any process executed by current user only. To get a full list of connections, runlsof
with root privilege.