CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
  • Week 4
    • 1.The Structure of Permissions: rwx and Octal
    • 2.Ownership and umask
    • 3.Special Permissions: SetUID · SetGID · Sticky bit
    • 4.Directory Hierarchy and File System Management
    • 5.Week 4 Review: Permissions on One Page
  • Week 5
  • Week 6
  • Week 7
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 4/Day 2

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 4 · Day 2~18 min read

Day 2 - Ownership and umask

📌 Key Takeaways

  • Every file carries one owner and one owning group. These are the 3rd and 4th columns of ls -l.
  • chown changes the owner, and chgrp changes the group. You can also change both at once in the form chown user:group.
  • Only root can change the owner. A regular user can't hand a file over to someone else, even if it's their own.
  • umask is a mask that trims the permissions of newly created files. The default for regular users is 022.
  • The base values differ — umask is subtracted from 666 for files and 777 for directories. Files have no x from the start.

The Two Names Attached to a File

   -rw-r--r--  1  alice  dev  1024  Sep  3 09:00  report.txt
                   └─┬─┘  └┬┘
                     │     └── owning group (group)
                     └──────── owner (owner)
  • When you create a file, the creator becomes the owner and the creator's primary group becomes the owning group.
  • Which of the three permission slots (user/group/other) applies is decided by these two names.
  • What a file actually stores is not names but numbers (UID and GID). ls -l just looks them up in /etc/passwd and /etc/group and shows them as names. That is why files left behind after deleting an account show the number as-is in the owner slot.
$ id
uid=1001(alice) gid=1001(alice) groups=1001(alice),1500(dev)
$ touch a.txt
$ ls -l a.txt
-rw-r--r--. 1 alice alice 0 Sep  3 09:00 a.txt
  • Even though alice also belongs to the dev group, the new file's group is her primary group, alice. A secondary group only grants "eligibility to access"; it doesn't decide the group of a new file.

chown and chgrp

CommandWhat it does
chown alice fileSet the owner to alice
chown alice:dev fileOwner and group together
chown :dev fileGroup only (write only after the colon)
chgrp dev fileSet the group to dev
chown -R alice:dev /dataRecursively through subdirectories
sudo chown alice report.txt        # change the owner
sudo chown alice:dev report.txt    # owner + group
sudo chgrp dev report.txt          # group only
sudo chown -R www-data:www-data /var/www

The Position of the Colon Decides the Meaning

UsageOwnerGroup
chown alice fileto aliceunchanged
chown alice:dev fileto aliceto dev
chown :dev fileunchangedto dev
chgrp dev fileunchangedto dev (same result as above)
$ ls -l report.txt
-rw-r--r--. 1 root root 1024 Sep  3 09:00 report.txt
$ sudo chown alice report.txt
$ ls -l report.txt
-rw-r--r--. 1 alice root 1024 Sep  3 09:00 report.txt
$ sudo chown :dev report.txt
$ ls -l report.txt
-rw-r--r--. 1 alice dev 1024 Sep  3 09:00 report.txt
  • chown changes only the owner and group. The permissions rw-r--r-- and the modification time stay the same. To adjust permissions too, you have to use chmod separately.
  • It also accepts numbers instead of names — chown 1001:1500 file.
  • As with chmod, recursion is uppercase -R. It is the same for all three: chown, chgrp, and chmod.
OptionMeaning (common to chown and chgrp)
-RApply to everything under the directory
-vPrint the result for each file processed
-cPrint only what actually changed

If a regular user tries it, it gets blocked like this.

$ chown bob a.txt
chown: changing ownership of 'a.txt': Operation not permitted
$ chgrp dev a.txt          ← allowed because alice belongs to dev
$ chgrp wheel a.txt        ← not allowed for a group she doesn't belong to
chgrp: changing group of 'a.txt': Operation not permitted

Only root can change the owner. If a regular user could hand ownership of their own file to someone else, it would become possible to dodge disk quotas or plant files under someone else's name. On the other hand, a group change is something the owner can also do, but only when switching to a group they belong to.

ActionWho can do it
Change owner (chown)Only root
Change group (chgrp)root, or the owner switching to a group they belong to
Change permissions (chmod)root, or the file's owner

umask — The Mask That Trims Default Permissions

Why are new files made with 644 every time? It is because umask trims the permissions from a fixed base value.

umask          # 0022  ← check the current value
umask 077      # change the value (applies only to the current shell)
umask -S       # u=rwx,g=rx,o=rx  ← view in symbolic form

How to Calculate

Subtract umask from the base value. But the base value differs for files and directories.

TargetBase valueReason
File666 (rw-rw-rw-)To not give execute permission to new files
Directory777 (rwxrwxrwx)Because a directory needs x to be entered

With umask 022:

   file        666  -  022  =  644   (rw-r--r--)
   directory   777  -  022  =  755   (rwxr-xr-x)

With umask 077:

   file        666  -  077  =  600   (rw-------)
   directory   777  -  077  =  700   (rwx------)
umaskFileDirectoryCharacter
022644755Regular user default
002664775For group collaboration
077600700Personal use only, strictest
000666777No restriction — dangerous

No matter how you set umask to 0, files never get execute permission. That is because the base value is 666, so the x slot is empty from the start. This comes up as a trick question asking "if umask is 000, does the file become 777?" The answer is 666.

It Is "Masking," Not Subtracting

"Subtract from the base value" works out for values like 022 and 077, but what actually happens is the permissions turned on in umask are switched off in the base value. Permissions that weren't there to begin with don't change when switched off. This difference shows up when odd digits are in the umask.

