본문 바로가기
리눅스

LSOF는 'List Open File'

by 혜룐 2015. 11. 10.
LSOF는 'List Open File'의 약자로, 해당 System에서 구동되고 있는 프로세스들에 의해서 열려진 파일들을 확인 할 수 있는 툴이다. 시스템의 의심스러운 프로세스에 대한 확인이 용이하고, 설치가 비교적 쉬워 많이 이용되고 있다.
[cert:root]:/user/kong/lsof_4.56>lsof
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sched 0 root cwd VDIR 32,0 1024 2 /
sched 0 root 0u VCHR 12,2 0t0 140375 /devices/pseudo/sad@0:user(COMMON)
.....
.....
sendmail 23403 root 13u inet 0x603ca5b8 0t2290 TCP cert:52969->211.45.162.90:smtp (ESTABLISHED)
sendmail 23403 root 14u inet 0x603ca5b8 0t2290 TCP cert:52969->211.45.162.90:smtp (ESTABLISHED)
......
2. 사용하기
■ LSOF 옵션
옵션 
기 능 
옵션 
기 능 
-?
-h 
list help 
-a 
AND selections (OR) 
-d
-D 
s select by FD set
D ?|i|b|r|u[path] 
+|-f 
-files +filesys 
-l 
list UID numbers 
-n
-N 
no host names
select NFS files 
-s 
list file size 
-t
-T 
terse listing
disable TCP/TPI info 
-v
-V 
display version info
verbose search 
-F [f] 
select fields;-F? for help 
-o o 
o 0t offset digits (8) 
-S [t] 
t second stat timeout(15) 
-i i 
select by IPv4 address: [proto][@host|addr][:svc_list|port_list] 
+|-r [t] 
repeat every t seconds (15);
+ until no files, - forever 
-b 
avoid kernel blocks 
-c c 
list command c 
-P 
no port names 
-i 
select IPv4 files 
-p s 
select by PID set 
-C 
no kernel name cache 
+|-w 
Warnings (+) 
-R 
list paRent PID 
-k k 
kernelsymbols (/dev/ksyms) 
-U 
select Unix socket 
-u s 
exclude(^)/select login/UID s 
-m m 
kernel memory (/dev/kmem) 
+|-M 
portMap registration (-) 
-- 
end option scan 
-g [s] 
select by process group ID set and print process group IDs 
names 
select named files or files on named file systems
■ 실행결과 보기
실행결과에 해당되는 각 column 들을 간단하게 살펴보면 다음 [표5] 와 같다. 
Column 
설 명 
Command 
프로세스와 관련된 Unix command 이름 
PID
PPID
PGRP 
Process IDentification number 
Parent Process IDentification number
(해당 프로세스의 부모 프로세스 ID) 
Process Group IDentification number
(해당 프로세스와 관련된 프로세스 그룹 ID) 
USER 
해당 프로세스를 소유한 사용자 ID 또는 login name 
FD 
File Descriptor number
(ex) cwd : current working directory
r : read access / w : write access / u : read and write access 
TYPE 
해당 파일과 관련한 노드 타입
(ex) inet : Internet domain socket 
DEVICE 
device number 
SIZE
SIZE/OFF
OFFSET 
file 이나 file offset의 사이즈 
INODE
NODE-ID 
local file 의 node number 또는 Internet protocol type
또는 서버 호스트의 NFS file의 inode number 
NAME 
해당 파일이 소속된 mount point나 파일 시스템의 이름
■ LSOF 주요 옵션 사용 예
* 특정 파일을 억세스하고 있는 프로세스 확인 : lsof<path/file-name>
[cert:root]:/>lsof /etc/passwd
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
ns-httpd 244 root 9r VREG 32,0 3044 99217 /etc/passwd
....
* internet socket 확인 : lsof -i
Internet address 출력형식 : [protocol][@hostname|hostaddr][:service|port]
- 특정 호스트(또는 ip)에 대한 접속 확인
[cert:root]:/>lsof -i@172.16.2.146
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
in.telnet 10124 root 0u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
in.telnet 10124 root 1u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
in.telnet 10124 root 2u inet 0x61d4b788 0t71 TCP cert:telnet->172.16.2.146:1109 (ESTABLISHED)
.....
- 특정 포트로 접속한 리스트 확인
[cert:root]:/usr/sbin>lsof -i @certcc.or.kr:23
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
in.telnet 104 root 2u inet 0x612df850 0t152 TCP cert:telnet->172.16.2.159:1176 (ESTABLISHED)
in.telnet 28462 root 0u inet 0x61aee578 0t71 TCP cert:telnet->172.16.2.146:4250 (ESTABLISHED)
.....
* 특정 user가 오픈한 프로세스를 확인 : lsof -u<loginname>혹은 lsof -u<UID>
[cert:root]:/>lsof -u kong
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
csh 21309 kong cwd VDIR 32,4 1536 223602 /user/kong/lsof_4.56
csh 21309 kong txt VREG 32,6 158608 298136 /usr/bin/csh
.......
- 특정사용자 제외시 "^" 심볼을 사용하고, 여러명을 동시에 지정하려면 ","를 이용한다.
[cert:root]:/dev/pts>lsof -u ^root,kong,yjkim
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
csh 8992 yjkim cwd VDIR 32,0 1024 2 /
csh 8992 yjkim txt VREG 32,6 158608 298136 /usr/bin/csh
csh 8992 yjkim txt VREG 32,6 70996 136979 /usr/lib/locale/ko/ko.so.1
csh 8992 yjkim txt VREG 32,6 1024888 6749 /usr/lib/libc.so.1
.....
csh 21309 kong cwd VDIR 32,4 1536 223602 /user/kong/lsof_4.56
csh 21309 kong txt VREG 32,6 158608 298136 /usr/bin/csh
* 특정 프로세스가 오픈한 파일 리스트 확인 : lsof -p<PID>
- PID 112를 가진 프로세스가 사용하는 파일들 확인
[cert:root]:/usr/sbin>lsof -p 143
COMMAND PID USER FD TYPE DEVICE SIZE/OFF INODE NAME
inetd 143 root cwd VDIR 32,0 1024 2 /
inetd 143 root txt VREG 32,6 33492 310933 /usr/sbin/inetd
inetd 143 root txt VREG 32,6 10696 6352 /usr (/dev/dsk/c0t0d0s6)
....
....
inetd 143 root 4u inet 0x603cab38 0t0 TCP *:ftp (LISTEN)
inetd 143 root 5u inet 0x610b13c0 0t0 TCP *:telnet (LISTEN)
inetd 143 root 6u inet 0x610b1240 0t0 TCP *:pop3 (LISTEN)
......
* ps 명령이 변조된 시스템에서는 의심스러운 포트 확인하기 예
[root@linux /root]# netstat -a
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:auth *:* LISTEN
tcp 0 0 *:ftp *:* LISTEN
tcp 0 0 *:telnet *:* LISTEN
tcp 0 0 *:login *:* LISTEN
tcp 0 0 *:2626 *:* LISTEN
.......
[root@linux /dev]# fuser -n tcp 2626
2626/tcp: 607
[root@linux /dev]# lsof -p 607
PID TTY STAT TIME COMMAND
607 ? S 0:00 /usr/sbin/mingetty