(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Linux】lsofコマンドの出力結果メモ

スポンサーリンク

はじめに

list open filesの略であるlsofコマンドは言葉の通りファイルに関する情報を表示できます。
Linuxではデバイス含めてファイルとして扱われる性質がある以上、幅広い情報を取得できるコマンドなので今回は出力結果についてメモを残しておきます。

lsofコマンドのバージョン

lsof version information:
revision: 4.89

lsofコマンドの出力結果

結果の先頭三行を表示してみました。

# lsof | head -n 3
COMMAND     PID   TID       USER   FD      TYPE             DEVICE  SIZE/OFF       NODE NAME
systemd       1             root  cwd       DIR                8,1      4096          2 /
systemd       1             root  rtd       DIR                8,1      4096          2 /
systemd       1             root  txt       REG                8,1   1120992     427542 /lib/systemd/systemd

上記を表形式に直すと以下になります。

COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd DIR 8,1 4096 2 /
systemd 1 root rtd DIR 8,1 4096 2 /
systemd 1 root txt REG 8,1 1120992 427542 /lib/systemd/systemd

-uオプションにてUSERを絞ることもできるので同時にログインしているユーザまたはバッチ用ユーザの挙動を見るには使えそうです。

試しにlsofコマンドがlsofコマンドでどう見えるか調査

$ lsof | grep lsof

とすると以下のようにツラツラと出てきました。

lsof       6180             root  cwd       DIR                8,1      4096     404819 /root/
lsof       6180             root  rtd       DIR                8,1      4096          2 /
lsof       6180             root  txt       REG                8,1    163136     131133 /usr/bin/lsof
lsof       6180             root  mem       REG                8,1    135440     393711 /lib/x86_64-linux-gnu/libpthread-2.24.so
lsof       6180             root  mem       REG                8,1     14640     393323 /lib/x86_64-linux-gnu/libdl-2.24.so
lsof       6180             root  mem       REG                8,1    468920     393513 /lib/x86_64-linux-gnu/libpcre.so.3.13.3
lsof       6180             root  mem       REG                8,1   1689360     393320 /lib/x86_64-linux-gnu/libc-2.24.so
lsof       6180             root  mem       REG                8,1    155400     393398 /lib/x86_64-linux-gnu/libselinux.so.1
lsof       6180             root  mem       REG                8,1    153288     393316 /lib/x86_64-linux-gnu/ld-2.24.so
lsof       6180             root  mem       REG                8,1   1679776     131075 /usr/lib/locale/locale-archive
lsof       6180             root    0u      CHR              136,0       0t0          3 /dev/pts/0
lsof       6180             root    1w     FIFO               0,10       0t0   12008650 pipe
lsof       6180             root    2u      CHR              136,0       0t0          3 /dev/pts/0
lsof       6180             root    3r      DIR                0,4         0          1 /proc
lsof       6180             root    4r      DIR                0,4         0   12008653 /proc/6180/fd
lsof       6180             root    5w     FIFO               0,10       0t0   12008658 pipe
lsof       6180             root    6r     FIFO               0,10       0t0   12008659 pipe

コマンドのフルパスや実行したディレクトリなど解析に必要そうな情報が取れていることが確認できました。

インストール

$ apt-cache search lsof
...
<b><span style="color: #ff0000">lsof - Utility to list open files</span></b>
...

で確認できるので

# apt-get install lsof

でインストール可能です。

終わりに

マニュアルを見ると以下のように解説がありますが

Command: The name of the command associated with the process that opened the file.
PID: Process Identification number of the process that opened the file.
TID: Task (thread) Identification number. A blank column means it is not a task; it is a process.
User: User ID or name of the user to whom the process belongs, or the user ID or login of the person that owns the directory in /proc where lsof finds information about the process.
FD: Shows the file descriptor of the file. File descriptors are described below.
Type: type of node associated with the file. Note types are described below.
Device: Contains either the device numbers, separated by commas, for a character special, block special, regular, directory or NFS file, or a kernel reference address that identifies the file. It might also show the base address or device name of a Linux AX.25 socket device.
Size/Off: Shows the size of the file or the file offset in bytes.
Node: Shows the node number of a local file, or the inode number of an NFS file in the server host, or internet protocol type. It might display STR for a stream or the IRQ or inode number of a Linux AX.25 socket device.
Name: Shows the name of the mount point and file system on which the file resides.

一先ずはlsofとgrepで関連する用語を表示しながら慣れていこうと思います。