Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 961
Alec.Zhou Avatar asked Mar 20 '18 09:03

Alec.Zhou


People also ask

What is netstat used for in Linux?

The network statistics ( netstat ) command is a networking tool used for troubleshooting and configuration, that can also serve as a monitoring tool for connections over the network. Both incoming and outgoing connections, routing tables, port listening, and usage statistics are common uses for this command.

What is the Linux equivalent of netstat?

Formally, ss is the socket statistics command that replaces netstat .

What is lsof used for?

The lsof command stands for LiSt Open Files and shows open files and which process uses them. Since Linux sees every object as a file, such as devices, directories, etc., unidentified open files prevent users from modifying them.

Does Linux have netstat?

The netstat command displays information on the network configuration and activity of a Linux system, including network connections, routing tables, interface statistics, and multicast memberships.


1 Answers

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.

like image 165
saravana_pc Avatar answered Oct 07 '22 19:10

saravana_pc