จุดดีของ Linux คือสามารถดูและจัดการทุกอย่างได้ ตั้งแต่ boot process จนถึงการติดตั้งซอฟต์แวร์ บทความนี้อธิบายวิธีใช้คำสั่ง lsof ใน Linux เพื่อดูไฟล์ที่เปิดอยู่และ processes ที่ใช้งานอยู่ การรู้วิธีนี้ช่วยเข้าใจการทำงานของระบบ และดำเนินการเฉพาะกับ processes ได้
คำสั่ง Lsof
เพื่อดูไฟล์ที่เปิดอยู่และ users หรือ processes ที่รับผิดชอบ ใช้ utility lsof โดย default lsof ติดตั้งมากับ distributions หลายตัว
หากยังไม่มี ติดตั้งผ่าน package manager ได้
Debian/Ubuntu
sudo apt-get install lsof
Arch/Manjaro
sudo pacman -S lsof
CentOS/REHL/Fedora
sudo dnf install lsof
ใช้ lsof แสดงไฟล์ที่เปิดสำหรับ Linux Process
เหมือนคำสั่ง Linux อื่นๆ lsof ใช้ง่ายมาก เริ่มด้วยคำสั่ง lsof:
sudo lsof
หลังรันคำสั่ง lsof จะคืนข้อมูลเกี่ยวกับไฟล์ที่เปิดอยู่ในระบบ
COMMAND PID TID TASKCMD USER FD TYPE DEVICE SIZE/OFF NODE NAME init 1 root cwd DIR 8,48 4096 2 / init 1 root rtd DIR 8,48 4096 2 / init 1 root txt REG 0,19 632048 281474976743906 /init init 1 root 0u CHR 1,3 0t0 15362 /dev/null init 1 root 1u CHR 1,3 0t0 15362 /dev/null init 1 root 2u CHR 1,3 0t0 15362 /dev/null init 1 root 3w CHR 1,11 0t0 15367 /dev/kmsg init 1 root 4u sock 0,8 0t0 22689 protocol: AF_VSOCK init 1 root 5r REG 0,4 0 4026532185 mnt init 1 root 6r REG 0,4 0 4026532201 mnt init 1 root 7r DIR 8,48 4096 240 /home/cap init 1 root 8u DIR 8,48 4096 2 / init 1 root 9u sock 0,8 0t0 21853 protocol: AF_VSOCK
หมายเหตุ: หากมี sudo privileges รันคำสั่งกับ sudo เพื่อหลีกเลี่ยง "permission denied" บนไฟล์บางไฟล์
จาก output ข้างต้น lsof มีคอลัมน์ดังนี้:
| Column | Representation |
|---|---|
| Command | แสดงชื่อ process ที่ใช้ target file |
| PID | Unique identifier ของ process ที่ใช้ไฟล์ |
| TID | Thread identifier |
| TASKCMD | ชื่อ task command |
| USER | Username หรือ UID ของผู้ใช้ที่รัน process |
| FD | File descriptor ของไฟล์และ modes |
| TYPE | Node ที่เชื่อมโยงกับ target file |
| DEVICE | Device number คั่นด้วยคอมม่า |
| SIZE/OFF | ขนาดไฟล์เป็นไบต์ หรือ file offset size |
| NODE | Inode value ของ local file ใช้ stat command ดู inode info ได้ |
| NAME | Mount point ของไฟล์ |
ตอนนี้เข้าใจคอลัมน์แล้ว มาใช้คำสั่งกรองข้อมูลเฉพาะเจาะจง
กรองสำหรับ Process เฉพาะ
เพื่อกรองเฉพาะไฟล์ที่ process เปิดอยู่ ใช้ process name หรือ PID
ตัวอย่าง แสดงไฟล์ที่ firefox process ใช้:
sudo lsof -c firefox
คำสั่งจะแสดงไฟล์ทั้งหมดที่ firefox process เปิดอยู่
กรองด้วย process ID ใช้ -p option ส่ง PID ใช้ top หา PID ของ target process
ตัวอย่าง หา PID ของ firefox:
sudo ps aux | grep firefox
ได้ PID แล้ว ใช้ lsof แสดงไฟล์ที่เปิด:
sudo lsof -p 2121
คำสั่งจะพิมพ์ไฟล์ที่ process ด้วย PID ที่ระบุเปิดอยู่
กรองสำหรับ User เฉพาะ
ดูเฉพาะไฟล์ที่ user เฉพาะเปิด ใช้ flag -u ตัวอย่าง กรอง user debian:
sudo lsof -u debian
กรองสำหรับไฟล์เฉพาะ
หากต้องการรู้แค่ process และ user ที่เปิดไฟล์เฉพาะ ส่งชื่อไฟล์ให้ lsof:
sudo lsof /bin/sleep
จะกรองเฉพาะไฟล์นั้นและคืนข้อมูลที่เกี่ยวข้อง รวมถึง user, process ID และอื่นๆ
สรุป
ใน tutorial ง่ายนี้ เราอธิบายวิธี query ระบบเพื่อข้อมูลไฟล์ที่เปิดอยู่ด้วย lsof command ใน Linux นี่คือคำสั่งเพิ่มเติมสำหรับ list เนื้อหา directory ใน terminal