SyntaxStudy
Sign Up
Linux / Bash Changing Permissions with chmod and Ownership with chown
Linux / Bash Beginner 1 min read

Changing Permissions with chmod and Ownership with chown

The `chmod` command changes file permissions. It accepts two syntaxes: octal notation, where you specify the full permission set as a three-digit number (e.g., `chmod 755 file`), and symbolic notation, where you add or remove specific bits using letters (e.g., `chmod u+x file` adds execute for the owner, `chmod o-w file` removes write for others). The `chown` command changes the owning user and group. It requires root or sudo privileges to change ownership away from yourself. The syntax is `chown user:group file`. You can change only the user (`chown alice file`), only the group (`chown :developers file`), or both together. With `-R` it applies recursively to all files in a directory tree. The `chgrp` command changes only the group ownership and is a convenience wrapper around `chown :group`. These ownership commands are frequently needed when setting up web server directories, deploying applications, or configuring shared project folders where multiple team members need collaborative access.
Example
# ---- chmod: octal notation ----
chmod 755 script.sh        # rwxr-xr-x
chmod 644 config.conf      # rw-r--r--
chmod 600 ~/.ssh/id_rsa    # rw------- (private key)
chmod 700 ~/.ssh/          # rwx------ (ssh directory)

# Apply recursively to a directory tree
chmod -R 755 /var/www/html/

# ---- chmod: symbolic notation ----
chmod u+x script.sh        # Add execute for owner
chmod g+w shared.txt       # Add write for group
chmod o-r private.txt      # Remove read for others
chmod a+r public.txt       # Add read for all (a = ugo)
chmod u=rw,g=r,o= file.txt # Set exact permissions with =
chmod +x deploy.sh         # Add execute for everyone (shorthand)

# ---- chown: change owner ----
sudo chown alice file.txt               # Change owner only
sudo chown alice:developers file.txt    # Change owner and group
sudo chown :www-data /var/www/html/     # Change group only
sudo chown -R alice:alice /home/alice/  # Recursive ownership change

# ---- chgrp: change group ----
sudo chgrp developers project/
sudo chgrp -R www-data /var/www/

# Verify changes
ls -la file.txt
stat file.txt