Ownership and umask
Who owns a file and why it matters: owner and group IDs, chown and chgrp with every common form, what normal users can and can't change, service accounts owning their files, shared team folders, orphaned files after a user is deleted, and how umask decides the permissions of every new file.
Why ownership matters
The last lesson's permission bits say what the owner, the group and others may do. Ownership decides who is the owner and the group. Change the owner and a completely different set of people gets the owner's rights, without touching a single permission bit.
On real servers, ownership is how you make these work:
- Services run as their own user. nginx runs as
www-data, PostgreSQL aspostgres. Each one must own the files it writes (logs, uploads, data) and nothing else. If an attacker breaks into the web app, they get only whatwww-dataowns. - Teams share through groups. A deployment folder owned by the
devopsgroup, with group write permission, lets the whole team work in it. - People own their homes.
/home/rohanbelongs to rohan, so the owner bits protect his files from everyone else.
Two of the most common deployment bugs are ownership bugs: files copied in as root that the service can't write, and files created by one engineer that teammates can't edit.
Same file, different owner, different access
The permission string never changed. Only who it applies to did.
Files store numbers
A file doesn't store the name "rohan". Its inode stores a UID and a GID, and ls translates them to names using /etc/passwd and /etc/group. ls -n skips the translation:
ls -ln /srv/team-n = show numeric UIDs and GIDs.
total 4
-rw-r--r-- 1 0 0 20 Nov 24 15:55 checklist.md0 0 is root's UID and GID.
This explains two things you'll run into:
- Orphaned files. Delete a user but not their files, and
lsshows a bare number where the name was. If a new account later gets that UID, it silently owns those files. - Containers and NFS. Inside a container or on a network share, the same UID can map to a different name, or none at all. That's why a file created as UID 1000 in a container shows up as whoever is UID 1000 on the host.
chown: change the owner
chown appsvc file # owner only
chown appsvc:appsvc file # owner and group
chown :devops file # group only (same as chgrp devops file)
chown rohan: file # owner rohan, group = rohan's primary group
chown -R appsvc:appsvc /srv/app
chown --reference=/srv/team file # copy owner and group from another file| Option | Meaning |
|---|---|
-R | recursive: the folder and everything below it |
-v | verbose: print each change |
-h | change a symlink itself instead of the file it points to |
--reference=F | use the owner and group of file F |
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:55 a
-rw-r--r-- 1 root root 0 Nov 24 15:55 ba was changed with chown rohan: (trailing colon), which also set the group to rohan's primary group. b copied root:root from /srv/team with --reference.
With -v you get a record of what changed, handy in scripts and change tickets:
changed ownership of '/srv/app/app.py' from root to appsvcchgrp, and what normal users may change
chgrp devops file changes just the group. The rules for who may do what:
| Action | root | file's owner |
|---|---|---|
change the owner (chown) | yes | no |
change the group (chgrp) | to any group | only to a group they belong to |
change permissions (chmod) | yes | yes |
su - rohan -c 'touch /tmp/r.txt; chgrp devops /tmp/r.txt; chgrp appsvc /tmp/r.txt; ls -l /tmp/r.txt'chgrp: changing group of '/tmp/r.txt': Operation not permitted
-rw-r--r-- 1 rohan devops 0 Nov 24 10:25 /tmp/r.txtRohan is in devops, so the first chgrp worked. He isn't in appsvc, so the second was refused. Note the wording: "Operation not permitted" (you're not allowed to make this change at all) rather than "Permission denied" (the permission bits blocked you).
Only root can give files away. Otherwise a user could dodge disk quotas, or plant a file under someone else's name.
Pattern: a service owns its files
The rule of thumb: a service should own what it writes and only read everything else.
| Path | Owner | Mode | Why |
|---|---|---|---|
/srv/app/app.py | root:root | 644 | code: the service reads it, can't modify it |
/srv/app/config/app.conf | root:appsvc | 640 | config: readable by the service's group, editable by root |
/srv/app/logs/ | appsvc:appsvc | 755 | the service writes logs here |
/srv/app/uploads/ | appsvc:appsvc | 750 | the service writes user uploads |
Giving a service ownership of its own code (chown -R appsvc /srv/app) is quick and common, but it means a compromised app can rewrite itself. Tighter setups give it only the folders it must write.
Give the app its files
Make the appsvc service account the owner of everything under /srv/app.
A service like nginx or your app runs as a dedicated user, not as root, so a bug or a hack can only touch what that user owns. That only works if the service's files actually belong to it. Files copied in by root belong to root, and the service can't write its own logs or uploads.
1. See who owns what
ls -l /srv/app
id appsvctotal 16
-rw-r--r-- 1 root root 22 Nov 24 15:54 app.py
drwxr-xr-x 2 root root 4096 Nov 24 15:54 config
drwxr-xr-x 2 root root 4096 Nov 24 15:54 logs
drwxr-xr-x 2 root root 4096 Nov 24 15:54 uploadsColumns 3 and 4 are the owner and the group: root for everything.
2. Try writing as the service
appsvc has the shell nologin (it's a service account), so to run a command as it you override the shell with -s /bin/bash:
su - appsvc -s /bin/bash -c 'echo hi >> /srv/app/logs/app.log'-bash: line 1: /srv/app/logs/app.log: Permission deniedThe log is rw-r--r-- owned by root, so appsvc falls into "others" and only gets r--.
3. Hand everything over with chown -R
chown -R appsvc:appsvc /srv/app
ls -l /srv/app /srv/app/logs| Part | Meaning |
|---|---|
chown | change owner |
-R | recursively: the folder and everything inside it |
appsvc:appsvc | new owner : new group |
/srv/app | where to start |
/srv/app:
total 16
-rw-r--r-- 1 appsvc appsvc 22 Nov 24 15:54 app.py
drwxr-xr-x 2 appsvc appsvc 4096 Nov 24 15:54 config
drwxr-xr-x 2 appsvc appsvc 4096 Nov 24 15:54 logs
drwxr-xr-x 2 appsvc appsvc 4096 Nov 24 15:54 uploads
/srv/app/logs:
total 4
-rw-r--r-- 1 appsvc appsvc 8 Nov 24 15:54 app.logThe permissions didn't change at all, but now appsvc is the owner, so the owner bits rw- apply to it. Run the su command again and it succeeds.
Make appsvc (user and group) the owner of /srv/app and everything inside it.
Expected result: su - appsvc -s /bin/bash -c 'echo hi >> /srv/app/logs/app.log' works.
Hint: the commands, explained
chown -R appsvc:appsvc /srv/appWhat each part does
chown -R appsvc:appsvc /srv/app: change the owner user:group to appsvc for the folder and (-R) everything inside it.- Check with
ls -l /srv/app: the 3rd and 4th columns now showappsvc appsvc.
Pattern: a shared team folder
chgrp devops /srv/team
chmod 775 /srv/teamEvery member of devops can now create files there. Two problems remain, and both have fixes:
- New files get the creator's primary group (
rohan:rohan), notdevops. The SGID bit on the folder fixes this; it's in the next lesson. - New files are
644, so teammates can't edit them. A umask of002fixes this; it's below.
A shared team folder
Let every member of the devops group create files in /srv/team, and nobody else.
Groups are how a team shares files. Give a folder to the team's group, give the group write permission, and every member can work in it, without opening it to everyone else on the server.
1. The team can't write
ls -ld /srv/team
id rohan
su - rohan -c 'touch /srv/team/rohan.txt'drwxr-xr-x 2 root root 4096 Nov 24 15:54 /srv/team
uid=1000(rohan) gid=1001(rohan) groups=1001(rohan),1000(devops)
touch: cannot touch '/srv/team/rohan.txt': Permission deniedRohan is in devops, but the folder's group is root, so he counts as "others" and gets r-x: he can look, not create.
2. Give the folder to the group, and let the group write
chgrp devops /srv/team
chmod 775 /srv/team
ls -ld /srv/team| Command | Does |
|---|---|
chgrp devops /srv/team | sets the folder's group to devops (same as chown :devops /srv/team) |
chmod 775 | owner rwx, group rwx, others r-x |
drwxrwxr-x 2 root devops 4096 Nov 24 15:54 /srv/team3. Try again
su - rohan -c 'touch /srv/team/rohan.txt'
ls -l /srv/teamtotal 4
-rw-r--r-- 1 root root 20 Nov 24 15:54 checklist.md
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:54 rohan.txtIt works. But look at the new file: it belongs to group rohan (his primary group), not devops, and it's rw-r--r--, so Priya can read it but not edit it. Step 4 fixes the permissions; the SUID, SGID and the Sticky Bit lesson shows how to make new files inherit the folder's group automatically.
Set up /srv/team so that members of devops can create files in it, and accounts outside the group can't:
- group
devops - group has
rwx - others have no write (use
775or770)
Expected result: su - rohan -c 'touch /srv/team/x' works, and appsvc can't create files there.
Hint: the commands, explained
chgrp devops /srv/team
chmod 775 /srv/teamWhat each part does
chgrp devops /srv/team: change only the group of the folder to devops (the owner stays the same).chmod 775: ownerrwx(7), grouprwx(7), othersr-x(5), so every devops member can create files there.
Finding files by owner
find /srv -user rohan # owned by rohan
find /srv -group devops # group is devops
find /srv -nouser # UID has no account (orphaned)
find /srv -nogroup # GID has no group
find / -xdev -nouser -exec chown rohan:devops {} + # adopt them allBefore you delete a user with userdel, run find / -xdev -user NAME to see what they leave behind, and decide who should own it.
Orphaned files
Find the files a deleted user left behind and hand them to rohan and the devops group.
A file stores its owner as a number (the UID), not a name. ls looks the number up in /etc/passwd to show a name. When a user is deleted without removing their files, the number stays on the files with no name to match: an orphaned file. If a new user later gets the same UID, they silently inherit those files, so audits look for them.
1. Spot the bare numbers
ls -la /srv/legacy/scriptstotal 20
drwxr-xr-x 2 1777 1777 4096 Nov 24 15:54 .
drwxr-xr-x 3 root root 4096 Nov 24 15:54 ..
-rw-r--r-- 1 1777 1777 27 Nov 24 15:54 backup.sh
-rw-r--r-- 1 1777 1777 28 Nov 24 15:54 cleanup.sh
-rw-r--r-- 1 1777 1777 27 Nov 24 15:54 rotate.sh1777 where a name should be: no account with UID 1777 exists any more. ls -n shows numbers for every file, which is how you'd confirm what's actually stored.
2. Find them all
find /srv -nouser| Test | Matches |
|---|---|
-nouser | files whose UID has no account |
-nogroup | files whose GID has no group |
-user rohan | files owned by rohan |
-group devops | files whose group is devops |
/srv/legacy/scripts
/srv/legacy/scripts/rotate.sh
/srv/legacy/scripts/backup.sh
/srv/legacy/scripts/cleanup.sh
/srv/legacy/README.txt3. Adopt them
chown -R rohan:devops /srv/legacy
ls -l /srv/legacy /srv/legacy/scripts/srv/legacy:
total 8
-rw-r--r-- 1 rohan devops 10 Nov 24 15:54 README.txt
drwxr-xr-x 2 rohan devops 4096 Nov 24 15:54 scripts
/srv/legacy/scripts:
total 12
-rw-r--r-- 1 rohan devops 27 Nov 24 15:54 backup.sh
-rw-r--r-- 1 rohan devops 28 Nov 24 15:54 cleanup.sh
-rw-r--r-- 1 rohan devops 27 Nov 24 15:54 rotate.shOn a big server you'd combine the two: find / -xdev -nouser -exec chown rohan:devops {} +.
- Save the list of orphaned paths under
/srvto/root/orphans.txt(one per line, asfindprints them). - Make rohan the owner and devops the group of everything under
/srv/legacy.
Expected result: /root/orphans.txt has 5 lines, and find /srv -nouser prints nothing.
Hint: the commands, explained
find /srv -nouser > /root/orphans.txt
chown -R rohan:devops /srv/legacyWhat each part does
find /srv -nouser: list files whose owner UID belongs to no existing user (left over after a user was deleted).>saves the list.chown -R rohan:devops /srv/legacy: give the whole folder to rohan and the devops group so someone owns it again.
umask: permissions for new files
When a program creates a file, it asks for 666 (rw-rw-rw-); for a directory, 777. The process's umask lists the bits to remove from that request.
How umask 022 shapes new files
umask is not subtracted arithmetically: it switches bits off. 666 with umask 033 gives 644, not 633.
umask
umask -S
umask 027
touch f2; mkdir d2; ls -ld f2 d2umask alone prints the mask; umask -S prints what's allowed in letters; umask 027 sets it for this shell.
0022
u=rwx,g=rx,o=rx
drwxr-x--- 2 root root 4096 Nov 24 15:54 d2
-rw-r----- 1 root root 0 Nov 24 15:54 f2With 027, group lost w and others lost everything: files 640, folders 750.
| umask | Files | Directories | Typical use |
|---|---|---|---|
022 | 644 | 755 | default on most systems |
002 | 664 | 775 | collaborative team setups |
027 | 640 | 750 | hardened servers |
077 | 600 | 700 | maximum privacy |
Where umask is set
Every process inherits its umask from its parent, so you set it where processes start:
| Where | Applies to |
|---|---|
umask 027 typed in a shell | that shell and what it starts, until you exit |
~/.profile or ~/.bashrc | one user's login shells |
UMASK in /etc/login.defs | default for all users at login |
UMask=0027 in a systemd unit | one service (covered in Writing systemd Services) |
UMASK 022
HOME_MODE 0750That's Ubuntu's /etc/login.defs: the system-wide default umask, and the mode new home folders get (which is why /home/dev was rwxr-x--- in the last lesson).
Copying files in as root and forgetting chown. scp, cp, git clone and tar -x run as root create root-owned files, and the service fails with "Permission denied" on its first write. Finish every deployment with the right chown.
chown -R on the wrong path. chown -R appsvc / (an empty variable in chown -R appsvc $DIR/) takes over the whole system and breaks sudo, SSH and more. Quote variables and double-check before pressing Enter.
Expecting umask to fix existing files. It only affects files created afterwards. Existing files need chmod.
Expecting umask to make scripts executable. New files never get x, whatever the umask. Use chmod +x.
"What's the difference between chmod and chown?" chmod changes the permission bits; chown changes who the owner and group are. Ownership decides which set of bits applies to you.
"Can a regular user chown a file to someone else?" No, only root. A user can chgrp to a group they belong to.
"What is umask? What does 022 give?" The bits removed from new files' permissions. Files start at 666, directories at 777; 022 gives 644 and 755.
"Your app returns 'Permission denied' writing its log after a deploy. What do you check?" Which user it runs as (ps -o user= -p PID or the unit's User=), ls -l of the log and its folder, and whether the deploy created them as root. Fix with chown on the folders it writes.
"How do you find files left behind by a deleted user?" find / -xdev -nouser (and -nogroup).
Default permissions with umask
Make the files Priya creates group-writable by default.
When a program creates a file, it asks for 666 (rw-rw-rw-); for a directory, 777. The umask is a mask of bits to take away from that. The common default 022 removes w from group and others:
files: 666 rw-rw-rw- directories: 777 rwxrwxrwx
minus umask 022 ----w--w- minus umask 022 ----w--w-
result 644 rw-r--r-- result 755 rwxr-xr-xEvery process has its own umask, inherited from the process that started it, which is why it's set in a user's shell startup file.
1. See your umask in action
umask
umask -S
touch f1; mkdir d1; ls -ld f1 d1umask -S shows what's allowed (the opposite of the mask) in letters.
0022
u=rwx,g=rx,o=rx
drwxr-xr-x 2 root root 4096 Nov 24 15:54 d1
-rw-r--r-- 1 root root 0 Nov 24 15:54 f12. Change it for this shell
umask 027
touch f2; mkdir d2; ls -ld f2 d2drwxr-x--- 2 root root 4096 Nov 24 15:54 d2
-rw-r----- 1 root root 0 Nov 24 15:54 f2027 removes w from the group and everything from others: files 640, directories 750. That's a common hardened server default.
| umask | New files | New directories | Used for |
|---|---|---|---|
022 | 644 | 755 | most systems' default |
002 | 664 | 775 | teams that edit each other's files |
027 | 640 | 750 | hardened servers |
077 | 600 | 700 | private: only you |
3. Make it permanent for a user
Typing umask only affects the current shell. To make it a user's default, put it in a file their shell runs at login. For a login shell on Ubuntu that's ~/.profile:
echo 'umask 002' >> /home/priya/.profile
su - priya -c 'umask; touch /tmp/p.txt; mkdir /tmp/pd; ls -ld /tmp/p.txt /tmp/pd'0002
-rw-rw-r-- 1 priya priya 0 Nov 24 10:24 /tmp/p.txt
drwxrwxr-x 2 priya priya 4096 Nov 24 10:24 /tmp/pdPriya's new files are now rw-rw-r--: anyone in the file's group can edit them. (The time differs because su - starts a fresh login environment with its own time zone settings.)
umask only affects new files. It never changes existing ones (that's chmod). And it can only take bits away: no umask makes a new file executable, which is why you always need chmod +x for a new script.
Make Priya's default umask 002 by adding it to /home/priya/.profile.
Expected result: su - priya -c umask prints 0002, and files she creates are rw-rw-r--.
Hint: the commands, explained
echo 'umask 002' >> /home/priya/.profileWhat each part does
umask 002: from now on, new files get664and new folders775, so the group can write too (the default022removes group write).>> /home/priya/.profile: priya's login runs this file, so the setting applies every time she logs in.
Summary
- Files store a UID and GID; names come from
/etc/passwdand/etc/group.ls -nshows the numbers. chown user,chown user:group,chown :group/chgrp group,chown -R. Only root can change the owner.- Services own what they write, read the rest. Teams share through a group-owned folder with group
rwx. find -user / -group / -nouser / -nogroupfind files by ownership.- New files:
666minus umask; directories:777minus umask.022→644/755,002→664/775,027→640/750. Set it in~/.profile,/etc/login.defsor a unit'sUMask=.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.