Ravindra BagaleCourses & study guides मराठी Track your progress

Linux File Permissions: chmod, chown, chgrp, umask, SUID, SGID and Sticky Bit Explained

Every file and folder in Linux has an owner, a group and three sets of rights: read, write and execute for the owner, the group and everyone else. In this chapter you learn to read ls -l, change rights with chmod (numbers and letters), change owners with chown and chgrp, set defaults with umask, and use the special bits SUID, SGID and sticky.

What you'll learn in this chapter

  • Read permissions in ls -l and stat
  • Know what r, w, x mean for files and for folders
  • Use chmod with numbers (755) and letters (u+x)
  • Change ownership with chown and chgrp
  • Control default rights with umask
  • Use SUID, SGID and the sticky bit safely

1. An everyday example: a family photo album

Why do we need this?

Aai keeps the family album. She can look at it, add photos and remove them. The family (Baba, kids) can look at it and add photos, but not remove them. Guests can only look. The album on the living room table is shared, but each person may take out only the photos they put in.

What is it?

Aai = owner (user). Family = group. Guests = others. Look = read (r). Add or remove = write (w). Opening the album = execute (x) on a folder. Shared table rule = sticky bit, like /tmp.

How to do it (step by step)

How Linux checks a permissionA process (with a UID and groups) opens a fileIs the UID root? Then almost always allowedIs the UID the file owner? Use the owner bits onlyElse, is one of its groups the file group? Use group bitsElse use the other bitsBit present: allowed. Missing: Permission denied
Only ONE set is used. An owner with no rights is denied even if others have rights.

2. ls -l: read the permission string

Why do we need this?

Every 'Permission denied' starts by reading these 10 characters.

What is it?

-rwxr-x--- means: type (- file, d folder, l link), then owner rwx, group r-x, others ---. Then the link count, owner, group, size, date and name. ls -ld FOLDER shows the folder itself, not its contents. ls -n shows numbers instead of names.

PositionExampleMeaning
1dType: folder
2-4rwxOwner can read, write, execute
5-7r-xGroup can read and execute
8-10---Others have nothing

How to do it (step by step)

  1. A script.
ls -l deploy.sh

Output:

-rwxr-x--- 1 ravi devs 812 Oct 25 10:10 deploy.sh

What it means: ravi full, devs read and run, others nothing.

  1. A folder itself.
ls -ld /srv/shared

Output:

drwxrwsr-x 2 root devs 4096 Oct 25 10:12 /srv/shared

What it means: s in group x place = SGID (covered below).

  1. Numbers, not names.
ls -ln deploy.sh

Output:

-rwxr-x--- 1 1001 3001 812 Oct 25 10:10 deploy.sh

What it means: Useful when a user was deleted.

3. r, w, x on files vs folders

Why do we need this?

People give r on a folder and wonder why cd fails. The meaning changes for folders.

What is it?

File: r read content, w change content, x run it as a program. Folder: r list names (ls), w create, delete or rename entries inside, x enter it and reach files inside (cd, open by path). To delete a file you need w on the FOLDER, not on the file.

BitOn a fileOn a folder
rRead contentList names
wChange contentAdd, delete, rename inside
xRun itEnter and pass through

How to do it (step by step)

  1. Folder with r but no x.
chmod 744 box && sudo -u priya ls box; sudo -u priya cat box/a.txt

Output:

ls: cannot access 'box/a.txt': Permission denied
a.txt
cat: box/a.txt: Permission denied

What it means: She sees names but cannot open them.

  1. Folder with x but no r.
chmod 711 box && sudo -u priya ls box; sudo -u priya cat box/a.txt

Output:

ls: cannot open directory 'box': Permission denied
hello

What it means: She cannot list, but can open a known name.

  1. Deleting needs w on the folder.
ls -ld /etc && rm /etc/passwd-test

Output:

dr-xr-xr-x. ... root root /etc
rm: cannot remove '/etc/passwd-test': Permission denied

What it means: The file's own bits do not matter here.

4. chmod with numbers (octal)

Why do we need this?

Numbers set all three groups at once and are what docs and tickets use: 644, 755, 600.

What is it?

r=4, w=2, x=1. Add them per set: 7=rwx, 6=rw-, 5=r-x, 4=r--, 0=---. Three digits are owner, group, others. A fourth digit in front sets special bits (4 SUID, 2 SGID, 1 sticky). -R changes a whole tree (careful).

