Permissions and Ownership
chmod, chown, rwx bits, sudo — understanding who can do what with files
Skills taught: linux.chmod, linux.chown, linux.sudo, linux.rwx, linux.octal-permissions
Understanding permission bits
ls -l deploy.sh
-rwxr-xr-x 1 alice developers 4096 Oct 6 deploy.sh
Permissions: -rwxr-xr-x
Position 1: - = regular file, d = directory, l = symlink
Positions 2-4: rwx = owner permissions
Positions 5-7: r-x = group permissions
Positions 8-10: r-x = others permissions
r = read (value: 4)
w = write (value: 2)
x = execute (value: 1)
- = no permission (value: 0)
Octal (numeric) notation:
rwx = 4+2+1 = 7
r-x = 4+0+1 = 5
r-- = 4+0+0 = 4
--- = 0+0+0 = 0
Common permission sets:
755 = rwxr-xr-x (executable: owner full, others read+execute)
644 = rw-r--r-- (file: owner read+write, others read only)
600 = rw------- (private file: owner only — use for SSH keys)
700 = rwx------ (private directory: owner only)
chmod — change permissions
# Numeric (octal) mode
chmod 755 deploy.sh # rwxr-xr-x
chmod 644 config.txt # rw-r--r--
chmod 600 ~/.ssh/id_rsa # rw------- (SSH private key must be 600)
chmod -R 755 /var/www/ # -R: apply recursively to directory
# Symbolic mode
chmod +x deploy.sh # add execute for everyone
chmod -w notes.txt # remove write for everyone
chmod u+x,g-w script.sh # user add execute, group remove write
chmod o-rwx secret.txt # remove all permissions for others
# Make a script executable (most common use)
chmod +x myscript.sh
./myscript.sh
sudo and chown
# sudo: run a command as the superuser (root)
sudo apt install nginx # install with admin rights
sudo systemctl start nginx # start a service
sudo nano /etc/hosts # edit a system file
# Become root (use sparingly)
sudo -i
# chown: change ownership
sudo chown alice file.txt # change owner to alice
sudo chown alice:developers dir/ # owner:group
sudo chown -R alice:alice /home/alice/ # recursive
Tip: SSH keys must be 600. If your SSH private key has permissions looser than 600 (rw-------), SSH will refuse to use it with 'Permissions too open'. Fix: chmod 600 ~/.ssh/id_rsa
Frequently Asked Questions
When do I need sudo?
When modifying system files (/etc, /var, /usr), installing packages, managing services (systemctl), or changing ownership of files not owned by you. On your own home directory and files, you do not need sudo. The rule: try without sudo first. If you get 'Permission denied', then consider if sudo is appropriate — don't sudo everything reflexively.