TExcelTAD
Home › Linux & Bash › chmod & chown

chmod & chown

Every file on Linux has an owner, a group and a set of permissions that say who may read, write or execute it. chmod (change mode) changes the permissions; chown (change owner) changes who owns the file. This lesson is about using the two commands; the Permissions Model lesson later explains the theory in depth.

Reading a permission string

bash · try it yourself
printf 'echo hi\n' > deploy.sh
printf 'secret=42\n' > api.key
stat -c '%A %U %G %n' deploy.sh api.key
-rw-r--r-- learner learner deploy.sh
-rw-r--r-- learner learner api.key

-rw-r--r-- reads as: a regular file (-), then three groups of rwx for user (owner), group and others. Here the owner can read and write, everyone else can only read. New files get these defaults from the umask (022 here).

Symbolic mode: who, +/-, what

The readable way to use chmod is WHO OPERATOR PERMISSIONS:

Part Values
Who u user/owner, g group, o others, a all
Operator + add, - remove, = set exactly
Permissions r read, w write, x execute
bash · try it yourself
printf 'echo deploying\n' > deploy.sh
printf 'secret=42\n' > api.key
chmod u+x deploy.sh
stat -c '%A %n' deploy.sh
chmod go-r api.key
stat -c '%A %n' api.key
chmod a=r api.key
stat -c '%A %n' api.key
chmod u+w,g=rw,o= api.key
stat -c '%A %n' api.key
-rwxr--r-- deploy.sh
-rw------- api.key
-r--r--r-- api.key
-rw-rw---- api.key

Several changes are separated by commas, without spaces. chmod +x file (no "who") adds execute for everyone, minus what the umask blocks.

Numeric (octal) mode

Each permission has a value: r = 4, w = 2, x = 1. Add them per group and write three digits:

Digits String Typical use
644 rw-r--r-- Normal files
600 rw------- Private files: SSH keys, .env
755 rwxr-xr-x Scripts, programs, directories
700 rwx------ Private scripts or directories
664 rw-rw-r-- Files shared with your group
bash · try it yourself
touch notes.txt run.sh id_ed25519
chmod 644 notes.txt
chmod 755 run.sh
chmod 600 id_ed25519
stat -c '%a %A %n' notes.txt run.sh id_ed25519
644 -rw-r--r-- notes.txt
755 -rwxr-xr-x run.sh
600 -rw------- id_ed25519

stat -c %a prints the octal form, %A the string form. Both describe the same thing.

Permissions really are enforced

bash · try it yourself
printf 'balance=100\n' > ledger.txt
chmod 444 ledger.txt
echo "balance=0" > ledger.txt
echo "write status: $?"
chmod 000 ledger.txt
cat ledger.txt
chmod 644 ledger.txt
cat ledger.txt
/w/main.sh: line 3: ledger.txt: Permission denied
write status: 1
cat: ledger.txt: Permission denied
balance=100

Read-only files can't be overwritten, and without r you can't even read your own file. As the owner you can always chmod it back, though. (The administrator, root, bypasses these checks.)

Recursive changes

chmod -R changes a whole tree. Careful: files and directories usually need different modes. The capital X adds execute only to directories (and files that are already executable), which is usually what you want:

bash · try it yourself
mkdir -p site/css
touch site/index.html site/css/main.css
chmod -R go-rwx site
chmod -R u=rwX,go=rX site
stat -c '%A %n' site/index.html site/css/main.css
-rw-r--r-- site/index.html
-rw-r--r-- site/css/main.css

chown: changing the owner

Only root can give a file to another user, so on a real system chown is usually run with sudo. The syntax is chown USER[:GROUP] FILE:

bash
sudo chown www-data /var/www/html/index.html        # new owner
sudo chown www-data:www-data /var/www/html/index.html  # owner and group
sudo chown -R deploy:deploy /srv/myapp                # a whole tree
sudo chgrp developers report.txt                       # only the group

A typical web-server example: the files are owned by a deploy user who uploads them, and the www-data group (the web server) may only read them: chown -R deploy:www-data /srv/site and chmod -R u=rwX,g=rX,o= /srv/site.

As a normal user you may only change the group of your own files to a group you belong to. Here learner is only in the learner group, so this works but giving the file away does not:

bash · try it yourself
touch report.txt
chown learner:learner report.txt
stat -c '%U:%G %n' report.txt
chown root report.txt
learner:learner report.txt
chown: changing ownership of 'report.txt': Operation not permitted

Common mistakes

  • chmod 777 "to make it work". It gives everyone write access. Find out which user needs access and grant exactly that.
  • Making data files executable with chmod -R 755. Use u=rwX,go=rX instead.
  • SSH refuses keys that others can read: private keys must be 600.

Test yourself

What permission string does chmod 750 tool produce?

  1. rwxr-x---
  2. rwxr-xr-x
  3. rw-r-----
  4. rwxrw----

Which command removes write permission for the group and others?

  1. chmod go+w f
  2. chmod go-w f
  3. chmod u-w f
  4. chmod 777 f

Why does chown alice file fail for a normal user?

  1. The syntax is wrong
  2. Only root may give files to other users
  3. alice must be logged in
  4. chown only works on directories