How do I change file permissions in Linux?
Symbolic and numeric modes
chmod changes permissions. Symbolic mode uses letters: u (user), g (group), o (others), a (all), and operators +, -, =. For example, chmod u+x file adds execute for the owner. Numeric mode uses octal digits: 4=read, 2=write, 1=execute. So chmod 755 file gives rwxr-xr-x.
You can combine symbolic modes: chmod u=rw,g=r,o= file sets exact permissions. The -R flag applies changes recursively to directories, but be cautious.
- chmod 644 file — owner read/write, others read
- chmod 755 script.sh — owner all, others read/execute
- chmod u+x file — add execute for owner
- chmod -R 750 directory — recursive change
- chmod --reference=file1 file2 — copy permissions from file1
Special permissions and ownership
Beyond the basic nine bits, there are setuid, setgid, and sticky bits. Setuid (4xxx) makes a program run as its owner; setgid (2xxx) on a directory makes new files inherit the group; sticky (1xxx) on a directory restricts deletion to file owners.
To change ownership, use chown user:group file. Only root can change ownership to another user. chgrp changes just the group. Permissions are checked in order: owner, group, others.
Common mistakes
- Using chmod -R 777 to fix permission issues: this gives everyone full access and is a security risk.
- Forgetting that execute permission on a directory means you can enter it (cd) and access files, not run it as a program.
- Assuming chmod affects symlinks: chmod changes the target file, not the symlink itself.
