Permission denied

Why can ana not read a file that is sitting right there?

$ cat /etc/shadow1cat: /etc/shadow: Permission denied$ ls -l /etc/shadow2-rw-r----- 1 root shadow 1404 Nov 16 06:59 /etc/shadow$ id3uid=1001(ana) gid=1001(ana) groups=1001(ana),1002(devops)1The kernel said nocat is fine; the file is protected2The file’s sideowner root, group shadow, and rw- r-- ---3Your sidenot root, not in shadow: you are "others"Every file has an owner, a group and permissions. Every command runs as a user. The kernel compares them.Here the owner (root) may read and write, members of the group shadow may read, and everyone else gets nothing: the lastthree characters are ---. ana is neither, so the answer is no. "Permission denied" is always this comparison, and reading ittakes the same two commands: ls -l for the file’s side and id for yours. The rest of the lesson fills in each part:accounts and groups, the nine letters, how to change them, and sudo for the times you must act as root.

Access is a comparison: the file’s owner, group and permissions against the user and groups the command runs as.

Users and groups

The kernel knows you by numbers: a UID and your groups

/etc/passwd: one line per account, readable by everyoneananamexpassword1001UID1001primary GIDAna Lopezcomment/home/anahome/bin/bashlogin shell/etc/shadow: the password hash and its ageing, readable by root onlyana:$y$j9T$Fq2...:20407:0:90:7:14::/etc/group: groups and their extra membersdevops:x:1005:ana,rajwheel:x:10:anawheel (RHEL) or sudo (Ubuntu): the group allowed to use sudoUID ranges0root: every check passes1 to 999system accounts for services1000 and uppeople65534nobody$ id anauid=1001(ana) gid=1001(ana)groups=1001(ana),1005(devops)The kernel knows you by numbers: a user ID, one primary group ID and a list of extra groups.Names are for people; files store only the numbers, which is why a file copied to another machine can show the wrong owner.Every process carries the IDs of the user who started it, and every permission check compares them with the file’s.Service accounts below 1000 usually have the shell /usr/sbin/nologin, so nobody can log in as them.

Accounts live in /etc/passwd, hashes in /etc/shadow and memberships in /etc/group; files store only the numbers.

/etc/shadow · Going deeper

Eight fields hold the hash and every ageing rule

FieldMeaningananame$y$j9T$Fq2...hash: $y$ is yescrypt20407last change, days since 19700min days between changes90max days: then it expires7warn days before14inactive days after(empty)account expiry dateSet it per user# chage -M 90 -W 7 -I 14 ana# chage -E 2026-12-31 contractor# passwd -l ana lock# chage -d 0 ana change at next loginDefaults for new users/etc/login.defs PASS_MAX_DAYS 90Eight colon-separated fields hold the hash and every ageing rule; chage edits them.Ubuntu 24.04 and RHEL 10 hash with yescrypt ($y$); older systems show $6$ for SHA-512. A ! in front of the hash means locked.Days are counted from 1 January 1970: 20407 is 15 November 2025.

Shadow fields store the hash, last change, minimum, maximum, warning, inactivity and expiry; chage edits them.

File permissions

Nine bits: read, write and execute, for three kinds of user

$ ls -l deploy.sh-rwxr-xr-- 1 ana devops 812 Nov 15 10:00 deploy.showner (ana)r4w2x1group (devops)r4-2x1othersr4-2-14+2+174+0+154+0+04= 754Nine bits decide access: read, write and execute, for the owner, the group and everyone else.Each set of three is one octal digit, with r = 4, w = 2 and x = 1, so rwxr-xr-- is 754: the owner can edit and run the script, thedevops group can run it, everyone else can only read it. chmod 754 deploy.sh sets all nine at once; chmod g-x,o=r changessome by letter. chown ana:devops deploy.sh sets the owner and group, and only root may give a file away.

Permissions are r, w and x for the owner, the group and others; each set is an octal digit, so rwxr-xr-- is 754.

chmod, chgrp, chown

How do you change who may do what?

$ ls -l deploy.sh1-rw-rw-r-- 1 ana ana 27 Nov 16 07:04 deploy.sh$ ./deploy.shbash: ./deploy.sh: Permission denied$ chmod u+x deploy.sh2$ ./deploy.shdeploying$ chmod 640 notes.txt3$ chgrp devops notes.txt4$ ls -l notes.txt-rw-r----- 1 ana devops 5 Nov 16 06:59 notes.txt$ chown root notes.txt5chown: changing ownership of 'notes.txt': Operation not permitted1No x, so it cannot be runeven by its owner2chmod u+xadd execute for the owner (u)3chmod 640set all nine: rw- r-- ---4chgrp devopsto a group you belong to5chown needs rootyou cannot give files awaychmod changes the permissions, chgrp the group and chown the owner. Only the owner or root may.Letters change one thing (u, g, o for owner, group, others; + or - ; r, w, x). Numbers set all nine at once.As root, chown ana:devops file sets owner and group together, and -R applies a change to a whole directory tree.

