linuxnetstatlsof

What are the differences between lsof and netstat on linux?


I encounted a problem today:

When I started HDP docker container, an error occured:

listen tcp 0.0.0.0:8086: bind: address already in use

According to error message, I know that port 8086 was already in use, so I tried some commands to determine which program was using port 8086. lsof -i:8086 lsof -i tcp:8086 lsof | grep 8086

But all of commands above make no outputs!

I felt really confused about that, after some searching on google, I tried another command: netstat -pna | grep 8086

I got correct output from this command.

I know some differences between lsof and netstat, but I really do not know why I cannot get any output from lsof -i:8086?.

Here are some differences between two commands I searched from google:

netstat(net statistic) is connection based,it shows NW connections (udp/tcp ports), routing tables, interface, multi-cast membership, etc.

lsof(list of open files) is application based, this is kind of like netstat + ps, there you can see all accessed ports, NW connections, etc. but lsof includes stuff like my local emacs window terminal session (tty dev/pts/n) which is not part of netstat


Solution

  • I faced a similar issue today. The solution was to run the lsof command with sudo privileges.

    sudo lsof -i:8086 
    

    should print the desired output.