NumberLettersTypical use
644rw-r--r--Normal files, web pages
600rw-------Private keys, secrets
755rwxr-xr-xScripts, folders
700rwx------Private folders, ~/.ssh
640rw-r-----Config read by a group
775rwxrwxr-xTeam folder

How to do it (step by step)

  1. Make a key private.
chmod 600 ~/.ssh/id_ed25519 && ls -l ~/.ssh/id_ed25519

Output:

-rw------- 1 ravi ravi 411 Oct 25 id_ed25519

What it means: SSH refuses keys that others can read.

  1. A script everyone can run.
chmod 755 backup.sh && ls -l backup.sh

Output:

-rwxr-xr-x 1 ravi ravi 540 Oct 25 backup.sh

What it means: 7 owner, 5 group, 5 others.

  1. Config readable by the app group.
sudo chmod 640 /etc/billing/db.conf && ls -l /etc/billing/db.conf

Output:

-rw-r----- 1 root billing 210 Oct 25 db.conf

What it means: Others cannot see the DB password.

  1. Never do this.
chmod -R 777 /var/www

Output:

(no output)

What it means: Anyone can change your site. Use 755 folders and 644 files.

5. chmod with letters (symbolic)

Why do we need this?

Often you want to add or remove one right without touching the rest, like making a script runnable.

What is it?

Who: u owner, g group, o others, a all. Action: + add, - remove, = set exactly. What: r w x, X (x only on folders or files already executable), s, t. You can combine: chmod u=rwx,g=rx,o= file.

How to do it (step by step)

  1. Make a script runnable.
chmod u+x run.sh && ls -l run.sh

Output:

-rwxr--r-- 1 ravi ravi 120 Oct 25 run.sh

What it means: Only the owner gets x.

  1. Remove rights for others.
chmod o-rwx salary.xlsx && ls -l salary.xlsx

Output:

-rw-r----- 1 hr hr 88K Oct 25 salary.xlsx

What it means: Others lose everything.

  1. Set exactly.
chmod u=rw,g=r,o= report.pdf && stat -c '%A %a' report.pdf

Output:

-rw-r----- 640

What it means: = replaces old bits for that set.

  1. Fix a web tree in one command.
chmod -R u=rwX,g=rX,o=rX /var/www/site

Output:

(no output)

What it means: Capital X gives x to folders only, so files stay 644 and folders 755.

chmod in depth: numeric vs symbolic side by side

Why do we need this?

You will meet both forms in docs and scripts. You must be able to read one and write the other.

What is it?

Numeric sets all bits at once (r=4, w=2, x=1). Symbolic changes only what you name (u g o a, + - =). Both can use -R for a folder tree.

Common options

OptionWhat it does
644 = u=rw,g=r,o=rNormal file.
755 = u=rwx,g=rx,o=rxScript or folder others can enter.
600 = u=rw,go=Secret file.
700 = u=rwx,go=Private folder.
g+wAdd write for group only; keep the rest.
-RApply to everything inside a folder.

How to do it (step by step)

  1. Check the start.
ls -l plan.txt

Output:

-rw-r--r-- 1 arjun team 900 Oct 25 plan.txt

What it means: Now it is 644.

  1. Add group write with symbolic.
chmod g+w plan.txt && stat -c %a plan.txt

Output:

664

What it means: Only the group part changed.

  1. Set exact rights with numbers.
chmod 640 plan.txt && ls -l plan.txt

Output:

-rw-r----- 1 arjun team 900 Oct 25 plan.txt

What it means: Owner rw, group r, others nothing.

  1. Same result in symbolic form.
chmod u=rw,g=r,o= plan.txt && stat -c %a plan.txt

Output:

640

What it means: Two ways, one result.

Practise this in the tasks below: Practice tasks

6. stat: exact permissions and times

Why do we need this?

ls hides the number and some details. stat shows the octal mode, owner IDs and all three times.

What is it?

stat FILE prints size, inode, mode, UID/GID and access/modify/change times. stat -c '%a %U %G %n' prints a custom line: %a octal, %A letters, %U owner, %G group.

How to do it (step by step)

  1. Full details.
stat /etc/passwd | sed -n '1p;4p'