With umask 033, simply subtracting for a file gives 666 − 033 = 633 (rw--wx-wx), but the actual result is different.

   base (file)    6 6 6   rw- rw- rw-
   umask          0 3 3   --- -wx -wx     ← switch off what's on here (w, x)
   ─────────────────────────────────
   result         6 4 4   rw- r-- r--     ← there was no x originally, so only w is switched off
umaskFileDirectoryWhat to note
033644744The file is not 633
027640750Blocks only write for the group, and blocks everything for others
007660770Blocks everything for others only
011666766A file has no x to switch off, so it stays 666
  • Tip: For a directory, just subtract (777 − umask). For a file, remove the x from that result. For 027, the directory is 750 (rwxr-x---), and the file is 640 (rw-r-----) with the x taken off.
  • So the larger the umask value, the narrower the permissions. 077 is the strictest and 000 is the loosest.

Notation and Scope of Application

$ umask
0022
$ umask -S
u=rwx,g=rx,o=rx
$ umask 027
$ touch new.txt; mkdir newdir
$ ls -ld new.txt newdir
-rw-r-----. 1 user user 0 Sep  3 09:00 new.txt
drwxr-x---. 2 user user 6 Sep  3 09:00 newdir
  • When umask shows four digits (0022), the leading 0 is the slot for special permissions. 022 and 0022 are the same value.
  • umask shows what to "block," and umask -S shows what "remains." 022 and u=rwx,g=rx,o=rx are the same setting written from opposite directions. The result of -S has the same shape as the directory permission (755).
  • umask acts only on what you create from now on. Changing umask doesn't change the permissions of files that already exist. Fix existing files with chmod.
  • umask only sets the default and is not a ceiling. If you run chmod 777 after creating, it becomes 777 regardless of umask.

Where the umask Value Lives

FileScope
/etc/profile, /etc/bashrcSystem-wide
~/.bashrc, ~/.bash_profilePer user
  • A umask changed by command is valid only in that shell and disappears when you log out. To keep it, you must write it in the config files above.
  • root's default umask is 022, and depending on the distribution, regular users may get 002.
  • A distribution that gives 002 is one where each user gets a private group with the same name as their own as their primary group. Since only they are in that group, opening write permission to the group (664) is not really dangerous.
  • If the exam asks for the "default umask" without conditions, answer 022 (file 644, directory 755).
Where it's setWho it applies toHow long it lasts
umask 027 on the command lineThat shell and programs run from itGone when the shell is closed
umask 027 in ~/.bashrcThat userApplied every time a new shell opens
/etc/profile · /etc/bashrcAll usersPersists (only root can edit)

Collaborating with Groups

For several people to use the same directory, put them in the same group and open up the group permissions.

sudo groupadd dev                  # create the group
sudo usermod -aG dev alice         # add a user (with -aG!)
sudo usermod -aG dev bob
sudo chgrp dev /project            # set the directory's group
sudo chmod 770 /project            # allow everything up to the group, block others
  • chmod 770 is rwxrwx---. The owner and group can do everything, and other users are blocked from access altogether.
  • However, if you stop here, the group of each person's files ends up all over the place. What solves this is SetGID, which we learn tomorrow.

Let's follow what actually happens.

$ ls -ld /project
drwxrwx---. 2 root dev 6 Sep  3 09:00 /project
$ id -gn                       ← alice's primary group
alice
$ touch /project/plan.txt
$ ls -l /project/plan.txt
-rw-r--r--. 1 alice alice 0 Sep  3 09:05 /project/plan.txt
Stumbling blockCauseHow to solve it
The file's group is alice, not devA new file follows the creator's primary groupchgrp dev file — you have to do it every time (tomorrow's SetGID automates it)
The group permission is r-- so bob can't editumask 022 masked the group's wchmod g+w file, or set umask to 002
A user just added to the group still can't accessThe group list is decided at loginLog out and log in again
  • For collaboration to go smoothly, all three have to be right — membership in the same group, the directory's group permission, and the new file's group plus group write permission.
  • Here is a one-line summary of what ownership, permissions, and umask each do.
ToolWhat it decidesWhen it acts
chown / chgrpWhose file it isOn files that already exist
chmodWhat they can doOn files that already exist
umaskWith what permissions a new file is bornOn files you create from now on

Tomorrow we cover the special permissions that sit outside the three regular slots — SetUID, SetGID, and the sticky bit.

📖 Glossary

  • Owner (owner) : The user who owns the file. The third column of ls -l.
  • Owning group (group) : The group attached to the file. The fourth column of ls -l.
  • chown : The command that changes the owner. Only root can run it.
  • chgrp : The command that changes the owning group.
  • umask : A mask that sets the value to trim from the permissions of newly created files and directories.
  • Base value : The permissions before umask is applied. 666 for files and 777 for directories.
  • Primary group : The group automatically attached to files a user creates. Decided by the GID field of /etc/passwd.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which of the following commands changes a file's owner and owning group at once?

Question 2

When the umask value is 022, which of the following is the permission of a newly created regular file?

Question 3

Which of the following has the authority to change a file's owner to another user?

Question 4

When the umask value is 077, which of the following is the permission of a newly created directory?

Question 5

Which of the following commands checks the umask value set in the current shell?

PreviousThe Structure of Permissions: rwx and OctalWeek 4 · Day 1Next Special Permissions: SetUID · SetGID · Sticky bitWeek 4 · Day 3

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Takeaways
  • The Two Names Attached to a File
  • chown and chgrp
  • The Position of the Colon Decides the Meaning
  • umask — The Mask That Trims Default Permissions
  • How to Calculate
  • It Is "Masking," Not Subtracting
  • Notation and Scope of Application
  • Where the umask Value Lives
  • Collaborating with Groups
  • Glossary
  • Practice Questions