chmod sets the permission bits with letters or numbers, chgrp changes the group and chown, as root, the owner.

Permission checks

Exactly one of the three sets applies, checked in order

ana opens report.txtIs ana the owner?yesuse the owner bits onlynoIn the file’s group?yesuse the group bits onlynouse the other bitsSo this happens:----rw-rw- ana ...ana owns it, and the ownerbits are empty: ana isrefused, while everyoneelse may read and write.root skips the checkfor read and write.Exactly one set of three bits applies: the owner’s if you own the file, else the group’s, else everyone’s.The sets are never added together. The first one that matches you is used, even if a later set would allow more.When you diagnose “Permission denied”, ask the questions in this order with ls -l and id, and check every directory on the path:to reach /srv/app/config.yml you need x on /, /srv and /srv/app as well as r on the file. namei -l shows them all at once.

The kernel uses the owner bits if you own the file, else the group bits if you are in its group, else the other bits.

Directory permissions

On a folder, write means you may delete what is inside

BitOn a fileOn a directoryrread the contentslist the names in it (ls)wchange the contentscreate, delete and rename entries in itxrun it as a programenter it and reach anything below (cd)/srv/drop drwxrwxrwxreport.pdf, r--r--r--, raj’sana can delete it:deleting edits the directory/tmp drwxrwxrwtraj’s fileana cannot delete it:the t (sticky bit) protects itOn a directory, w lets you create and delete names in it, whatever the files’ own permissions say.A file’s write bit protects its contents, not its existence: the name belongs to the directory. That is why shared folders such as/tmp carry the sticky bit, t in the last place, which limits deleting to each file’s owner. Without x on a directory, r aloneshows names but nothing can be opened; a directory needs r and x together to be useful.

On a directory r lists, w creates and deletes entries, x enters; the sticky bit limits deletion to each file’s owner.

Directory permissions · Going deeper · 1 of 3

New files start at 666, and the umask takes bits away

a new file666mask022=644rw-r--r--umask 022 (root, most servers)a new directory777mask022=755rwxr-xr-xumask 022a new file666mask002=664rw-rw-r--umask 002 (Ubuntu users with their own group)a new file666mask077=600rw-------umask 077 (private)New files start from 666 and directories from 777; the umask removes bits from that start.It is a mask, not a subtraction: each bit set in the umask is cleared. Files never start executable, so a script needs chmod +x.umask shows the current value; set it for everyone in /etc/login.defs or a file in /etc/profile.d, and for a service in its systemdunit with UMask=. The Red Hat exam asks you to change a user’s default permissions this way.

The umask clears bits from the default 666 for files and 777 for directories: umask 022 gives 644 and 755.

Directory permissions · Going deeper · 2 of 3

setuid, setgid and sticky: a fourth digit with real power

BitSeen asOn a fileOn a directorysetuid 4-rwsr-xr-xruns as the file’s owner(no effect)setgid 2drwxrwsr-xruns as the file’s groupnew files inherit its groupsticky 1drwxrwxrwt(no effect)only owners may delete$ ls -l /usr/bin/passwd-rwsr-xr-x 1 root root 64152 /usr/bin/passwdany user runs it as root, to write /etc/shadow# chgrp devops shared; chmod 2775 shareddrwxrwsr-x 2 root devops 4096 shared-rw-rw-r-- 1 ana devops 0 shared/xana’s new file belongs to devops, not to anaThree more bits sit in front of the nine: setuid, setgid and sticky, written as a fourth octal digit.chmod 2775 shared makes a team folder: setgid gives every new file the folder’s group, so the whole team can edit it. setuidprograms run with their owner’s power, which makes each one a security risk: audit them with find / -perm -4000 -type f.A capital S or T means the bit is set but the x underneath is not, which is almost always a mistake.

setuid runs a program as its owner, setgid on a directory passes its group to new files, and sticky protects deletes.

Directory permissions · Going deeper · 3 of 3

Give one more person access without a new group

$ setfacl -m u:raj:r report.pdf$ ls -l report.pdf-rw-r-----+ 1 ana devops 52K report.pdf$ getfacl report.pdf# file: report.pdf# owner: ana# group: devopsuser::rw-user:raj:r--group::r--mask::r--other::---What it addsa + at the end of ls -l: an ACL existsnamed users and groups, each with rwxmask: the most any named entry can getFor a folder, a default ACL is inherited$ setfacl -d -m g:audit:rx /srv/reportsremove: setfacl -x u:raj file; all: -bAn access control list adds named users and groups to the nine bits, without changing the owner.Use it when one more person needs access and making a new group would be overkill. The kernel checks the owner, then named users,then groups, then others; the mask caps everything but the owner and others. cp and tar drop ACLs unless told to keep them(cp -a, tar --acls). ext4 and XFS support ACLs on Ubuntu and RHEL without extra mount options.

An ACL adds named users and groups to a file’s permissions; setfacl sets them, getfacl shows them, + marks them.