Output:

  File: /etc/passwd
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

What it means: 0644 in numbers.

  1. Short form for many files.
stat -c '%a %U:%G %n' /etc/shadow /etc/sudoers

Output:

0 root:root /etc/shadow
440 root:root /etc/sudoers

What it means: Great for audits.

7. chown: change owner and group

Why do we need this?

Files copied as root, uploads by the web server, or restored backups end up with the wrong owner and the app cannot write.

What is it?

sudo chown USER FILE changes owner. chown USER:GROUP FILE changes both. chown :GROUP FILE only group. -R for a tree. --reference=OTHER copies owner from another file. Only root can give files away.

How to do it (step by step)

  1. Give the app folder to the app user.
sudo chown -R deploy:deploy /srv/app && ls -ld /srv/app

Output:

drwxr-xr-x 5 deploy deploy 4096 Oct 25 /srv/app

What it means: Everything inside now belongs to deploy.

  1. Owner and group for uploads.
sudo chown nginx:www /srv/app/uploads

Output:

(no output)

What it means: The web server can write uploads.

  1. Normal user cannot give away.
chown priya notes.txt

Output:

chown: changing ownership of 'notes.txt': Operation not permitted

What it means: Only root can change the owner.

  1. Copy ownership from another file.
sudo chown --reference=/etc/nginx/nginx.conf /etc/nginx/conf.d/shop.conf

Output:

(no output)

What it means: Same owner and group as nginx.conf.

chown in depth: user:group and -R

Why do we need this?

Web servers, apps and teams need the right owner. A wrong owner gives "Permission denied" even with good chmod bits.

What is it?

chown USER file sets the owner. USER:GROUP sets both. :GROUP sets only the group. -R does the whole tree. Changing owner needs sudo.

Common options

OptionWhat it does
chown user fOwner only.
chown user:group fOwner and group.
chown :group fGroup only (like chgrp).
chown user: fOwner, and group = that user's main group.
-RWhole folder tree.
-v / -cPrint every change / only real changes.

How to do it (step by step)

  1. See who owns it.
ls -l /srv/blog/index.html

Output:

-rw-r--r-- 1 root root 2.1K Oct 25 /srv/blog/index.html

What it means: root owns it, so the app user cannot edit.

  1. Set owner and group together.
sudo chown -v www-data:www-data /srv/blog/index.html

Output:

changed ownership of '/srv/blog/index.html' from root:root to www-data:www-data

What it means: Both parts changed in one command.

  1. Only the group.
sudo chown :editors /srv/blog/drafts && ls -ld /srv/blog/drafts

Output:

drwxrwxr-x 2 www-data editors 4096 Oct 25 /srv/blog/drafts

What it means: Owner stayed, group is editors.

  1. Whole tree with -R, show only real changes.
sudo chown -Rc www-data:www-data /srv/blog

Output:

changed ownership of '/srv/blog/drafts' from www-data:editors to www-data:www-data

What it means: -c printed only the item that really changed.

Practise this in the tasks below: Practice tasks

8. chgrp: change only the group

Why do we need this?

A team folder must belong to the team group so all members get group rights.

What is it?

chgrp GROUP FILE changes the group. A normal user can do it if they own the file and are a member of the new group. -R for a tree.

How to do it (step by step)

  1. Hand a report to finance.
chgrp finance q3.xlsx && ls -l q3.xlsx

Output:

-rw-r----- 1 ravi finance 40K Oct 25 q3.xlsx

What it means: Finance members can now read it.

  1. Not a member.
chgrp hr q3.xlsx

Output:

chgrp: changing group of 'q3.xlsx': Operation not permitted

What it means: You must be in hr, or use sudo.

9. umask: default rights for new files

Why do we need this?

Files created by an app or a user get rights from umask. A wrong umask makes secrets world-readable, or team files unreadable.

What is it?

New files start from 666, folders from 777, and the umask bits are removed. umask 022 gives 644 files and 755 folders. umask 027 gives 640 and 750. umask 077 gives 600 and 700. umask -S shows letters. Set it in ~/.bashrc, /etc/login.defs or a systemd unit (UMask=).

umaskNew fileNew folder
022644755
002664775
027640750
077600700

How to do it (step by step)

  1. Check your umask.
umask; umask -S

Output:

