lsof 란?
lsof(list open files)의 약자로 시스템에서 열린 파일 목록을 알려주고, 사용하는 프로세스, 디바이스 정보, 파일등의 상세 정보를 출력한다.
리눅스와, 유닉스는 추상화된 파일 시스템 (VFS - Virtual File System)을 사용하므로 일반파일, 디렉터리, 네트워크 소켓, 라이브러리, 심볼릭 링크 등도 모두 파일로 처리 되며 상세정보를 확인 가능하다.
어떻게 사용할 수 있는가?
lsof 사용자 지정
지정된 사용자로 열린 파일을 볼 수 있다.
> lsof -u whydda
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
loginwind 391 whydda cwd DIR 1,17 640 2 /
loginwind 391 whydda txt REG 1,17 2754560 1152921500312204239 /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow
loginwind 391 whydda txt REG 1,17 46732 10027125 /Library/Preferences/Logging/.plist-cache.Pk9Eq9oH
loginwind 391 whydda txt REG 1,17 209680 1152921500312353171 /System/Library/LoginPlugins/FSDisconnect.loginPlugin/Contents/MacOS/FSDisconnect
loginwind 391 whydda txt REG 1,17 2160688 1152921500312781028 /usr/lib/dyld
loginwind 391 whydda txt REG 1,17 30400112 1152921500312792673 /usr/share/icu/icudt70l.dat
loginwind 391 whydda txt REG 1,17 335328 1152921500312353121 /System/Library/LoginPlugins/DisplayServices.loginPlugin/Contents/MacOS/DisplayServices
loginwind 391 whydda txt REG 1,17 240720 277685 /private/var/db/timezone/tz/2022a.1.0/icutz/icutz44l.dat
loginwind 391 whydda txt REG 1,17 136152 1152921500312198918 /System/Library/CoreServices/SystemAppearance.bundle/Contents/Resources/SystemAppearance.car
loginwind 391 whydda txt REG 1,17 208016 1152921500312781071 /usr/lib/pam/pam_opendirectory.so.2
loginwind 391 whydda txt REG 1,17 265264 1152921500312200067 /System/Library/CoreServices/TextInputMenuCore.bundle/Contents/MacOS/TextInputMenuCore
loginwind 391 whydda txt REG 1,17 17258 1152921500312204296 /System/Library/CoreServices/loginwindow.app/Contents/Resources/ShutDown.tiff
loginwind 391 whydda txt REG 1,17 7738 1152921500312344324 /System/Library/Input Methods/KoreanIM.app/Contents/PlugIns/KIM_Extension.appex/Contents/Resources/2SetKorean.tiff
loginwind 391 whydda txt REG 1,17 4124 1677016 /Library/Caches/com.apple.iconservices.store/11C21287-3B82-3F0F-8A72-64B1ABC05E00.isdata
loginwind 391 whydda txt REG 1,17 4124 1458414 /Library/Caches/com.apple.iconservices.store/F7747B5B-5F20-3649-8194-E77310F0AFFD.isdata
loginwind 391 whydda txt REG 1,17 135104 1455657 /System/Library/Caches/com.apple.IntlDataCache.le.kbdx
loginwind 391 whydda txt REG 1,17 1273836 1152921500312348465 /System/Library/Keyboard Layouts/AppleKeyboardLayouts.bundle/Contents/Resources/AppleKeyboardLayouts-L.dat
loginwind 391 whydda txt REG 1,17 2736376 1152921500312198908 /System/Library/CoreServices/SystemAppearance.bundle/Contents/Resources/FauxVibrantLight.car
loginwind 391 whydda txt REG 1,17 71320 1152921500312198904 /System/Library/CoreServices/SystemAppearance.bundle/Contents/Resources/FauxVibrantDark.car
loginwind 391 whydda txt REG 1,17 7704 1960699 /Library/Application Support/CrashReporter/SubmitDiagInfo.domains
loginwind 391 whydda txt REG 1,17 202800 1152921500312265470 /System/Library/Frameworks/FileProvider.framework/OverrideBundles/CloudDocsFileProvider.bundle/Contents/MacOS/CloudDocsFileProvider
lsof port 지정
특정 프로세스가 kill이 필요한 경우 아주 유용하게 사용할 수 있다.
> lsof -i :9090
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
java 89219 whydda 293u IPv6 0x45e219647eae227 0t0 TCP *:websm (LISTEN)
특정 명령어가 사용하는 포트
apache httpd 같은 경우 fork 하여 여러개의 프로세스가 뜨게 된다. -c 옵션과 httpd 를 주면 httpd 가 오픈한 파일 정보를 출력한다.
$ lsof -c httpd
현재 크게 사용할만한 정보만 적어보았고 나머지는 아래의 링크를 통해 필요한게 있으면 그때 그때 가져다 쓰면 될듯하다. 자주 사용하지 않는 것은 까먹기 쉬움으로 자주 사용하지 않는 명령어는 아래의 링크를 통해 좀더 자세히 배울 수 있따.
https://www.lesstif.com/system-admin/lsof-20776078.html