Learn / Linux / Users & Permissions
Lesson 19 of 74

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.

20 min read Report a problem Hands-on lab included 6-question quiz

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 as postgres. 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 what www-data owns.
  • Teams share through groups. A deployment folder owned by the devops group, with group write permission, lets the whole team work in it.
  • People own their homes. /home/rohan belongs 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

root:root-rw-r--r-- app.log
appsvc writes?No: appsvc is "others", r-- only
chown appsvc:appsvc
appsvc:appsvc-rw-r--r-- app.log
appsvc writes?Yes: appsvc is the owner, rw-

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:

bash
ls -ln /srv/team

-n = show numeric UIDs and GIDs.

text
total 4
-rw-r--r-- 1 0 0 20 Nov 24 15:55 checklist.md

0 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 ls shows 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

bash
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
OptionMeaning
-Rrecursive: the folder and everything below it
-vverbose: print each change
-hchange a symlink itself instead of the file it points to
--reference=Fuse the owner and group of file F
text
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:55 a
-rw-r--r-- 1 root  root  0 Nov 24 15:55 b

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

text
changed ownership of '/srv/app/app.py' from root to appsvc

chgrp, and what normal users may change

chgrp devops file changes just the group. The rules for who may do what:

Actionrootfile's owner
change the owner (chown)yesno
change the group (chgrp)to any grouponly to a group they belong to
change permissions (chmod)yesyes
bash
su - rohan -c 'touch /tmp/r.txt; chgrp devops /tmp/r.txt; chgrp appsvc /tmp/r.txt; ls -l /tmp/r.txt'
text
chgrp: changing group of '/tmp/r.txt': Operation not permitted
-rw-r--r-- 1 rohan devops 0 Nov 24 10:25 /tmp/r.txt

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

PathOwnerModeWhy
/srv/app/app.pyroot:root644code: the service reads it, can't modify it
/srv/app/config/app.confroot:appsvc640config: readable by the service's group, editable by root
/srv/app/logs/appsvc:appsvc755the service writes logs here
/srv/app/uploads/appsvc:appsvc750the 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.

Try it · Task 1 of 4

Give the app its files

Make the appsvc service account the owner of everything under /srv/app.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /srv/app
id appsvc
text
total 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 uploads

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

bash
su - appsvc -s /bin/bash -c 'echo hi >> /srv/app/logs/app.log'
text
-bash: line 1: /srv/app/logs/app.log: Permission denied

The log is rw-r--r-- owned by root, so appsvc falls into "others" and only gets r--.

3. Hand everything over with chown -R

bash
chown -R appsvc:appsvc /srv/app
ls -l /srv/app /srv/app/logs
PartMeaning
chownchange owner
-Rrecursively: the folder and everything inside it
appsvc:appsvcnew owner : new group
/srv/appwhere to start
text
/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.log

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

2 · Your task

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
text
chown -R appsvc:appsvc /srv/app

What 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 show appsvc appsvc.
First start the lab machine in the terminal panel on the right.

Pattern: a shared team folder

bash
chgrp devops /srv/team
chmod 775 /srv/team

Every member of devops can now create files there. Two problems remain, and both have fixes:

  1. New files get the creator's primary group (rohan:rohan), not devops. The SGID bit on the folder fixes this; it's in the next lesson.
  2. New files are 644, so teammates can't edit them. A umask of 002 fixes this; it's below.
Try it · Task 2 of 4

A shared team folder

Let every member of the devops group create files in /srv/team, and nobody else.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -ld /srv/team
id rohan
su - rohan -c 'touch /srv/team/rohan.txt'
text
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 denied

Rohan 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

bash
chgrp devops /srv/team
chmod 775 /srv/team
ls -ld /srv/team
CommandDoes
chgrp devops /srv/teamsets the folder's group to devops (same as chown :devops /srv/team)
chmod 775owner rwx, group rwx, others r-x
text
drwxrwxr-x 2 root devops 4096 Nov 24 15:54 /srv/team

3. Try again

bash
su - rohan -c 'touch /srv/team/rohan.txt'
ls -l /srv/team
text
total 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.txt

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

2 · Your task

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 775 or 770)

Expected result: su - rohan -c 'touch /srv/team/x' works, and appsvc can't create files there.

Hint: the commands, explained
text
chgrp devops /srv/team
chmod 775 /srv/team