0022
u=rwx,g=rx,o=rx

What it means: The usual default.

  1. See it in action.
umask 077; touch secret.txt; mkdir private; ls -ld secret.txt private

Output:

-rw------- 1 ravi ravi 0 secret.txt
drwx------ 2 ravi ravi 4096 private

What it means: Only you can touch them.

  1. Team-friendly umask.
umask 002; touch shared.txt; ls -l shared.txt

Output:

-rw-rw-r-- 1 ravi devs 0 shared.txt

What it means: Group can write too.

  1. Set it for a service.
sudo systemctl edit billing  # add: [Service] UMask=0027

Output:

(editor opens)

What it means: Files the service writes get 640.

10. SUID (4000): run as the file owner

Why do we need this?

passwd must write /etc/shadow, which only root can. SUID lets a normal user run it with root's power, only for that program.

What is it?

SUID on an executable makes it run with the owner's UID. It shows as s in the owner x place (S if x is missing). Set with chmod u+s or chmod 4755. It is ignored on scripts and on file systems mounted nosuid. Every extra SUID binary is an attack target: audit them.

How to do it (step by step)

  1. The classic example.
ls -l /usr/bin/passwd

Output:

-rwsr-xr-x. 1 root root 32648 /usr/bin/passwd

What it means: s: it runs as root for every user.

  1. Find all SUID files.
sudo find / -xdev -perm -4000 -type f 2>/dev/null | head -4

Output:

/usr/bin/passwd
/usr/bin/sudo
/usr/bin/su
/usr/bin/mount

What it means: Compare with a known-good list.

  1. Remove SUID from a risky copy.
sudo chmod u-s /opt/tools/oldping && ls -l /opt/tools/oldping

Output:

-rwxr-xr-x 1 root root 7k oldping

What it means: No longer runs as root.

11. SGID (2000): group inheritance

Why do we need this?

In a team folder, files made by each person get their own group, and teammates cannot edit them. SGID on the folder fixes this.

What is it?

On a folder, SGID makes new files and folders inside take the folder's group (new sub-folders get SGID too). On a program, it runs with the file's group. Shows as s in the group x place. Set with chmod g+s or chmod 2775.

How to do it (step by step)

  1. Create a team folder.
sudo mkdir /srv/devs && sudo chgrp devs /srv/devs && sudo chmod 2775 /srv/devs && ls -ld /srv/devs

Output:

drwxrwsr-x 2 root devs 4096 Oct 25 /srv/devs

What it means: s in the group part.

  1. New files get the team group.
touch /srv/devs/plan.md && ls -l /srv/devs/plan.md

Output:

-rw-rw-r-- 1 ravi devs 0 Oct 25 plan.md

What it means: Group is devs, not ravi.

  1. Pair with umask 002.
umask 002; echo hi > /srv/devs/notes.txt; ls -l /srv/devs/notes.txt

Output:

-rw-rw-r-- 1 ravi devs 3 notes.txt

What it means: Teammates can edit.

12. Sticky bit (1000): only owners delete

Why do we need this?

In a folder everyone can write, like /tmp, anyone could delete anyone's files. The sticky bit stops that.

What is it?

On a folder, sticky means only the file owner, the folder owner or root can delete or rename a file. Shows as t in the others x place. Set with chmod +t or chmod 1777.

How to do it (step by step)

  1. See it on /tmp.
ls -ld /tmp

Output:

drwxrwxrwt. 18 root root 4096 Oct 25 /tmp

What it means: t at the end.

  1. A shared upload folder.
sudo mkdir /srv/drop && sudo chmod 1777 /srv/drop

Output:

(no output)

What it means: Everyone can add, only owners can remove.

  1. Try deleting someone else's file.
sudo -u priya rm /srv/drop/ravi.txt

Output:

rm: cannot remove '/srv/drop/ravi.txt': Operation not permitted

What it means: The sticky bit protected it.

13. Putting it together: safe permission recipes

Why do we need this?

Most real tasks are the same few patterns. Learn them once.

What is it?

Web root: owner deploy, group nginx, folders 2750, files 640. SSH: ~/.ssh 700, keys 600, authorized_keys 600. Secrets: 600 or 640 with an app group. Team share: SGID 2775 plus umask 002. Shared drop: 1777.

How to do it (step by step)

  1. Fix SSH folder rights.
chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys && stat -c '%a %n' ~/.ssh ~/.ssh/authorized_keys

Output:

700 /home/ravi/.ssh
600 /home/ravi/.ssh/authorized_keys

What it means: sshd ignores keys if these are too open.

  1. Web root recipe.
sudo chown -R deploy:nginx /srv/www && sudo find /srv/www -type d -exec chmod 2750 {} + && sudo find /srv/www -type f -exec chmod 640 {} +

Output:

(no output)

What it means: nginx can read, only deploy can write.

  1. Find world-writable files to fix.
sudo find /srv -xdev -type f -perm -o+w

Output:

/srv/www/config.php

What it means: Fix with chmod o-w.

14. Real examples from many industries

Why do we need this?

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

What is it?

Each row is a made-up team. It shows who uses this and how.

How to do it (step by step)

Team and cityHow they use itCommand
Kiran, web agency, NashikResets a WordPress tree to 755/644find wp -type d -exec chmod 755 {} +
Ahmed, bank, RiyadhKeeps DB configs readable only by the app groupchmod 640 /etc/app/db.conf
Sofia, design studio, LisbonTeam share where everyone can editchmod 2775 /srv/design
Tom, university, BostonShared drop folder for studentschmod 1777 /srv/drop
Neha, security audit, GurugramLists SUID binaries every weekfind / -xdev -perm -4000 -type f
Chen, SaaS, ShenzhenSets UMask=0027 for servicesUMask=0027 in the systemd unit

15. Common errors and how to fix them

Why do we need this?

Everyone hits these errors. Knowing the cause saves hours.

What is it?

Read the exact message first. Then match it to a row below.

How to do it (step by step)

Error you seeWhy it happensHow to fix
bash: ./deploy.sh: Permission deniedNo x bit on the scriptAdd execute.
chmod u+x deploy.sh
cat: /etc/shadow: Permission deniedNormal user lacks rUse sudo if you really need it.
sudo cat /etc/shadow
chown: changing ownership of 'x': Operation not permittedOnly root can change ownersUse sudo.
sudo chown deploy x
WARNING: UNPROTECTED PRIVATE KEY FILE! Permissions 0644 for 'key.pem' are too open.SSH key readable by otherschmod 600 the key.
chmod 600 key.pem
Authentication refused: bad ownership or modes for directory /home/raviHome or ~/.ssh is group/other writableRemove g/o write.
chmod go-w ~ && chmod 700 ~/.ssh
rm: cannot remove 'x': Operation not permitted (in /tmp)Sticky bit: not your fileAsk the owner or use sudo.
ls -l /tmp/x
cd: /srv/data: Permission deniedNo x on the folder (or a parent)Check each folder with namei -l.
namei -l /srv/data/file
nginx: 403 Forbiddennginx user cannot read files or pass a parent folderGive r on files and x on every parent.
namei -l /srv/www/index.html
chmod: changing permissions of 'x': Read-only file systemDisk mounted read-onlyCheck mount and dmesg.
mount | grep ' / '
Team member cannot edit a shared fileFile has the creator's group or umask 022Use SGID folder and umask 002.
chmod 2775 /srv/devs

16. Troubleshooting step by step

Why do we need this?

For 'Permission denied', walk the path from / to the file and check the user, the group and each bit.

What is it?

Go top to bottom. Stop at the first step that shows the problem.

How to do it (step by step)

  1. Who is the process running as?
ps -o user,group,comm -C nginx
  1. Check every folder on the path.
namei -l /srv/www/site/index.html
  1. Exact mode and owner of the file.
stat -c '%A %a %U:%G' /srv/www/site/index.html
  1. Is it a group problem?
id nginx
  1. Test as that user.
sudo -u nginx cat /srv/www/site/index.html
  1. Still denied? Check ACLs and SELinux.
getfacl /srv/www/site/index.html; ls -Z /srv/www/site/index.html

17. Lab 1: Build a team share

Why do we need this?

Create a folder where every devs member can create and edit files, but others cannot see it.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Create users and group.
sudo groupadd -f devs && sudo useradd -m -G devs dev1 && sudo useradd -m -G devs dev2

Output:

(no output)
  1. Create the folder with SGID.
sudo mkdir -p /srv/team && sudo chgrp devs /srv/team && sudo chmod 2770 /srv/team && ls -ld /srv/team

