Chmod Cheat Sheet
Quick reference for Unix file permissions: octal notation, symbolic notation, special bits, and recursive command patterns.
Permission values
| Symbol | Value | Effect |
|---|---|---|
r | 4 | Read — view file contents or list directory |
w | 2 | Write — modify file or add/remove directory entries |
x | 1 | Execute — run file or enter (cd into) directory |
- | 0 | Permission denied |
Common octal modes
| Octal | Symbolic | Use case |
|---|---|---|
700 | rwx------ | Private executable (only owner) |
755 | rwxr-xr-x | Public executable / directory |
644 | rw-r--r-- | Public file (HTML, CSS, configs) |
600 | rw------- | Private file (SSH keys, secrets) |
666 | rw-rw-rw- | World-writable file (rare, risky) |
777 | rwxrwxrwx | World-everything (almost never correct) |
400 | r-------- | Read-only for owner (locked file) |
Symbolic notation
| Pattern | Effect |
|---|---|
chmod u+x file | Add execute for owner |
chmod g-w file | Remove write for group |
chmod o=r file | Set others to read-only |
chmod a+r file | Add read for all (a = u+g+o) |
chmod ug+w file | Add write for owner and group |
chmod -R u+w dir/ | Recursive — apply to all contents |
chmod +X dir/ | Add execute only on directories and already-executable files |
Special bits (4-digit octal)
| Bit | Octal | Effect |
|---|---|---|
| setuid | 4xxx | Run executable with owner's privileges (e.g., 4755) |
| setgid | 2xxx | Run with group's privileges, OR new files in directory inherit group |
| sticky | 1xxx | Only file owner can delete (used on /tmp, e.g., 1777) |
In ls -l output, special bits show as s, S, t, or T in the execute column.
Reading ls -l output
-rwxr-xr-x 1 alice staff 4096 May 6 12:34 script.sh └┬┘└┬┘└┬┘└┬┘ │ │ │ └─ others permissions (r-x = read, execute) │ │ └──── group permissions (r-x = read, execute) │ └─────── owner permissions (rwx = read, write, execute) └────────── file type: - regular file, d directory, l symlink, b/c device
Recursive permission fixes
| Goal | Command |
|---|---|
| Files 644, dirs 755 (websites) | find . -type f -exec chmod 644 {} + then find . -type d -exec chmod 755 {} + |
| Add x only on directories | chmod -R +X . |
| Lock everything down | chmod -R go= . |
| SSH config dir | chmod 700 ~/.ssh && chmod 600 ~/.ssh/* |
Visual chmod calculator: Stax Chmod Calculator.
Chmod Cheat Sheet — คู่มืออ้างอิง Unix File Permission สำหรับนักพัฒนาและ System Administrator
File permission เป็นหนึ่งในแนวคิดพื้นฐานในการดูแลระบบ Unix และ Linux — แต่ octal notation ทำให้แม้แต่นักพัฒนาที่มีประสบการณ์สับสนเมื่อพบไม่บ่อย ไม่ว่าคุณจะกำลัง deploy web application บน Linux server และต้องตั้ง permission ที่ถูกต้องบน config file ทำงานกับ CI/CD pipeline ที่ต้องการ execute permission บน shell script หรือ hardening SSH setup ที่ต้องการ permission 600 ที่เข้มงวดบน private key คู่มืออ้างอิงนี้ให้คำสั่งและค่าที่แน่นอนที่คุณต้องการโดยไม่ต้องค้นหาใน man page
ตาราง permission ด้านล่างครอบคลุม: ค่าตัวเลขสำหรับ permission แต่ละประเภท (r=4, w=2, x=1), octal mode ที่พบบ่อยที่สุดพร้อม symbolic equivalent และกรณีการใช้งานที่แนะนำ, syntax ของ symbolic notation สำหรับการเปลี่ยนแปลงแบบ relative โดยไม่รีเซ็ต permission ทั้งหมด, special bit (setuid, setgid, sticky bit) พร้อม octal prefix, วิธีอ่าน output ของ ls -l และคำสั่ง recursive permission ที่ถูกต้องสำหรับสถานการณ์ทั่วไปเช่น web server deployment แต่ละแถวคือการอ้างอิงโดยตรง — ค้นหาสถานการณ์ แล้วคัดลอกคำสั่ง
755 (rwxr-xr-x) คือ permission ที่ถูกต้องสำหรับไฟล์ที่รันได้ script และ directory ที่ควรอ่านและรันได้โดยสาธารณะแต่เขียนได้เฉพาะ owner นี่คือสิ่งที่คุณต้องการบน directory ของ web server และ shell script ส่วนใหญ่ 644 (rw-r--r--) ถูกต้องสำหรับไฟล์ปกติเช่น HTML, CSS, JavaScript และ config file — อ่านได้โดยทุกคน เขียนได้เฉพาะ owner 600 (rw-------) สำหรับไฟล์ส่วนตัวที่ละเอียดอ่อนเช่น SSH private key, .env file และ credential file — มีเพียง owner เท่านั้นที่อ่านหรือเขียนได้ 700 (rwx------) สำหรับไฟล์ที่รันได้ส่วนตัวหรือ directory ที่ควรซ่อนจากผู้ใช้อื่นอย่างสมบูรณ์ ห้ามใช้ 777 ใน production เด็ดขาด — มันให้สิทธิ์เขียนและรันแก่ทุก user บนระบบและเป็นช่องโหว่ความปลอดภัยที่ร้ายแรง
นักพัฒนาเว็บที่ deploy บน Linux server ใช้เพื่อตั้ง permission ที่ถูกต้องบนไฟล์ application ที่อัปโหลดและตรวจสอบให้แน่ใจว่า web server process อ่านได้แต่เขียน config file ไม่ได้ DevOps engineer ใช้เมื่อเขียน Dockerfile หรือ Ansible playbook ที่ตั้ง file permission เป็นส่วนหนึ่งของการตั้งค่า infrastructure System administrator ใช้สำหรับรูปแบบการแก้ไข recursive permission เมื่อกู้คืน permission ที่ถูกต้องหลังจากคำสั่ง chmod -R ที่ผิดพลาด Security auditor ใช้เพื่อยืนยันว่าไฟล์ที่ละเอียดอ่อน (private key, database credential, API token) มี permission ที่จำกัดเพียงพอในระหว่างการ review การ hardening server
นี่คือหน้าอ้างอิงแบบ static — ไม่มีการเก็บข้อมูล input ของผู้ใช้ และไม่มีการส่งข้อมูลไปยังเซิร์ฟเวอร์ใด
คำถามที่พบบ่อย
- What's the difference between 755 and 644?
- 755 (rwxr-xr-x) — owner can read/write/execute, group and others can read/execute. Used for executable files and directories. 644 (rw-r--r--) — owner can read/write, group and others can only read. Used for regular files (HTML, CSS, images, configs).
- Why does 777 work but everyone says don't use it?
- 777 grants read/write/execute to everyone. It works in the sense that the file becomes accessible by all processes, but it's a security catastrophe — any compromised user or process can modify the file. Use 644 for files, 755 for executables, 700 for private data, 600 for SSH keys. Reserve 777 only for /tmp-style world-writable directories that intentionally need it.
- What's the difference between numeric and symbolic chmod?
- Numeric (chmod 755) sets all 9 permission bits in one shot. Symbolic (chmod u+x file) modifies specific bits without touching others. Use numeric for absolute permissions, symbolic for relative changes. Both produce the same final state — pick whichever reads cleaner for your case.
- Why doesn't chmod -R work as expected on directories?
- Recursive chmod applies the SAME permissions to all files and directories — but executable bit on a regular file is rarely what you want. Use find: 'find . -type f -exec chmod 644 {} +' for files, 'find . -type d -exec chmod 755 {} +' for directories. Or chmod's symbolic +X (capital X) which only sets execute on directories and already-executable files.
- Why does sudo chmod fail to change /tmp?
- Some directories have the sticky bit set (last digit 1, e.g., 1777). The sticky bit on /tmp means only the owner of a file can delete it, regardless of directory permissions. This prevents users from deleting each other's temp files. To remove sticky bit: chmod -t /path. To set sticky: chmod +t /path or chmod 1755 /path.
เครื่องมือที่เกี่ยวข้อง
- JSON Formatter, Validator & Repair Tool
Format, minify, validate, and repair JSON instantly in your browser. Sort keys alphabetically, auto-format on paste, download as file, escape/unescape strings — free, no sign-up, 100% client-side.
- ตัวสร้าง QR Code
สร้าง QR code สำหรับ URL ข้อความ Wi-Fi และอื่นๆ ดาวน์โหลดเป็น PNG
- ตัวสร้างรหัสผ่าน
สร้างรหัสผ่านสุ่มที่แข็งแกร่งด้วยความยาวและชุดอักขระที่กำหนดเอง
- Base64 เข้ารหัส / ถอดรหัส
เข้ารหัสข้อความเป็น Base64 หรือถอดรหัสกลับเป็นข้อความธรรมดา
- URL เข้ารหัส / ถอดรหัส
เข้ารหัสหรือถอดรหัส URL และ query strings ด้วย percent-encoding