What each part does

  • chgrp devops /srv/team: change only the group of the folder to devops (the owner stays the same).
  • chmod 775: owner rwx (7), group rwx (7), others r-x (5), so every devops member can create files there.
First start the lab machine in the terminal panel on the right.

Finding files by owner

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

Before you delete a user with userdel, run find / -xdev -user NAME to see what they leave behind, and decide who should own it.

Try it · Task 3 of 4

Orphaned files

Find the files a deleted user left behind and hand them to rohan and the devops group.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -la /srv/legacy/scripts
text
total 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.sh

1777 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

bash
find /srv -nouser
TestMatches
-nouserfiles whose UID has no account
-nogroupfiles whose GID has no group
-user rohanfiles owned by rohan
-group devopsfiles whose group is devops
text
/srv/legacy/scripts
/srv/legacy/scripts/rotate.sh
/srv/legacy/scripts/backup.sh
/srv/legacy/scripts/cleanup.sh
/srv/legacy/README.txt

3. Adopt them

bash
chown -R rohan:devops /srv/legacy
ls -l /srv/legacy /srv/legacy/scripts
text
/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.sh

On a big server you'd combine the two: find / -xdev -nouser -exec chown rohan:devops {} +.

2 · Your task
  1. Save the list of orphaned paths under /srv to /root/orphans.txt (one per line, as find prints them).
  2. 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
text
find /srv -nouser > /root/orphans.txt
chown -R rohan:devops /srv/legacy

What 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.
First start the lab machine in the terminal panel on the right.

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

Program asksfile 666 rw-rw-rw-
remove 022
File gets 644rw-r--r--
Program asksdirectory 777 rwxrwxrwx
remove 022
Directory gets 755rwxr-xr-x

umask is not subtracted arithmetically: it switches bits off. 666 with umask 033 gives 644, not 633.

bash
umask
umask -S
umask 027
touch f2; mkdir d2; ls -ld f2 d2

umask alone prints the mask; umask -S prints what's allowed in letters; umask 027 sets it for this shell.

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

With 027, group lost w and others lost everything: files 640, folders 750.

umaskFilesDirectoriesTypical use
022644755default on most systems
002664775collaborative team setups
027640750hardened servers
077600700maximum privacy

Where umask is set

Every process inherits its umask from its parent, so you set it where processes start:

WhereApplies to
umask 027 typed in a shellthat shell and what it starts, until you exit
~/.profile or ~/.bashrcone user's login shells
UMASK in /etc/login.defsdefault for all users at login
UMask=0027 in a systemd unitone service (covered in Writing systemd Services)
text
UMASK		022
HOME_MODE	0750

That'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).

Try it · Task 4 of 4

Default permissions with umask

Make the files Priya creates group-writable by default.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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:

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

Every 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

bash
umask
umask -S
touch f1; mkdir d1; ls -ld f1 d1

umask -S shows what's allowed (the opposite of the mask) in letters.

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

2. Change it for this shell

bash
umask 027
touch f2; mkdir d2; ls -ld f2 d2
text
drwxr-x--- 2 root root 4096 Nov 24 15:54 d2
-rw-r----- 1 root root    0 Nov 24 15:54 f2

027 removes w from the group and everything from others: files 640, directories 750. That's a common hardened server default.

umaskNew filesNew directoriesUsed for
022644755most systems' default
002664775teams that edit each other's files
027640750hardened servers
077600700private: 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:

bash
echo 'umask 002' >> /home/priya/.profile
su - priya -c 'umask; touch /tmp/p.txt; mkdir /tmp/pd; ls -ld /tmp/p.txt /tmp/pd'
text
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/pd

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

2 · Your task

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
text
echo 'umask 002' >> /home/priya/.profile

What each part does

  • umask 002: from now on, new files get 664 and new folders 775, so the group can write too (the default 022 removes group write).
  • >> /home/priya/.profile: priya's login runs this file, so the setting applies every time she logs in.
First start the lab machine in the terminal panel on the right.

Summary

  • Files store a UID and GID; names come from /etc/passwd and /etc/group. ls -n shows 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 / -nogroup find files by ownership.
  • New files: 666 minus umask; directories: 777 minus umask. 022 → 644/755, 002 → 664/775, 027 → 640/750. Set it in ~/.profile, /etc/login.defs or a unit's UMask=.
Check your understanding

Quick quiz

Feedback