Output:

drwxrws--- 2 root devs 4096 Oct 25 /srv/team
  1. dev1 creates a file.
sudo -u dev1 bash -c 'umask 002; echo v1 > /srv/team/plan.txt' && ls -l /srv/team

Output:

-rw-rw-r-- 1 dev1 devs 3 plan.txt
  1. dev2 edits it.
sudo -u dev2 bash -c 'echo v2 >> /srv/team/plan.txt' && cat /srv/team/plan.txt

Output:

v1
v2
  1. An outsider is blocked.
sudo -u nobody ls /srv/team

Output:

ls: cannot open directory '/srv/team': Permission denied

18. Lab 2: Numbers, letters, umask and special bits

Why do we need this?

Practise chmod both ways, see umask results, and find SUID and sticky bits.

What is it?

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

How to do it (step by step)

  1. Numbers.
mkdir -p ~/lab15 && cd ~/lab15 && touch a && chmod 640 a && stat -c '%A %a' a

Output:

-rw-r----- 640
  1. Letters.
chmod u+x,g-r a && stat -c '%A %a' a

Output:

-rwx------ 700
  1. umask 027.
(umask 027; touch b; mkdir c) && stat -c '%a %n' b c

Output:

640 b
750 c
  1. Sticky folder.
mkdir d && chmod 1777 d && ls -ld d

Output:

drwxrwxrwt 2 ravi ravi 4096 d
  1. SUID count.
sudo find /usr/bin -perm -4000 | wc -l

Output:

14

Ravindra Bagale's Tip

Never fix 'Permission denied' with chmod 777. Find WHO is denied (ps, id), WHERE (namei -l), and give only the missing bit to the right owner or group.

19. Frequently asked questions

Q. What does 755 mean?

Owner rwx (7), group r-x (5), others r-x (5).

Q. Why does my script say Permission denied?

It needs x: chmod u+x script.sh, or run it with bash script.sh.

Q. Can root ignore permissions?

Mostly yes for read and write, but root still needs at least one x bit to run a file, and immutable files or SELinux can still block root.

Q. What is the difference between s and S?

s means the special bit and x are both set. S means the special bit is set but x is missing (usually a mistake).

Q. Does SUID work on shell scripts?

No, Linux ignores SUID on scripts for safety. Use sudo rules instead.

Q. How do I see permissions as numbers?

stat -c '%a %n' FILE.

Q. Why can I delete a file I cannot read?

Delete depends on w and x of the folder, not the file.

Q. Where do I set umask for everyone?

/etc/login.defs (UMASK), /etc/profile or a file in /etc/profile.d; for services use UMask= in the unit.

20. Interview questions and answers

Beginner level

Q. What do r, w and x mean?

Read, write and execute.

Q. What is chmod 644?

Owner read and write; group and others read only.

Q. How do you make a script executable?

chmod +x script.sh.

Q. How do you change a file's owner?

sudo chown user:group file.

Mid level level

Q. What does x mean on a folder?

Permission to enter it and reach files inside.

Q. With umask 027, what are new file and folder rights?

Files 640, folders 750.

Q. What is SGID on a folder used for?

New files inherit the folder's group, for team shares.

Q. Why is /tmp 1777?

Everyone can write, but the sticky bit lets only owners delete their files.

Senior level

Q. nginx returns 403 for a file with 644. Why?

A parent folder lacks x for nginx, the file is outside the allowed root, an ACL denies it, or SELinux context is wrong. Check namei -l, getfacl and ls -Z.

Q. Why is an unexpected SUID root binary dangerous?

Any user who runs it gets root rights in that program; bugs or shell escapes give full root.

Q. How do you prevent SUID abuse on data disks?

Mount them with nosuid,nodev (and noexec where possible), and audit find -perm -4000 regularly.

Q. A deploy breaks because files are owned by root after a CI copy. How do you fix it for good?

Run the copy as the deploy user, or chown in the pipeline, set UMask in the service, and use SGID group folders.

Try at home

  1. Create a folder 'family' that only you and a group 'home' can open, where new files keep the 'home' group. Prove it with ls -ld and a test file.
  2. Write down the umask that gives new files 600, and test it.

Practice Tasks

About this scenario

