Terminal GuideTerminal Guide

chmod Command Guide

The chmod (change mode) command controls file permissions in Linux. Learn how to manage read, write, and execute permissions effectively.

7 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Symbolic

chmod u+x fileAdd execute for owner
chmod g-w fileRemove write for group
chmod o=r fileSet read only for others

Octal

chmod 755 filerwxr-xr-x (executable)
chmod 644 filerw-r--r-- (normal file)
chmod 600 filerw------- (private)

Common

chmod +x script.shMake executable
chmod -R 755 dir/Recursive change
chmod a+r fileReadable by all

Special

chmod u+s fileSet SUID bit
chmod g+s dirSet SGID bit
chmod +t dirSet sticky bit

Downloadable Image Preview

Failed to generate preview

Understanding Permissions

Linux file permissions are divided into three categories for three groups:

Permission Types

r (read)View file contents or list directory
w (write)Modify file or create/delete in directory
x (execute)Run file or access directory

Permission Groups

u (user)The file owner
g (group)Users in the file group
o (others)All other users
a (all)All users (u, g, and o)

View current permissions with ls -l:

bash
ls -l file.txt
-rw-r--r-- 1 user group 1024 Jan 15 10:30 file.txt

Symbolic Mode

Symbolic mode uses letters to modify permissions.

Add permission (+)

bash
# Add execute for owner
chmod u+x script.sh

# Add read for group
chmod g+r file.txt

# Add write for others
chmod o+w file.txt

Remove permission (-)

bash
# Remove write from group
chmod g-w file.txt

# Remove all permissions for others
chmod o-rwx file.txt

Set exact permission (=)

bash
# Set exact permissions for user
chmod u=rwx file.txt

# Set read-only for all
chmod a=r file.txt

Combined operations

bash
# Multiple changes at once
chmod u+x,g-w,o-rwx file.txt

# Same permission for multiple groups
chmod ug+x file.txt

Octal (Numeric) Mode

Octal mode uses numbers to set all permissions at once.

Octal Values

4Read (r)
2Write (w)
1Execute (x)
0No permission

Add the values for each permission group (owner, group, others):

bash
# 755 = rwxr-xr-x (owner: all, group/others: read+execute)
chmod 755 script.sh

# 644 = rw-r--r-- (owner: read+write, group/others: read)
chmod 644 file.txt

# 700 = rwx------ (owner: all, group/others: none)
chmod 700 private.txt

# 777 = rwxrwxrwx (everyone: all) - Use with caution!
chmod 777 file.txt

Common Octal Permissions

755rwxr-xr-x - Standard for executables
644rw-r--r-- - Standard for files
700rwx------ - Private executables
600rw------- - Private files
775rwxrwxr-x - Group writable
664rw-rw-r-- - Group writable files
Warning
Avoid using chmod 777 as it gives everyone full access. Use the minimum permissions necessary.

Recursive Changes

Use -R to change permissions recursively.

bash
# Change all files in a directory
chmod -R 755 /path/to/directory

# Make all scripts executable
chmod -R u+x scripts/
Tip
When applying permissions recursively, consider using find to target only files or directories:
bash
# Set 755 for directories only
find /path -type d -exec chmod 755 {} \;

# Set 644 for files only
find /path -type f -exec chmod 644 {} \;

Practical Examples

Make a script executable

bash
chmod +x script.sh
# or
chmod 755 script.sh

Protect sensitive files

bash
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

Web server permissions

bash
# HTML files readable by all
chmod 644 /var/www/html/*.html

# CGI scripts executable
chmod 755 /var/www/cgi-bin/*.cgi

Shared directory for a group

bash
chmod 775 /shared/project
chmod g+s /shared/project  # Set group ID bit

Special Permissions

Special Permission Bits

setuid (4000)Run as file owner
setgid (2000)Run as file group
sticky (1000)Only owner can delete
bash
# Set setuid
chmod u+s file
chmod 4755 file

# Set setgid on directory
chmod g+s directory
chmod 2755 directory

# Set sticky bit (common on /tmp)
chmod +t directory
chmod 1755 directory
Info
The sticky bit on a directory prevents users from deleting files they don't own, even if they have write permission on the directory.

Summary

Understanding chmod is essential for Linux system administration. Key takeaways:

  • Use symbolic mode (u+x) for simple changes
  • Use octal mode (755) for setting complete permissions
  • Common patterns: 755 for scripts, 644 for files, 700/600 for private
  • Use -R for recursive changes
  • Always use minimum necessary permissions for security

Related Articles