chmod – change file permissions

chmod command
chmod command

chmod changes the access permissions of files and directories. Permissions control who can read, write, or execute a file.

Synopsis

chmod [OPTIONS] MODE FILE...
chmod [OPTIONS] OCTAL FILE...

Permission Basics

Each file has three permission sets:

  • User (u) — the file owner
  • Group (g) — members of the file’s group
  • Others (o) — everyone else

Each set can have:

  • r (read) — view contents
  • w (write) — modify contents
  • x (execute) — run as program / enter directory

Common Options

OptionDescription
-RRecursive (apply to directories and contents)
-vVerbose (show files being changed)
-cReport only when changes are made
--reference=FILECopy permissions from another file

Symbolic Mode

Use letters to add (+), remove (-), or set (=) permissions:

CommandMeaning
chmod u+x fileAdd execute for user
chmod g-w fileRemove write for group
chmod o=r fileSet others to read only
chmod a+x fileAdd execute for all (user, group, others)
chmod ug+rw fileAdd read/write for user and group

Octal Mode

Permissions can be set using octal numbers (0-7):

NumberPermissionMeaning
0---No permissions
1--xExecute only
2-w-Write only
3-wxWrite and execute
4r--Read only
5r-xRead and execute
6rw-Read and write
7rwxRead, write, execute

Common Octal Permissions

ModePermissionsUse Case
755rwxr-xr-xExecutables, public directories
644rw-r--r--Regular files
700rwx------Private executables
600rw-------Private files (SSH keys)
777rwxrwxrwxFull access (avoid!)

Examples

Make a script executable

$ chmod +x script.sh
$ ls -l script.sh
-rwxr-xr-x 1 greys staff 1024 Jan 15 10:00 script.sh

Set specific permissions with octal

$ chmod 644 document.txt
$ ls -l document.txt
-rw-r--r-- 1 greys staff 2048 Jan 15 10:00 document.txt

Secure SSH private key

$ chmod 600 ~/.ssh/id_rsa

Make directory accessible to group

$ chmod 775 shared_folder/

Apply recursively to directory

$ chmod -R 755 /var/www/html/

Remove all permissions for others

$ chmod o-rwx private_file.txt

Copy permissions from another file

$ chmod --reference=file1.txt file2.txt

Add execute for directories only (recursive)

$ find /path -type d -exec chmod +x {} \;

Special Permissions

ModeNameEffect
4xxxsetuidRun as file owner
2xxxsetgidRun as file group / inherit group in dirs
1xxxstickyOnly owner can delete (e.g., /tmp)
# Set sticky bit on directory
$ chmod 1777 /tmp

# Set setuid on executable
$ chmod 4755 /usr/bin/passwd

Tips

  • Check before changing: ls -l file to see current permissions
  • Directories need +x to be entered (cd into them)
  • Avoid 777 — it’s a security risk
  • SSH keys must be 600 or SSH will refuse to use them
  • Web files typically need 644 (files) and 755 (directories)

See Also

  • chown — Change file owner and group
  • ls — List files (use -l to see permissions)
  • umask — Set default permissions for new files

Tutorials