London Legal LLP is a law business in London. Oliver Hughes looks after its IT and cloud work. They count money in GBP and run their AWS resources in the eu-west-2 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 21

Easy See permissions

Why: Start every access check here.

What to do: Show the permissions of release.sh in long format.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Folder itself

Why: ls -l shows contents otherwise.

What to do: Show the permissions of the /srv/projects folder itself, not its contents.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Make a script runnable

Why: Permission denied on ./start.sh.

What to do: Add execute for the owner on start.sh, using letters.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Private key

Why: SSH rejects open keys.

What to do: Make prod.pem readable and writable only by the owner, using numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Script for everyone

Why: Shared tools at BIZ.

What to do: Give sync.sh rwxr-xr-x using numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Change group only

Why: Finance needs the report.

What to do: Change the group of payroll.xlsx to hr.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Set umask symbolically

Why: New files get odd rights.

What to do: Set the umask in symbolic form so the owner gets rwx, the group rx and others nothing.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Change only the group

Why: Finance team must share it.

What to do: Change only the group of budget.xlsx to finance using chown.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Owner and group

Why: App user must own its log.

What to do: Make user ravi and group devs the owner and group of app.log.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Add group write

Why: Team edits one file.

What to do: Give the group write permission on shared.txt without changing anything else.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Private folder

Why: Keys and backups stay private.

What to do: Make the folder private so only the owner can read, write and enter it, using numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Group-readable config

Why: App group reads the DB config.

What to do: Set db.conf to rw for owner, r for group, nothing for others, with numbers.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Remove all rights for others

Why: Salary data.

What to do: Remove read, write and execute for others on bonus.xlsx.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Mode as a number

Why: Tickets use numbers.

What to do: Print only the octal mode of /etc/passwd with stat.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Give the app folder to www

Why: Root-owned files break wwws.

What to do: Recursively set owner and group of /srv/web to www.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Private by default

Why: Secrets in this shell.

What to do: Set the umask so new files become 660 and folders 770.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Team folder group inheritance

Why: Teammates must edit each other's files.

What to do: Set /srv/qa to 2775.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Shared drop folder

Why: Users must not delete each other's files.

What to do: Give /srv/uploads the mode 1777.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Fix a web tree

Why: Folders need x, files do not.

What to do: Recursively give /var/www/blog u=rwX,g=rX,o=rX.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Audit SGID files

Why: Each SGID root file is a target.

What to do: Find regular files with the SGID bit under / on this file system only.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Check every folder on a path

Why: nginx 403 on a 644 file.

What to do: Show owner and mode of every part of /var/www/blog/app.js.

How:
  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. ls -l: type, owner, group, others; stat for numbers.
  2. Folder x = enter; delete needs folder w.
  3. chmod 644/755/600 or u+x, g-w, o=.
  4. chown user:group, chgrp group, -R with care.
  5. umask 022 → 644/755; 027 → 640/750.
  6. SUID 4, SGID 2, sticky 1; never chmod 777.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

chmod numbers. Control who reads, writes, runs. r=4 w=2 x=1, add them up. Folders need x to enter. Private SSH key.
Revision card: chmod numbers · Download PNG · Open card page
SUID SGID sticky. Extra bits for special jobs. SUID 4, SGID 2, sticky 1. SGID on team folders. Shared drop folder.
Revision card: SUID SGID sticky · Download PNG · Open card page
chmod 755 vs chmod u+x. Sets all bits at once. / Changes only named bits.
Comparison: chmod 755 vs chmod u+x · Download PNG · Open card page
SUID vs SGID. Run as the file owner. / New files take the folder group.
Comparison: SUID vs SGID · Download PNG · Open card page
755 vs 644?. 755 for folders and scripts. 644 for normal files. Fix a web tree.
Interview card: 755 vs 644? · Download PNG · Open card page
What does umask do?. It removes bits from new files. 022 gives 644 files, 755 dirs. Private by default.
Interview card: What does umask do? · Download PNG · Open card page
Why is /tmp 1777?. Everyone may write there. Sticky stops deleting others' files. See the t.
Interview card: Why is /tmp 1777? · Download PNG · Open card page
644 file, still 403?. A parent folder lacks x. Check every part of the path. Walk the path.
Interview card: 644 file, still 403? · Download PNG · Open card page

More cards: Ravi Sir's Cards.