
list open files - lsof
lsof 는 list open files 의 약자로, 시스템에서 열린 파일 목록과 관련 정보를 알려줌.
해당 관련 정보는 다음과 같이 표시됨.
COMMAND: 실행한 명령어PID: process idUSER: 실행한 사용자FD: File Descriptor (cwd: current working directory,rtd: root directory,mem: memory-mapped file,txt: program text)TYPE: 파일종류 (DIR: directory,CHR: character special file,REGregular file,unix: unix socket)DEVICE: 장치번호SIZE/OFF: 파일크기, 오프셋NODE: node numberNAME: 파일이름
사용법
다음과 같이 아무 인자 없이 수행시 모든 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과 같이 범위로 지정할 수 있음.NAMEcolumn에서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
참고자료
2023.09.08 - [Linux] - [Tip] 특정 port 사용 중인 프로세스 종료시키기
[Tip] 특정 port 사용 중인 프로세스 종료시키기
sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill 80 port에서 TCP소켓으로 listen 상태인 process의 Process ID를 반환 : sudo lsof -t -i tcp:80 -s tcp:listen xargs로 앞서 수행의 결과인 Process ID들을 kill의 인자로 넘겨줌. 2
ds31x.tistory.com
https://www.lesstif.com/system-admin/lsof-20776078.html
lsof 사용법
www.lesstif.com
'Linux' 카테고리의 다른 글
| [Tip] 특정 문구가 있는 process들을 모두 종료시키기: kill (0) | 2023.09.08 |
|---|---|
| [Tip] 특정 port 사용 중인 프로세스 종료시키기 (0) | 2023.09.08 |
| [zsh] 사용자 Shell 확인 및 변경하기. (0) | 2023.09.08 |
| [Linux] homebrew 설치하기 (Ubuntu) (1) | 2023.09.05 |
| [Tip] Linux 특정 process의 memory 점유율 확인하기 : ps + awk + grep (0) | 2023.08.23 |