sudo

One command as root, if a rule allows it, and on the record

ana$ sudo systemctl restart nginxsudoreads /etc/sudoersallowedruns as rootthis command onlylogged to the journalOne rule, four parts%wheelwho: members of wheelALLon any host=(ALL)as any userALLany commandana ALL=(root) /usr/bin/systemctlnarrower: one user, one programsudo runs one command as root, if a rule in /etc/sudoers allows it, and records that it did.You type your own password, not root’s, and it is remembered for a few minutes. Add people to wheel on Red Hat or sudo on Ubunturather than writing rules for each. Edit rules only with visudo, which refuses to save a file with a syntax error, and put themin /etc/sudoers.d/. sudo -l lists what you may run; sudo -i opens a root shell when you need several commands.

sudo checks /etc/sudoers, runs a single command as root with your own password, and logs it; edit rules with visudo.

Account management

Create, group, age and remove an account in five commands

day 0password changedday 83warnings start (90 - 7)day 90expired: change it at loginday 104inactive days over: account lockedDrawn to scale, in days.chage -M 90 -W 7 -I 14 ana: where each number landsCommandDoesuseradd -m -G devops -s /bin/bash rajcreate, with a home and an extra grouppasswd rajset the passwordusermod -aG wheel rajadd a group; without -a the others are removeduserdel -r rajdelete, with the home directoryAccounts are created, given groups, aged and removed with five commands; mind the -a in usermod.

useradd, passwd, usermod -aG, chage and userdel manage the account lifecycle; chage sets password ageing.

PAM and limits · Going deeper

Every login passes through PAM, which also sets limits

login: ssh, su, sudoPAMmodules in /etc/pam.d/decide and set upauthwho are you? password, key, SSSDaccountallowed now? expired, lockedpasswordrules for a new passwordsessionset limits, make the home/etc/security/limits.conf@devops hard nofile 65536raj soft nproc 200ulimit -a shows yours; soft can be raised up to hardCentral accounts: SSSDLDAP or Active Directory usersappear in id and getent passwdEvery login passes through PAM, which checks the password, the account, and sets limits for the session.

PAM modules authenticate, check the account and set session limits from limits.conf; SSSD adds LDAP or AD users.

Lab

Build a team folder and prove who can reach it

# groupadd devops; useradd -m -G devops raj; passwd raj1# mkdir /srv/team; chgrp devops /srv/team; chmod 2770 /srv/team2# ls -ld /srv/teamdrwxrws--- 2 root devops 4096 Nov 15 10:20 /srv/team# su - ana -c "ls /srv/team"3ls: cannot open directory '/srv/team': Permission denied# setfacl -m u:ana:rx /srv/team; su - ana -c "ls /srv/team"4# chage -M 90 -W 7 raj; chage -l raj | grep Maximum5Maximum number of days between password change : 901Account and groupraj, in devops2A team foldersetgid, no access for others3Prove the refusalana is in neither class4Grant one personan ACL, not a new group5Age the passwordand read it backLab: build a team folder, prove who can reach it, then age a password.Run as root on a machine you can break. Press the down arrow for the steps.

Create users and a group, a setgid folder, an ACL for one outsider, a sudo rule for one command, and a password policy.

Lab · Going deeper

Users, a team folder and a password policy

Run these as root (sudo -i) on a lab machine. Every step can be checked with ls -l, id, getfacl or chage -l.

Steps

  1. Create the people. groupadd devops; useradd -m -G devops raj; useradd -m ana; passwd raj. Read the new lines in /etc/passwd, /etc/shadow and /etc/group.
  2. Decode permissions. ls -l /etc/passwd /etc/shadow /usr/bin/passwd. Write each as octal and say why passwd needs the s.
  3. Make a team folder. mkdir /srv/team; chgrp devops /srv/team; chmod 2770 /srv/team. As raj, touch a file in it and check its group.
  4. Prove the refusal. su - ana -c "ls /srv/team". Walk the check: is ana the owner, in the group, or other?
  5. Grant by ACL. setfacl -m u:ana:rx /srv/team; getfacl /srv/team; repeat the listing as ana. Then add a default ACL so new files are readable too.
  6. Test the sticky bit. chmod 1777 /srv/drop; have raj create a file and ana try to delete it. Remove the t and try again.
  7. Change the umask. As ana run umask 077; touch private.txt; ls -l. Make it permanent in ~/.bashrc and explain the result in octal.
  8. Delegate one command. visudo -f /etc/sudoers.d/raj with raj ALL=(root) /usr/bin/systemctl; as raj run sudo -l and one allowed and one refused command.
  9. Age a password. chage -M 90 -W 7 -I 14 raj; chage -l raj. Then chage -d 0 raj and log in as raj to see the forced change.
Check: ana is refused before the ACL and allowed after it, raj’s files in /srv/team belong to devops, and sudo -l shows raj only the one command.

Lesson complete

You finished “Users, groups and permissions”

Nicely done.

Open in a new tab