
Terminal - Commands tagged lsof - 30 results
lsof -i -n | grep ESTABLISHED
This is sample output - yours may be different.
Fast and easy way to find all established tcp connections without using the netstat command.
watch "lsof -i -P |grep ESTABLISHED |awk '{printf \"%15.15s \\t%s\\n\", \$1, \$9}'"
This is sample output - yours may be different.
Finder localhost:53255->localhost:26172
imagent mycomputer.foo.com:59319->qa-in-f125.1e100.net:5223
imagent mycomputer.foo.com:59339->bos-m015c-new-rdr2.blue.aol.com:443
Google mycomputer.foo.com:58776->qc-in-f125.1e100.net:5222
Google mycomputer.foo.com:60731->qa-in-f125.1e100.net:5222
Google mycomputer.foo.com:60858->qc-in-f125.1e100.net:5222
Google mycomputer.foo.com:60982->qc-in-f125.1e10
Shows which applications are making connections, and the addresses they're connecting to. Refreshes every 2 seconds (watch's default). Test on OSX, should work anywhere watch and lsof work.
This is sample output - yours may be different.
also could specify port number:
lsof -ni TCP:80
lsof -i -P +c 0 +M | grep -i "$1"
This is sample output - yours may be different.
watch -d=c -n3 'lsof -itcp -iudp -c php'
This is sample output - yours may be different.
Shows files and processes of the command php
lsof +c 15 | awk '{print $1}' | sort | uniq -c | sort -rn | head
This is sample output - yours may be different.
5993 chromium-browse
1466 gwibber-service
1148 xfce4-terminal
765 bash
725 ssh
721 apache2
314 pidgin
298 thunderbird-bin
295 gtk-window-deco
288 firefox
This is sample output - yours may be different.
apache2 14485 www-data 8u IPv4 186095222 0t0 TCP *:443 (LISTEN)
apache2 14770 www-data 4u IPv4 186095213 0t0 TCP *:80 (LISTEN)
This option is much faster, as it checks only network nodes.
This is sample output - yours may be different.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
dhclient3 817 root 5u IPv4 3745 0t0 UDP *:bootpc
sshd 1014 root 3u IPv4 4770 0t0 TCP *:ssh (LISTEN)
sshd 1014 root 4u IPv6 4772 0t0 TCP *:ssh (LISTEN)
postgres 1117 postgres 3u IPv6 5608 0t0 TCP localhost:7175 (LISTEN)
postgres 1117 postgres 4u IPv4 5609 0t0 TCP localhost:7175 (LISTEN)
postgres 1117 postgres 6u IPv6 5622 0t0 UDP localhost:56982->localhost:56982
postgres 1487 postgres 6u IPv6 5622 0t0 UDP localhost:56982->localhost:56982
postgres 1488 postgres 6u IPv6 5622 0t0 UDP localhost:56982->localhost:56982
postgres 1489 postgres 6u IPv6 5622 0t0 UDP localhost:56982->localhost:56982
postgres 1490 postgres 6u IPv6 5622 0t0 UDP localhost:56982->localhost:56982
cupsd 1539 root 5u IPv6 5710 0t0 TCP localhost:ipp (LISTEN)
cupsd 1539 root 6u IPv4 5711 0t0 TCP localhost:ipp (LISTEN)
sshd 27689 root 3r IPv4 80824 0t0 TCP 172.16.221.137:ssh->vayu:59742 (ESTABLISHED)
sshd 27689 root 8u IPv6 80950 0t0 TCP localhost:6010 (LISTEN)
sshd 27689 root 9u IPv4 80951 0t0 TCP localhost:6010 (LISTEN)
This option selects the listing of all Internet and x.25 (HP-UX) network files.
This is sample output - yours may be different.
Proto Recv-Q Send-Q Adresse locale Adresse distante Etat PID/Program name
tcp 0 0 0.0.0.0:8118 0.0.0.0:* LISTEN 1284/privoxy
tcp 0 0 0.0.0.0:4001 0.0.0.0:* LISTEN 1901/java
tcp 0 0 0.0.0.0:7175 0.0.0.0:* LISTEN 1546/postgres
udp 0 0 0.0.0.0:68 0.0.0.0:* 2028/dhclient
udp 0 0 0.0.0.0:5353 0.0.0.0:* 1394/avahi-daemon:
udp 0 0 0.0.0.0:46876 0.0.0.0:* 1394/avahi-daemon:
udp6 0 0 :::58041 :::* 1394/avahi-daemon:
udp6 0 0 :::5353 :::* 1394/avahi-daemon:
Check open TCP and UDP ports
This is sample output - yours may be different.
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13443/apache2
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 801/sshd
tcp6 0 0 :::25565 :::* LISTEN 1249/java
tcp6 0 0 :::6667 :::* LISTEN 12101/bitlbee
tcp6 0 0 :::22 :::* LISTEN 801/sshd
While `lsof` will work, why not use the tool designed explicitly for this job?
(If not run as root, you will only see the names of PID you own)
This is sample output - yours may be different.
This is sample output - yours may be different.
It may be helpful in case you need to umount a directory and some process is preventing you to do so keeping the folder busy. The lsof may process the +D option slowly and may require a significant amount of memory because it will descend the full dir tree. On the other hand it will neither follow symlinks nor other file systems.
watch -n 1 "/usr/sbin/lsof -p PID |awk '/TCP/{split(\$8,A,\":\"); split(A[2],B,\">\") ; split(B[1],C,\"-\"); print A[1],C[1],B[2], \$9}' | sort | uniq -c"
This is sample output - yours may be different.
Every 1.0s: /usr/sbin/lsof -p 24685 |awk '/TCP/{split($8,A,":"); split(A[2],B,">") ; split(B[1],C,"-"); print A[1],C[1],B[2], $9}' | sort | uniq -c Fri Aug 12 13:58:17 2011
1 172.1.1.1 35735 10.60.10.30 (ESTABLISHED)
1 172.1.1.1 54371 10.60.10.31 (ESTABLISHED)
1 172.1.1.1 icl 10.6.20.30 (ESTABLISHED)
2 172.1.1.1 icl appcc (ESTABLISHED)
1 * icl (LISTEN)
Maybe this will help you to monitor your load balancers or reverse proxies if you happen to use them. This is useful to discover TIME OUTS and this will let you know if one or more of your application servers is not connected by checking.
lsof -nPi | txt2html > ~/lsof.html | gnome-open lsof.html
This is sample output - yours may be different.
In addition to generating the current connections, it also opens then in your default browser on gnome.
lsof -nPi | txt2html > ~/lsof.html
This is sample output - yours may be different.
Open html file with browser to view output.
The output of lsof is piped to txt2html which converts it to html.
# Perl module HTML::TextToHTML needed
This is sample output - yours may be different.
lsof | awk '/*:https?/{print $2}' | sort -u
This is sample output - yours may be different.
% lsof -v
lsof version information:
revision: 4.78
lsof -nP -c COMMAND | egrep -o '(TCP|UDP).*$' | sort -u
This is sample output - yours may be different.
$ lsof -nP -c apache2 | egrep -o 'TCP.*$' | sort -u
TCP 192.168.0.2:443->192.168.0.136:3373 (ESTABLISHED)
TCP 192.168.0.2:443->192.168.0.137:11127 (ESTABLISHED)
TCP 192.168.0.2:443->192.168.0.137:11153 (ESTABLISHED)
TCP *:443 (LISTEN)
TCP *:80 (LISTEN)
$ lsof -p 17279|egrep -o '(TCP|UDP).*$'
TCP 127.0.0.1:32434 (LISTEN)
UDP *:32435
Where COMMAND is the process(es) name. I prefer to get all states but you may add ESTABLISHED in the grep regex.
lsof -c apache2 | egrep -o 'TCP.*ESTABLISHED.*$'
-nP flags are optional and UDP is irrelevant for established connections
Similar but using the process id:
lsof -nP -p PID | egrep -o '(TCP|UDP).*$'
This is sample output - yours may be different.
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 20355 root cwd DIR 8,1 4096 25952257 /folder
lsof /dev/snd/pcm*p /dev/dsp | awk ' { print $2 }' | xargs kill
This is sample output - yours may be different.
for when a program is hogging the sound output. finds, and kills. add -9 to the end for wedged processes. add in 'grep ^program' after lsof to filter.
This is sample output - yours may be different.
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nmbd 1381 root 10u IPv4 37683465 0t0 UDP *:137
nmbd 1381 root 13u IPv4 37683466 0t0 UDP *:138
nmbd 1381 root 14u IPv4 37683468 0t0 UDP 192.168.1.66:137
nmbd 1381 root 15u IPv4 37683469 0t0 UDP 192.168.1.66:138
nmbd 1381 root 16u IPv4 37683470 0t0 UDP 192.168.1.6:137
nmbd 1381 root 17u IPv4 37683471 0t0 UDP 192.168.1.6:138
smbd 1384 root 25u IPv6 37683528 0t0 TCP *:445 (LISTEN)
smbd 1384 root 26u IPv6 37683530 0t0 TCP *:139 (LISTEN)
smbd 1384 root 27u IPv4 37683532 0t0 TCP *:445 (LISTEN)
smbd 1384 root 28u IPv4 37683534 0t0 TCP *:139 (LISTEN)
portmap 1642 daemon 4u IPv4 4146 0t0 UDP *:111
portmap 1642 daemon 5u IPv4 4161 0t0 TCP *:111 (LISTEN)
rpc.statd 1658 statd 4u IPv4 4186 0t0 UDP *:986
rpc.statd 1658 statd 6u IPv4 4195 0t0 UDP *:50215
rpc.statd 1658 statd 7u IPv4 4198 0t0 TCP *:37962 (LISTEN)
cupsd 1689 root 5u IPv4 51957733 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 16u IPv6 51957734 0t0 TCP *:631 (LISTEN)
cupsd 1689 root 18u IPv4 51957737 0t0 UDP *:631
dhclient 2031 root 5u IPv4 5307 0t0 UDP *:68
apache2 2093 root 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
avahi-dae 2281 avahi 13u IPv4 5670 0t0 UDP *:5353
avahi-dae 2281 avahi 14u IPv6 5671 0t0 UDP *:5353
avahi-dae 2281 avahi 15u IPv4 5672 0t0 UDP *:60574
avahi-dae 2281 avahi 16u IPv6 5673 0t0 UDP *:37378
hddtemp 2705 root 0u IPv4 6851 0t0 TCP 127.0.0.1:7634 (LISTEN)
dhclient 3115 root 5u IPv4 10765 0t0 UDP *:68
exim4 13674 Debian-exim 4u IPv4 45844908 0t0 TCP 127.0.0.1:25 (LISTEN)
sshd 26220 root 3u IPv4 16151998 0t0 TCP *:22 (LISTEN)
sshd 26220 root 4u IPv6 16152000 0t0 TCP *:22 (LISTEN)
apache2 30313 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
apache2 30316 www-data 4u IPv6 5480 0t0 TCP *:80 (LISTEN)
This command is more portable than it's cousin netstat. It works well on all the BSDs, GNU/Linux, AIX and Mac OS X. You won't find lsof by default on Solaris or HPUX by default, but packages exist around the web for installation, if needed, and the command works as shown. This is the most portable command I can find that lists listening ports and their associated pid.
This is sample output - yours may be different.
vlc 24143 miles mem CHR 116,4 4240 /dev/snd/pcmC0D0p
When trying to play a sound you may sometimes get an error saying that your sound card is already used, but not by what process. This will list all processes playing sound, useful to kill processes that you no longer need but that keep using your sound card.
lsof -p $(pidof firefox) | awk '/.mozilla/ { s = int($7/(2^20)); if(s>0) print (s)" MB -- "$9 | "sort -rn" }'
This is sample output - yours may be different.
41 MB -- /home/tzk/.mozilla/firefox/default/urlclassifier3.sqlite
6 MB -- /home/tzk/.mozilla/firefox/default/places.sqlite
3 MB -- /home/tzk/.mozilla/firefox/default/XPC.mfasl
2 MB -- /home/tzk/.mozilla/firefox/default/extensions/writearea@writearea.com/chrome/writearea.jar
2 MB -- /home/tzk/.mozilla/firefox/default/extensions/writearea@writearea.com/chrome/writearea.jar
2 MB -- /home/tzk/.mozilla/firefox/default/extensions/{4BBDD651-70CF-4821-84F8-2B918CF89CA3}-trash/chrome/febe.jar
Just refining last proposal for this check, showing awk power to make more complex math (instead /1024/1024, 2^20). We don't need declare variable before run lsof, because $(command) returns his output. Also, awk can perform filtering by regexp instead to call grep. I changed the 0.0000xxxx messy output, with a more readable form purging all fractional numbers and files less than 1 MB.
lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
This is sample output - yours may be different.
kopete
ktorrent
firefox-b
show only the name of the apps that are using internet
FFPID=$(pidof firefox-bin) && lsof -p $FFPID | awk '{ if($7>0) print ($7/1024/1024)" MB -- "$9; }' | grep ".mozilla" | sort -rn
This is sample output - yours may be different.
29.8359 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/urlclassifier3.sqlite
4.91797 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/places.sqlite
4.11752 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/Cache/_CACHE_003_
4.0147 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/XUL.mfasl
2.73011 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/extensions/{000a9d1c-beef-4f90-9363-039d445309b8}/lib/ff35/libgears.so
2.25571 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/extensions/{c45c406e-ab73-11d8-be73-000a95be3b12}/chrome/webdeveloper.jar
2.03915 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/Cache/_CACHE_001_
2.03215 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/Cache/_CACHE_002_
1.25815 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/extensions/{DDC359D1-844A-42a7-9AA1-88A850A938A8}/chrome/chrome.jar
0.382812 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/cookies.sqlite
0.191917 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/extensions/{a7c6cf7f-112c-4500-a7ea-39801a327e5f}/chrome/fireftp.jar
0.185547 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/signons.sqlite
0.166023 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/extensions/{582195F5-92E7-40a0-A127-DB71295901D7}/chrome/gmanager.jar
0.149414 MB -- /home/user/.mozilla/firefox/l0ad8wrj.default/ubiquity_ann.sqlite
Check which files are opened by Firefox then sort by largest size (in MB). You can see all files opened by just replacing grep to "/". Useful if you'd like to debug and check which extensions or files are taking too much memory resources in Firefox.