Changing ownership and permissions
On this page
Your report should be readable by a project teammate, but nobody outside that group. A permission string describes the policy; it does not prove that the intended account can actually use the file.
By the end, you can change ordinary permission bits symbolically and numerically, change owning users and groups, and verify the result with both metadata and real access tests.
Before you start
Section titled “Before you start”Finish File versus directory permissions. Keep your B04 lab directory. Use a disposable lab VM or container. The names sam and team are reserved for this exercise; do not reuse accounts or groups containing somebody else’s work. Begin from a clean lab state or complete Reset this lesson first.
Some setup commands need administration rights. Prefixing one command with sudo runs that command as root after your own password, if policy permits it. Your shell remains learner. Type your lab account’s password at the prompt; nothing appears while you type. B11.5 explains delegation and authentication in detail.
mkdir -p ~/lab/b11/changescd ~/lab/b11/changessudo useradd -m -s /bin/bash samsudo groupadd teamsudo usermod -aG team samsudo usermod -aG team learnerThis creates a practice account with a home directory and Bash shell, creates team, then adds each account to that supplementary group. -aG adds membership without replacing the existing supplementary memberships. Replace the final learner only if your VM account has another name. Do not assign a password to sam; the access tests use sudo from your authorized account.
learner@rootvolt:~/lab/b11/changes$ sudo useradd -m -s /bin/bash sam[sudo] password for learner: learner@rootvolt:~/lab/b11/changes$ Steps (5)
- Type
sudo useradd -m -s /bin/bash sam - Press Enter
- Output:
[sudo] password for learner: - Type the password (the terminal shows nothing)
- Press Enter — Type the password (not shown) and press Enter
Check the current shell against the configured accounts:
id -Gnlearner sudoid learneruid=1001(learner) gid=1001(learner) groups=1001(learner),27(sudo),1003(team)id samuid=1002(sam) gid=1002(sam) groups=1002(sam),1003(team)Notice that id -Gn does not include team, while the named-account lookup does. Configuration changed; the already-running shell’s supplementary groups did not. Keep this shell open so the later comparison uses the same identity.
State the owner, group, and other permissions
Section titled “State the owner, group, and other permissions”Create the disposable report and establish its starting mode:
printf 'Team review\n' > report.txtchmod 644 report.txtls -l report.txt-rw-r--r-- 1 learner learner 12 Nov 14 15:53 report.txtRead the owner and group names as well as the bits. Dates, IDs, and some metadata can differ on your VM; the prepared permission fields are what you compare here.
Use a symbolic mode to state the desired permissions by class:
chmod u=rw,g=r,o= report.txtls -l report.txt-rw-r----- 1 learner learner 12 Nov 14 15:53 report.txtu selects the owning user, g the owning group, and o everyone else. Each = replaces that class’s ordinary permissions. o= has no letters after it, so it clears all three other bits. The result keeps owner read/write, group read, and no other access.
The operators from B11.3 also work here: + adds selected bits and - removes them while leaving the other ordinary bits alone. Use explicit classes so you can see whose permissions you are changing. chmod changes the mode; it does not change the owning account or group.
Read three octal digits as three fields
Section titled “Read three octal digits as three fields”A numeric mode uses base-eight, or octal, digits. Add these values within each class:
| Bit | Value |
|---|---|
| Read | 4 |
| Write | 2 |
| Execute/search | 1 |
Three digits specify owner, group, and other, in that order. Owner read/write is 4 + 2 = 6; group read is 4; no bits is 0. Thus 640 expresses the same ordinary bits as the symbolic mode above.
Predict first: does changing 640 to 600 remove the owner’s ability to read?
chmod 600 report.txtls -l report.txtRun it, then compare with your guess
-rw------- 1 learner learner 12 Nov 14 15:53 report.txtNo. Only the middle digit changed: group read was removed. Owner rw- remains.
Restore the group-read bit with the complete numeric mode, then verify it:
chmod 640 report.txtls -l report.txt-rw-r----- 1 learner learner 12 Nov 14 15:53 report.txtNumeric modes describe the full set of ordinary permissions. Symbolic additions or removals are useful when you intend to change only selected bits. Neither form changes the file’s text.
Change the group, then test actual access
Section titled “Change the group, then test actual access”The file still belongs to group learner. Change only its owning group:
sudo chgrp team report.txtls -l report.txt-rw-r----- 1 learner team 12 Nov 14 15:53 report.txtCompare this with the preceding listing: the group changed to team; the owner and rw-r----- stayed the same. An ordinary owner can change a file to a group available to their process. This running shell does not yet have team, so this exercise uses sudo for the group change.
Run the same read as the owner, a teammate, and an unrelated account:
cat report.txtTeam reviewsudo -u sam cat report.txtTeam reviewsudo -u nobody cat report.txtcat: report.txt: Permission deniedsudo -u sam runs this command as sam, not as root. The teammate succeeds through group team; nobody falls into the other class and fails. These commands inherit the existing practice working directory. They test access there; they do not grant traversal through your home directory from elsewhere.
In words, each account uses one applicable class. This model assumes the inherited practice working directory is accessible, with no additional ACL rules or privileged bypass.
learner@rootvolt:~/lab$ cd ~/lab/b11/changeslearner@rootvolt:~/lab/b11/changes$ ls -l report.txt-rw-r--r-- 1 learner learner 12 Nov 14 15:53 report.txtlearner@rootvolt:~/lab/b11/changes$ chmod u=rw,g=r,o= report.txtlearner@rootvolt:~/lab/b11/changes$ ls -l report.txt-rw-r----- 1 learner learner 12 Nov 14 15:53 report.txtlearner@rootvolt:~/lab/b11/changes$ sudo chgrp team report.txt[sudo] password for learner: learner@rootvolt:~/lab/b11/changes$ ls -l report.txt-rw-r----- 1 learner team 12 Nov 14 15:53 report.txtlearner@rootvolt:~/lab/b11/changes$ sudo -u sam cat report.txtTeam reviewlearner@rootvolt:~/lab/b11/changes$ sudo -u nobody cat report.txtcat: report.txt: Permission deniedlearner@rootvolt:~/lab/b11/changes$ cat report.txtTeam reviewlearner@rootvolt:~/lab/b11/changes$ Files ~/lab/b11/changes
- ~
- lab/b11/changes/cwd
- report.txtargument-rw-r-----
Steps (27)
- Type
cd ~/lab/b11/changes - Press Enter
- Type
ls -l report.txt - Press Enter
- Output:
-rw-r--r-- 1 learner learner 12 Nov 14 15:53 report.txt - Type
chmod u=rw,g=r,o= report.txt - Press Enter
- Type
ls -l report.txt - Press Enter
- Output:
-rw-r----- 1 learner learner 12 Nov 14 15:53 report.txt - Type
sudo chgrp team report.txt - Press Enter
- Output:
[sudo] password for learner: - Type the password (the terminal shows nothing)
- Press Enter — Type the password (not shown) and press Enter
- Type
ls -l report.txt - Press Enter
- Output:
-rw-r----- 1 learner team 12 Nov 14 15:53 report.txt - Type
sudo -u sam cat report.txt - Press Enter
- Output:
Team review - Type
sudo -u nobody cat report.txt - Press Enter
- Output:
cat: report.txt: Permission denied - Type
cat report.txt - Press Enter
- Output:
Team review
The Files panel follows permission changes. Owning names are verified in the terminal listings; the panel does not display ownership or simulate another account’s access.
Transfer ownership without changing the mode
Section titled “Transfer ownership without changing the mode”The preceding listing is your before-state. Transfer the file to sam while retaining group team:
sudo chown sam:team report.txtls -l report.txt-rw-r----- 1 sam team 12 Nov 14 15:53 report.txtchown user:group sets both owning identities. Changing the owning user requires privilege on this system, even when you originally owned the file. The mode still grants owner read/write and group read.
Predict first: can the original learner shell still read it, merely because its configured account was added to team?
cat report.txtRun it, then compare with your guess
cat: report.txt: Permission deniedNo. This shell no longer matches the owner and still lacks the supplementary group. It uses the other field, which grants nothing.
Start a fresh command as learner and inspect its groups before reading:
sudo -u learner iduid=1001(learner) gid=1001(learner) groups=1001(learner),27(sudo),1003(team)sudo -u learner cat report.txtTeam reviewThis process obtains the configured team membership. The original shell’s groups remain unchanged. On your VM, a fresh login normally obtains the updated list; do not assume every existing terminal changed with the account database.
Restore ownership for the challenge, verifying the transition from the preceding sam-owned state:
sudo chown learner:team report.txtls -l report.txt-rw-r----- 1 learner team 12 Nov 14 15:53 report.txtMistakes to recognize
Section titled “Mistakes to recognize”Reading the other field when a group match applies. The successful sam read used group r--. Permissions from different classes are not added together.
Changing permissions when membership is stale. Compare the named-account lookup with the actual process identity before broadening access. The learner failure above came from its unchanged running group list.
Treating a successful chmod as complete verification. Inspect the resulting bits and test the intended operation as the intended account. A valid mode still cannot repair a blocked parent path from B11.3.
Try it yourself
Section titled “Try it yourself”Goal: Allow the existing owning group to write the report too, keeping the owner unchanged and other access absent. Add only the necessary bit, then verify the result.
Hint
Use the symbolic addition operator for the group class. Compare with the before-state rw-r-----.
Solution
chmod g+w report.txtls -l report.txt-rw-rw---- 1 learner team 12 Nov 14 15:53 report.txtThe result is rw-rw----, equivalent to ordinary mode 660. Group write permits modifying contents; it does not grant execution.
Reset this lesson
Section titled “Reset this lesson”First return the practice file to your normal owning identities and inspect the result:
sudo chown learner:learner report.txtls -l report.txt-rw-rw---- 1 learner learner 12 Nov 14 15:53 report.txtcd ~/lab/b11ls -AR changesrm -r ~/lab/b11/changessudo userdel -r samsudo groupdel teamgetent passwd samgetent group teamgetent queries the configured account/group databases. Both final lookups print no entries and return status 2 in the verified reset. userdel reports that sam’s mail spool is absent in this container; the account and home are removed. Repeating setup starts from those absent names. Do not remove unrelated accounts with these commands.
- Symbolic modes select classes and add, remove, or replace permission bits.
- Numeric modes assign owner, group, and other bits using values 4, 2, and 1.
chgrpchanges the owning group; privilegedchownchanges the owning user, optionally with its group.- Verify metadata and actual access, using the group list of the process doing the work.
Next, Using sudo and su separates delegated commands, target-account authentication, and the shell opening a redirection.
Further reading
Section titled “Further reading”- On your system:
man chmod,man chown,man chgrp,man sudo,man usermod, andman userdel. - GNU Coreutils: Setting Permissions, chown invocation, and chgrp invocation.
- Shadow utilities: usermod and userdel.