File Permissions และ chmod Command — จัดการสิทธิ์ไฟล์บน Linux

File Permission เป็นกลไกพื้นฐานที่สุดของความปลอดภัยใน Linux ทุกไฟล์และ Directory มีการกำหนดว่าใครสามารถอ่าน, เขียน, หรือ Execute ได้บ้าง การเข้าใจและตั้งค่า Permission อย่างถูกต้องเป็นทักษะที่ขาดไม่ได้สำหรับ System Administrator เพราะ Permission ที่ผิดพลาดสามารถเปิดช่องโหว่ดด้านความปลอดภัยหรือทำให้ Service ทำงานผิดปกติได้

บทความนี้อธิบายระบบ Permission ของ Linux และคำสั่ง chmod สำหรับปรับสิทธิ์ไฟล์ ทั้งในรูปแบบ Symbolic และ Numeric (Octal)

โครงสร้าง File Permission

เมื่อรันคำสั่ง ls -l จะเห็นข้อมูล Permission ในรูปแบบเช่น -rwxr-xr-- ซึ่งแบ่งออกเป็น 4 ส่วนดังนี้

-rwxr-xr--  1  alice  webteam  4096  Apr 16  script.sh
│└──┘└──┘└──┘
│  │   │   │
│  │   │   └─ Other permission (r--)
│  │   └───── Group permission (r-x)
│  └───────── Owner permission (rwx)
└─────────── File type (- = file, d = directory, l = symlink)

สัญลักษณ์แต่ละตัวมีความหมายดังนี้: r (read) = อ่านได้, w (write) = เขียนได้, x (execute) = รันได้ สำหรับ Directory x หมายถึงสามารถ cd เข้าไปได้ และ r หมายถึงสามารถ ls ดูรายการไฟล์ได้

Numeric (Octal) Permission

นอกจากการใช้สัญลักษณ์ r, w, x แล้ว Permission ยังแทนได้ด้วยตัวเลข (Octal) โดยแต่ละ Permission มีค่าดังนี้

  • r = 4
  • w = 2
  • x = 1
  • - (ไม่มีสิทธิ์) = 0

นำค่ามาบวกกันเพื่อได้ตัวเลขสำหรับแต่ละกลุ่ม เช่น rwx = 4+2+1 = 7, r-x = 4+0+1 = 5, r-- = 4+0+0 = 4 ดังนั้น rwxr-xr-- = 754 และ rwxrwxr-x = 775

chmod — เปลี่ยน Permission

chmod (change mode) ใช้เปลี่ยน Permission ของไฟล์และ Directory ทำได้ 2 วิธีคือ Numeric และ Symbolic

Numeric Mode

# ให้ owner rwx, group r-x, other r--
chmod 754 script.sh

# permission ที่ใช้บ่อย
chmod 755 script.sh     # rwxr-xr-x (executable file, web directory)
chmod 644 config.txt    # rw-r--r-- (config file, web file)
chmod 600 private.key   # rw------- (private key, sensitive file)
chmod 777 /tmp/shared   # rwxrwxrwx (shared writable — ระวัง!)
chmod 400 backup.tar    # r-------- (read-only backup)

# ใช้กับ directory
chmod 750 /app/data     # rwxr-x--- (owner+group เข้าได้, other ไม่ได้)

Symbolic Mode

Symbolic Mode ใช้ตัวอักษรแทนกลุ่ม (u=owner, g=group, o=other, a=all) และตัวดำเนินการ (+=เพิ่ม, -=ลบ, ==กำหนด)

# เพิ่ม execute ให้ owner
chmod u+x script.sh

# ลบ write ออกจาก group และ other
chmod go-w sensitive.conf

# กำหนดให้ other อ่านได้อย่างเดียว
chmod o=r document.txt

# เพิ่ม execute ให้ทุกกลุ่ม
chmod a+x deploy.sh

# หลายคำสั่งพร้อมกัน
chmod u+x,go-w script.sh

Recursive Permission Change

# เปลี่ยน permission ทั้ง directory และไฟล์ข้างใน
chmod -R 755 /var/www/html

# ระวัง: -R จะใส่ permission เดียวกันทั้ง directory และไฟล์
# ควรใช้ find เพื่อแยก directory และไฟล์:
find /var/www/html -type d -exec chmod 755 {} \;
find /var/www/html -type f -exec chmod 644 {} \;

