본문 바로가기

리눅스

[리눅스] lsof 명령어

반응형

lsof(list open file) 명령어

 : Linux/UNIX 시스템에서 열린 파일을 나열하는 유틸리티

lsof 패키지 설치

- centos

yum install -y lsof

- ubuntu

apt-get install -y lsof

lsof 실행 결과

lsof

 

-u s   exclude(^)|select login|UID set s

lsof -u vagrant
$ lsof -u vagrant
COMMAND    PID    USER   FD      TYPE             DEVICE SIZE/OFF       NODE NAME
systemd  23013 vagrant  cwd       DIR              253,0     4096          2 /
systemd  23013 vagrant  rtd       DIR              253,0     4096          2 /
systemd  23013 vagrant  txt       REG              253,0  1849992    1594212 /usr/lib/systemd/systemd
...

 

-i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]

lsof -i 4
$ lsof -i 4
COMMAND     PID            USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
systemd-r   622 systemd-resolve   13u  IPv4  24218      0t0  UDP localhost:domain
systemd-r   622 systemd-resolve   14u  IPv4  24219      0t0  TCP localhost:domain (LISTEN)
nmbd        643            root    4u  IPv4  27515      0t0  UDP kube-control1:netbios-ns
nmbd        643            root   13u  IPv4  24945      0t0  UDP *:netbios-ns
nmbd        643            root   14u  IPv4  24946      0t0  UDP *:netbios-dgm
nmbd        643            root   15u  IPv4  24957      0t0  UDP kube-control1:netbios-ns
nmbd        643            root   16u  IPv4  24958      0t0  UDP 192.168.0.255:netbios-ns
nmbd        643            root   17u  IPv4  24959      0t0  UDP kube-control1:netbios-dgm
nmbd        643            root   18u  IPv4  24960      0t0  UDP 192.168.0.255:netbios-dgm
nmbd        643            root   21u  IPv4  27516      0t0  UDP 172.17.255.255:netbios-ns
nmbd        643            root   22u  IPv4  27517      0t0  UDP kube-control1:netbios-dgm
nmbd        643            root   23u  IPv4  27518      0t0  UDP 172.17.255.255:netbios-dgm
snmpd       646     Debian-snmp    6u  IPv4  24810      0t0  UDP *:snmp
sshd        691            root    3u  IPv4  25657      0t0  TCP *:ssh (LISTEN)
in.tftpd    692            root    4u  IPv4  24855      0t0  UDP *:tftp
smbd        721            root   46u  IPv4  25122      0t0  TCP *:microsoft-ds (LISTEN)
smbd        721            root   47u  IPv4  25123      0t0  TCP *:netbios-ssn (LISTEN)
lsof -i TCP:22
$ lsof -i TCP:22
COMMAND   PID    USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd      691    root    3u  IPv4  25657      0t0  TCP *:ssh (LISTEN)
sshd      691    root    4u  IPv6  25659      0t0  TCP *:ssh (LISTEN)
sshd    23010    root    4u  IPv4 520042      0t0  TCP kube-control1:ssh->121.168.247.231:49947 (ESTABLISHED)
sshd    23099 vagrant    4u  IPv4 520042      0t0  TCP kube-control1:ssh->121.168.247.231:49947 (ESTABLISHED)
sshd    23289    root    4u  IPv4 527252      0t0  TCP kube-control1:ssh->20.232.30.249:37592 (ESTABLISHED)
sshd    23290    sshd    4u  IPv4 527252      0t0  TCP kube-control1:ssh->20.232.30.249:37592 (ESTABLISHED)
sshd    23291    root    4u  IPv4 528814      0t0  TCP kube-control1:ssh->207.159.173.225:43090 (ESTABLISHED)
sshd    23292    sshd    4u  IPv4 528814      0t0  TCP kube-control1:ssh->207.159.173.225:43090 (ESTABLISHED)

 

