← Learning Hub
Beginner

Users and Permissions

Every file has an owner, a group, and permissions for three kinds of people: the owner (u), the group (g) and everyone else (o). Each can have read, write and execute (run) permission.

Linux is a multi-user system, so every file says who may read, write or run it. Correct permissions keep secrets private and stop programs from changing things they shouldn't. Most "Permission denied" errors are fixed by understanding these three letters.

Example
$ echo 'echo "backup done"' > backup.sh
$ stat -c '%A %n' backup.sh
-rw-r--r-- backup.sh
$ ./backup.sh
bash: ./backup.sh: Permission denied
$ chmod u+x backup.sh
$ stat -c '%A %n' backup.sh
-rwxr--r-- backup.sh
$ ./backup.sh
backup done

-rw-r--r-- means: a normal file (-), owner can read and write, group can read, others can read. After chmod u+x the owner can also run it. (ls -l shows the same letters, plus the owner, size and date.)

Number form

Example
$ touch secret.env
$ chmod 600 secret.env
$ stat -c '%A %a %n' secret.env
-rw------- 600 secret.env
$ chmod 644 secret.env
$ stat -c '%A %a %n' secret.env
-rw-r--r-- 644 secret.env
  • r = 4, w = 2, x = 1. Add them for each of owner, group, others
  • 600 - only the owner can read and write (good for secret files like .env)
  • 644 - owner writes, everyone reads (normal files)
  • 755 - owner writes, everyone can read and run (scripts, folders)

sudo: running as administrator

Example
sudo apt update              # sudo runs one command as the 'root' administrator
sudo chown asha:asha app.log # change a file's owner and group
Don't fix permission errors with chmod 777 (everyone can do everything). Give the right user just the access it needs.
Key points
  • r, w, x for owner, group and others.
  • chmod changes permissions; chown changes owner.
  • Use 600 for secrets, never 777.

📝 Test yourself

1. What does chmod 600 give?
2. What does sudo do?
3. Fill in the command to make a script runnable by its owner:
chmod backup.sh