Permission ที่ใช้บ่อยใน Production

  • 755 (rwxr-xr-x) — Web Directory, Script ที่ทุกคนรันได้ แต่แก้ไขได้แค่ owner
  • 644 (rw-r--r--) — ไฟล์ทั่วไป เช่น HTML, PHP, Config File ที่ไม่ต้อง execute
  • 600 (rw-------) — ไฟล์ความลับ เช่น SSH Private Key, ไฟล์ที่มี Password
  • 700 (rwx------) — Directory ส่วนตัว, Script ที่ให้ owner รันได้เท่านั้น
  • 640 (rw-r-----) — Config File ที่ Group สามารถอ่านได้ แต่ other ไม่ได้
  • 400 (r--------) — Read-only file เช่น Backup, Certificate ที่ไม่ควรแก้ไข

Special Permissions

นอกจาก rwx มาตรฐานแล้ว ยังมี Special Permission 3 ชนิดที่ต้องรู้จัก

SetUID (SUID) — bit ที่ 4

เมื่อไฟล์ Executable มี SUID set ไฟล์นั้นจะทำงานด้วยสิทธิ์ของ Owner แทนสิทธิ์ของผู้รัน เช่น /usr/bin/passwd มี SUID เพื่อให้ User ทั่วไปเปลี่ยน Password ของตัวเองใน /etc/shadow ได้ (ไฟล์ที่ปกติต้องใช้ root)

# ดู SUID files ในระบบ (s แทน x ใน owner position)
ls -la /usr/bin/passwd    # -rwsr-xr-x

# ตั้ง SUID
chmod u+s /path/to/file
chmod 4755 /path/to/file

# ค้นหา SUID files ทั้งหมด (ควร audit สม่ำเสมอ)
find / -perm -4000 -type f 2>/dev/null

SetGID (SGID) — bit ที่ 2

สำหรับ Directory เมื่อมี SGID ไฟล์ที่สร้างภายใน Directory นั้นจะ inherit Group ของ Directory แทนที่จะใช้ Primary Group ของผู้สร้าง มีประโยชน์มากสำหรับ Shared Directory ของทีม

# ตั้ง SGID บน shared directory
chmod g+s /var/www/html
chmod 2755 /var/www/html

# ผลลัพธ์: ไฟล์ใหม่ใน directory จะใช้ group เดียวกับ directory

Sticky Bit — bit ที่ 1

บน Directory ที่มี Sticky Bit จะทำให้ User ลบได้เฉพาะไฟล์ที่ตัวเองเป็น Owner เท่านั้น แม้ว่า Directory จะ Writable สำหรับทุกคน ตัวอย่างที่คุ้นเคยคือ /tmp ซึ่งทุกคนเขียนได้ แต่ลบไฟล์ของคนอื่นไม่ได้

# ดู sticky bit (t แทน x ใน other position)
ls -la /tmp    # drwxrwxrwt

# ตั้ง sticky bit
chmod +t /shared/uploads
chmod 1777 /shared/uploads

umask — กำหนด Default Permission

umask กำหนด Permission เริ่มต้นที่ถูกหักออกจาก Permission สูงสุด (666 สำหรับไฟล์, 777 สำหรับ Directory) เมื่อสร้างไฟล์ใหม่ umask ค่าเริ่มต้นทั่วไปคือ 022 ทำให้ไฟล์ใหม่ได้ 644 และ Directory ได้ 755

# ดู umask ปัจจุบัน
umask

# ตั้ง umask ชั่วคราว (เฉพาะ session)
umask 027    # ไฟล์ใหม่ได้ 640, directory ได้ 750

# ตั้ง umask ถาวรใน ~/.bashrc หรือ /etc/profile

สรุป

ระบบ Permission ของ Linux ใช้ 3 กลุ่ม (owner, group, other) และ 3 สิทธิ์ (r, w, x) คำสั่ง chmod ใช้ปรับ Permission ทั้งในรูปแบบ Octal (เช่น 755, 644) และ Symbolic (เช่น u+x, go-w) Permission ที่ใช้บ่อยที่สุดในงาน Web Server คือ 755 สำหรับ Directory และ 644 สำหรับไฟล์ ส่วน Special Permission (SUID, SGID, Sticky) มีประโยชน์เฉพาะกรณีและต้องใช้ด้วยความระมัดระวัง

แนะนำบริการ DE

การฝึก File Permission บน Server จริงต้องการ Root Access ที่ยืดหยุ่น Cloud VPS ของ DE ให้คุณทดลองตั้งค่า Permission, SUID, SGID และ Sticky Bit ได้อย่างอิสระ พร้อมฝึกแก้ปัญหา Permission ที่พบในงาน Production จริง

สำหรับผู้ที่ต้องการโฮสต์เว็บไซต์โดยไม่ต้องกังวลเรื่อง File Permission ระดับ System Cloud Hosting ของ DE จัดการ Permission พื้นฐานให้อัตโนมัติผ่าน Control Panel ที่ใช้งานง่าย