본문 바로가기
Linux

[Command] lsof : 열린 파일 혹은 port 관련 정보 출력

by ds31x 2023. 9. 8.

lsof 는 list open files 의 약자로, 시스템에서 열린 파일 목록과 관련 정보를 알려줌.

해당 관련 정보는 다음과 같이 표시됨.

  • COMMAND : 실행한 명령어
  • PID : process id
  • USER : 실행한 사용자
  • FD : File Descriptor (cwd : current working directory, rtd : root directory, mem : memory-mapped file, txt: program text)
  • TYPE : 파일종류 (DIR : directory, CHR : character special file, REG regular file, unix : unix socket)
  • DEVICE : 장치번호
  • SIZE/OFF : 파일크기, 오프셋
  • NODE : node number
  • NAME : 파일이름

다음과 같이 아무 인자 없이 수행시 모든 open file 정보를 출력함.

❯ lsof
COMMAND    PID    TID    TASKCMD    USER     FD        TYPE    DEVICE    SIZE/OFF    NODE    NAME
systemd      1                   root    cwd     unknown                                  /proc/1/cwd (readlink: Permission denied)
systemd      1                   root    rtd     unknown                                  /proc/1/root (readlink: Permission denied)
systemd      1                   root    txt     unknown                                  /proc/1/exe (readlink: Permission denied)
systemd      1                   root    NOFD                                             /proc/1/fd (opendir: Permission denied)
init         5                   root    cwd     unknown                                  /proc/5/cwd (readlink: Permission denied)
init         5                   root    rtd     unknown                                  /proc/5/root (readlink: Permission denied)
이하 생략.

사용자 지정을 하면 특정 사용자의 open file을 보여줌.

❯ lsof -u dsaint31

특정 파일을 사용하고 있는 process의 정보를 보려면 다음과 같이 처리.

❯ lsof /var/log/nginx/error.log
COMMAND    PID     USER    FD   TYPE DEVICE SIZE/OFF   NODE NAME
nginx   290914 dsaint31    2w    REG   8,48      458 129482 /var/log/nginx/error.log
nginx   290914 dsaint31    5w    REG   8,48      458 129482 /var/log/nginx/error.log
nginx   290915 dsaint31    2w    REG   8,48      458 129482 /var/log/nginx/error.log
nginx   290915 dsaint31    5w    REG   8,48      458 129482 /var/log/nginx/error.log
nginx   290916 dsaint31    2w    REG   8,48      458 129482 /var/log/nginx/error.log
이하 생략

특정 디렉토리 내 열린 파일도 확인 가능함.

❯ lsof +D /var/log/nginx
COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF   NODE NAME
nginx   290914 dsaint31    2w   REG   8,48      458 129482 /var/log/nginx/error.log
nginx   290914 dsaint31    4w   REG   8,48    15318 129619 /var/log/nginx/access.log
nginx   290914 dsaint31    5w   REG   8,48      458 129482 /var/log/nginx/error.log
이하생략

특정 port를 사용하는 process 확인하기.

❯ lsof -i TCP:80
COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   290914 dsaint31    7u  IPv4 4553090      0t0  TCP *:http (LISTEN)
nginx   290914 dsaint31    8u  IPv6 4553091      0t0  TCP *:http (LISTEN)
nginx   290915 dsaint31    7u  IPv4 4553090      0t0  TCP *:http (LISTEN)
이하생략
  • -i 옵션 뒤에 protocol과 port를 명시해줌.
  • lsof -i TCP:22-80 과 같이 범위로 지정할 수 있음.
  • NAME column에서 http와 같이 /etc/services에 지정된 서비스명이 출력됨.
  • -P 옵션을 주면, 해당 부분이 서비스명 대신 port를 표시함.
❯ lsof -i TCP:80 -P
COMMAND    PID     USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
nginx   290914 dsaint31    7u  IPv4 4553090      0t0  TCP *:80 (LISTEN)
nginx   290914 dsaint31    8u  IPv6 4553091      0t0  TCP *:80 (LISTEN)
nginx   290915 dsaint31    7u  IPv4 4553090      0t0  TCP *:80 (LISTEN)
이하 생략

특정 port에서 TCP로 listen상태인 경우 다음과 같이 인자를 줌.

-i tpc:80 -s tcp:listen

참고로 -t 옵션을 줄 경우, PID만을 출력함.
다음의 명령어는 dsaint31 User가 실행한 PID를 보여줌.

lsof -t -u dsaint31

참고자료

https://www.lesstif.com/system-admin/lsof-20776078.html

 

lsof 사용법

 

www.lesstif.com

 

728x90