Chmod Calculator: Understanding Unix File Permissions
In Unix and Linux-based systems, file permissions are a critical aspect of security and system administration. The chmod command (change mode) is the tool used to manage who can read, write, or execute a file or directory. While the system is logical, the numeric and symbolic representations can be confusing for beginners and even some seasoned developers.
In this guide, we'll explain how Unix permissions work, the difference between octal and symbolic modes, and provide a comprehensive reference table.
The Basics of Unix Permissions
Every file and directory in a Linux system has three types of permissions for three different classes of users.
The Three Classes
- User (u): The owner of the file.
- Group (g): Members of the group that the file belongs to.
- Others (o): All other users on the system.
The Three Permissions
- Read (r): Permission to view the contents of a file or list the contents of a directory.
- Write (w): Permission to modify a file or add/remove files in a directory.
- Execute (x): Permission to run a file as a program/script or enter a directory.
Octal (Numeric) Mode Explained
Octal mode uses three digits to represent permissions. Each digit is the sum of the values of the permissions for that class.
- Read (r) = 4
- Write (w) = 2
- Execute (x) = 1
- No Permission = 0
How to calculate a digit:
Read (4) + Write (2) + Execute (1) = 7(Full permissions)Read (4) + Write (2) = 6(Read and Write)Read (4) + Execute (1) = 5(Read and Execute)Read (4) = 4(Read only)
Example: chmod 755
- User: 7 (4+2+1) -> rwx
- Group: 5 (4+1) -> r-x
- Others: 5 (4+1) -> r-x
Symbolic Mode Explained
Symbolic mode uses letters to represent the classes and the permissions.
- Classes:
u(user),g(group),o(others),a(all) - Operations:
+(add),-(remove),=(set exactly) - Permissions:
r,w,x
Example: chmod u+x script.sh
This adds execute permission to the owner of the file.
Chmod Reference Table
| Octal | Symbolic | Description | Common Use Case |
|---|---|---|---|
| 777 | a+rwx |
Everyone can do everything | Public folders (Use with caution!) |
| 755 | u=rwx,go=rx |
Owner can do everything, others can read/execute | Web server directories, Scripts |
| 644 | u=rw,go=r |
Owner can read/write, others can only read | Regular text files, HTML files |
| 600 | u=rw,go= |
Only owner can read/write | Private keys, SSH keys |
| 400 | u=r,go= |
Only owner can read | Sensitive config files |
| 700 | u=rwx,go= |
Only owner can do everything | Private scripts or directories |
Common Use Cases and Examples
Making a script executable
chmod +x my-script.sh
# or
chmod 755 my-script.sh
Securing an SSH private key
SSH keys will not work if they are too accessible. They must be restricted to the owner only.
chmod 600 ~/.ssh/id_rsa
Changing permissions recursively
To change the permissions of a directory and all its contents:
chmod -R 755 /path/to/directory
Special Permissions: SUID, SGID, and Sticky Bit
Advanced Linux systems use a fourth leading digit for special permissions:
- SUID (4): When the file is executed, the process runs with the privileges of the owner.
- SGID (2): Process runs with the privileges of the group, or files created in the directory inherit the group.
- Sticky Bit (1): Only the owner of a file in a directory can delete or rename it (often used for
/tmp).
Example: chmod 1777 /tmp (Full access for everyone, but only owners can delete their files).
Frequently Asked Questions (FAQ)
What does "755" mean exactly?
It means the owner has Read, Write, and Execute (4+2+1=7) permissions. The group and others have Read and Execute (4+1=5) permissions.
Why is 777 considered dangerous?
777 allows any user on the system to modify or delete your files. In a web environment, this could allow an attacker to upload malicious scripts and execute them.
How do I check the current permissions of a file?
Use the ls -l command in your terminal. You will see a string like -rwxr-xr-x, where the first character is the file type and the next 9 characters represent the permissions.
Conclusion
Understanding file permissions is vital for maintaining a secure and functional Linux environment. While numeric codes like 644 or 755 are standard, using an online calculator can help you visualize exactly what each code represents.
Need to find the right permission code quickly? Use our Chmod Calculator (coming soon) to generate the perfect command for your needs.