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

AWS Cloud

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.

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

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.

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 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

Read -rw-r----- in groups. The first dash means a regular file. rw- gives the owner read and write, r-- gives members of the file’s group read, and --- gives everyone else no access through these mode bits. Compare the file’s owner and group in the ls -l row with your identity from id. For a regular file using only these bits, Linux picks your class and checks the requested right. A person in the group can open the file to read but can’t save changes through that access.

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

-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
  1. A script.
ls -l deploy.sh

Output:

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

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

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

Useful when a user was deleted.

3. r, w, x on files vs folders

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

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
  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

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

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

The file's own bits do not matter here.

4. chmod with numbers (octal)

Try reading 640 without memorising it. The owner digit is 6, which is 4 for read plus 2 for write. The group digit is 4, so its members can read. Other users get the last digit, zero. To let the owner execute the same file as well, change only the owner digit to 7. Predict that ls -l will start with -rwxr----- after chmod 740 on your own test file, then compare what you see.

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

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
  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

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

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

Others cannot see the DB password.

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

Output:

(no output)

Anyone can change your site. Use 755 folders and 644 files.

5. chmod with letters (symbolic)

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

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.

  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

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

Others lose everything.

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

Output:

-rw-r----- 640

= 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)

Capital X gives x to folders only, so files stay 644 and folders 755.

chmod in depth: numeric vs symbolic side by side

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

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.
  1. Check the start.
ls -l plan.txt

Output:

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

Now it is 644.

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

Output:

664

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

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

Two ways, one result.

Practise this in the tasks below: Practice tasks

6. stat: exact permissions and times

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

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.

  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)

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

Great for audits.

7. chown: change owner and group

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

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.

  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

Everything inside now belongs to deploy.

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

Output:

(no output)

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

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)

Same owner and group as nginx.conf.

chown in depth: user:group and -R

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

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.
  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

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

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

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

-c printed only the item that really changed.

Practise this in the tasks below: Practice tasks

8. chgrp: change only the group

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

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.

  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

Finance members can now read it.

  1. Not a member.
chgrp hr q3.xlsx

Output:

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

You must be in hr, or use sudo.

9. umask: default rights for new files

Work from what the creating program requests. A regular file often starts with a request for 666, and a directory often starts with 777 because entering a directory needs execute permission. With umask 027 and no default ACL, take away write from the group and all rights from others. The usual result is 640 for the file and 750 for the folder. Create one of each inside a test subshell and compare them with ls -ld. An existing file keeps its mode when you change the umask.

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

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
  1. Check your umask.
umask; umask -S

Output:

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

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

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

Group can write too.

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

Output:

(editor opens)

Files the service writes get 640.

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

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

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.

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

Output:

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

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

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

No longer runs as root.

11. SGID (2000): group inheritance

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

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.

  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

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

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

Teammates can edit.

12. Sticky bit (1000): only owners delete

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

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.

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

Output:

drwxrwxrwt. 18 root root 4096 Oct 25 /tmp

t at the end.

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

Output:

(no output)

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

The sticky bit protected it.

13. Putting it together: safe permission recipes

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

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.

  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

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)

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

Fix with chmod o-w.

14. Real examples from many industries

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

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

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

Everyone hits these errors. Knowing the cause saves hours.

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

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

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

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

  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

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

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

  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

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

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

  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.

Practise this chapter

Interview questions and answers

A teammate can open a file but cannot remove it. Which permissions explain this?

Reading the file checks its own mode or ACL. Removing its name is a change to the containing directory, so check write and execute permission there. If the sticky bit is set, also check who owns the file and directory before expecting a removal.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Which rights does chmod 640 give the file’s group?

Answer and explanation

B. Read only

The group gets the middle digit, 4. Read is 4, write is 2, and execute is 1.

2. With requested mode 666, umask 027 and no default ACL, which mode does a new file usually get?

Answer and explanation

A. 640

The mask removes group write and all permissions for others. It cannot add execute bits to a request that never included them.

3. What access does execute permission on a directory provide?

Answer and explanation

B. The ability to traverse that directory to a known path, subject to other checks

Directory execute is search or traversal. Listing names requires directory read, and changing entries requires additional rights.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

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

Start every access check here.

Show the permissions of release.sh in long format.

  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

ls -l shows contents otherwise.

Show the permissions of the /srv/projects folder itself, not its contents.

  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

Permission denied on ./start.sh.

Add execute for the owner on start.sh, using letters.

  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

SSH rejects open keys.

Make prod.pem readable and writable only by the owner, using numbers.

  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

Shared tools at BIZ.

Give sync.sh rwxr-xr-x using numbers.

  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

Finance needs the report.

Change the group of payroll.xlsx to hr.

  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

New files get odd rights.

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

  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

Finance team must share it.

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

  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

App user must own its log.

Make user ravi and group devs the owner and group of app.log.

  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

Team edits one file.

Give the group write permission on shared.txt without changing anything else.

  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

Keys and backups stay private.

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

  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

App group reads the DB config.

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

  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

Salary data.

Remove read, write and execute for others on bonus.xlsx.

  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

Tickets use numbers.

Print only the octal mode of /etc/passwd with stat.

  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

Root-owned files break wwws.

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

  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

Secrets in this shell.

Set the umask so new files become 660 and folders 770.

  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

Teammates must edit each other's files.

Set /srv/qa to 2775.

  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

Users must not delete each other's files.

Give /srv/uploads the mode 1777.

  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

Folders need x, files do not.

Recursively give /var/www/blog u=rwX,g=rX,o=rX.

  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

Each SGID root file is a target.

Find regular files with the SGID bit under / on this file system only.

  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

nginx 403 on a 644 file.

Show owner and mode of every part of /var/www/blog/app.js.

  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.