-c c  cmd c ^c /c/[bix

lsof -c systemd-resolv
$ lsof -c systemd-resolv
COMMAND   PID            USER   FD      TYPE             DEVICE SIZE/OFF    NODE NAME
systemd-r 622 systemd-resolve  cwd       DIR              253,0     4096       2 /
systemd-r 622 systemd-resolve  rtd       DIR              253,0     4096       2 /
systemd-r 622 systemd-resolve  txt       REG              253,0   489904 1594239 /usr/lib/systemd/systemd-resolved
systemd-r 622 systemd-resolve  mem       REG              253,0    27072 1579287 /usr/lib/x86_64-linux-gnu/libcap-ng.so.0.0.0
systemd-r 622 systemd-resolve  mem       REG              253,0    47688 1579323 /usr/lib/x86_64-linux-gnu/libffi.so.8.1.0
systemd-r 622 systemd-resolve  mem       REG              253,0   613064 1573408 /usr/lib/x86_64-linux-gnu/libpcre2-8.so.0.10.4
systemd-r 622 systemd-resolve  mem       REG              253,0   133200 1579267 /usr/lib/x86_64-linux-gnu/libaudit.so.1.0.0
systemd-r 622 systemd-resolve  mem       REG              253,0   526896 1579342 /usr/lib/x86_64-linux-gnu/libgmp.so.10.4.1
systemd-r 622 systemd-resolve  mem       REG              253,0   289800 1579361 /usr/lib/x86_64-linux-gnu/libhogweed.so.6.4
...

lsof 사용법

$ lsof -h
lsof 4.93.2
 latest revision: https://github.com/lsof-org/lsof
 latest FAQ: https://github.com/lsof-org/lsof/blob/master/00FAQ
 latest (non-formatted) man page: https://github.com/lsof-org/lsof/blob/master/Lsof.8
 usage: [-?abhKlnNoOPRtUvVX] [+|-c c] [+|-d s] [+D D] [+|-E] [+|-e s] [+|-f[gG]]
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+m [m]] [+|-M] [-o [o]] [-p s]
 [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Defaults in parentheses; comma-separated set (s) items; dash-separated ranges.
  -?|-h list help          -a AND selections (OR)     -b avoid kernel blocks
  -c c  cmd c ^c /c/[bix]  +c w  COMMAND width (9)    +d s  dir s files
  -d s  select by FD set   +D D  dir D tree *SLOW?*   +|-e s  exempt s *RISKY*
  -i select IPv[46] files  -K [i] list|(i)gn tasKs    -l list UID numbers
  -n no host names         -N select NFS files        -o list file offset
  -O no overhead *RISKY*   -P no port names           -R list paRent PID
  -s list file size        -t terse listing           -T disable TCP/TPI info
  -U select Unix socket    -v list version info       -V verbose search
  +|-w  Warnings (+)       -X skip TCP&UDP* files     -Z Z  context [Z]
  -- end option scan
  -E display endpoint info              +E display endpoint info and files
  +f|-f  +filesystem or -file names     +|-f[gG] flaGs
  -F [f] select fields; -F? for help
  +|-L [l] list (+) suppress (-) link counts < l (0 = all; default = 0)
                                        +m [m] use|create mount supplement
  +|-M   portMap registration (-)       -o o   o 0t offset digits (8)
  -p s   exclude(^)|select PIDs         -S [t] t second stat timeout (15)
  -T qs TCP/TPI Q,St (s) info
  -g [s] exclude(^)|select and print process group IDs
  -i i   select by IPv[46] address: [46][proto][@host|addr][:svc_list|port_list]
  +|-r [t[m<fmt>]] repeat every t seconds (15);  + until no files, - forever.
       An optional suffix to t is m<fmt>; m must separate t from <fmt> and
      <fmt> is an strftime(3) format for the marker line.
  -s p:s  exclude(^)|select protocol (p = TCP|UDP) states by name(s).
  -u s   exclude(^)|select login|UID set s
  -x [fl] cross over +d|+D File systems or symbolic Links
  names  select named files or files on named file systems
Anyone can list all files; /dev warnings disabled; kernel ID check disabled.

 

참고URL

- lsof 명령어 설치 및 사용법 : https://hbase.tistory.com/64

 

반응형

'리눅스' 카테고리의 다른 글

[리눅스] hpasmcli 툴  (0) 2014.06.13
[리눅스] Cacti Install Guide CentOS6(64bit)  (2) 2014.06.10
[리눅스] iftop 명령어  (1) 2014.06.03
[리눅스] route 테이블 추가/삭제  (0) 2014.05.20
[리눅스] sed 명령어  (1) 2014.05.19