A useful permission change begins with a policy: who should be able to do what? It also needs a way back. In this lesson, you will give one synthetic note a stated mode and group, check the evidence, meet a second policy, then restore the note's actual original state.
The goal is deliberate control of one owned file. The file's text stays the same throughout. Its mode and group are evidence about that object; they do not by themselves demonstrate what a different logged-in user can do through its whole pathname.
1. Choose the tool that changes the intended property
| Tool | Property it changes | Boundary in this lesson |
|---|---|---|
chmod | Permission mode bits | Change the one owned note's basic mode. |
chgrp | Owning group | Choose a group already in the current process's group set. |
chown | Owning user, and optionally group | Explain its role; do not transfer ownership here. |
Changing mode does not change the owner or group. Changing the owning group does not itself choose the group's read/write bits. Ownership transfer is a different operation and normally requires privilege; it is not a substitute for identifying the correct mode or group.
The previous lesson's ordered owner, else matching group, else other model still applies. Directory search and other access controls still matter. Here you will inspect the current user's own note under the checked ordinary guest account, with no extended ACLs, special bits or privilege overrides.
State a complete mode policy
Each class has a digit: read contributes 4, write 2, execute 1.
The digits are ordered owner, group, other.
| Policy | Mode | Symbolic permissions |
|---|---|---|
| Owner reads/writes; group reads; other has none; no execute bits | 640 | rw-r----- |
| Owner reads/writes; group and other have none; no execute bits | 600 | rw------- |
A numeric mode specifies the complete ordinary policy, including absent bits. It is not a remembered guess about what a newly created file “should” have. You will record that file's real initial mode before applying a target mode.
Symbolic = sets the listed class's bits to exactly the stated rights:
u=rw sets owner read/write, g=r sets group read, and o= sets no other
rights. Commas combine the class clauses into one mode argument. Thus
u=rw,g=r,o= expresses the same ordinary policy as 640. Unlike the previous
lesson's single-bit u-x, these clauses replace the specified class's rights.
The no-special-bits fixture condition keeps this comparison within basic modes.
Creation defaults are a separate question
Bash's umask is a creation mask: it removes selected permission bits from
the mode an application requests for a new object. It does not retroactively
change existing objects, and it is not the mode of every file you create.
Invoking umask without operands inspects the current mask. This lesson never
sets it, exports a setting or edits a startup file. The observed stat result,
rather than an assumed mask, is your restoration baseline.
2. Prepare one owned note and capture its real state
Goal: apply a stated mode/group policy without changing the note's text,
then restore the exact original mode and numeric group. Use the ordinary
Ubuntu guest and the fresh workspace
~/linux-practice/change-owned-file-access/. Run one line at a time, inspect
each result and stop after an unexpected error before any following mutation.
Check the already taught identity, shell and command origins:
id -u
id -un
id
ps -p $ -o comm=
type cd pwd printf set declare unset umask id ps mkdir stat chmod chgrp cat ls rm rmdir
Require the expected nonzero ordinary user, Bash and unchanged current groups.
cd, pwd, printf, set, declare, unset and umask must be Bash
builtins. The utilities must resolve to expected guest executables; only an
understood color-only ls alias is acceptable. Reconcile an unexpected
function, alias or executable before continuing.
Use the taught read-only option inspection. Require allexport off so the
two temporary assignments below remain local, and errexit off for the
expected absence checks. Preserve any differing setting and reconcile the
baseline; do not change it here.
set -o
Inspect the creation mask with no operands:
umask
Record that value for awareness. You will neither change it nor infer a file's actual mode from it.
Two temporary local names will hold recovery values. As in the environment
lesson, declare -p NAME inspects a named shell variable, including variables
that are not exported. It also exposes attributes such as readonly or name
reference. Require each name to be absent before assigning anything:
declare -p VECTA_LAB_ACCESS_MODE
declare -p VECTA_LAB_ACCESS_GID
The expected result for each is an absent/not-found variable diagnostic. A displayed declaration means a collision, even if its value is empty. Stop and preserve an existing value, readonly name or name reference; do not unset it to make the exercise fit.
Inspect the shared parent and prospective workspace without trailing slashes:
ls -ld ~
ls -ld ~/linux-practice
ls -ld ~/linux-practice/change-owned-file-access
Require known ordinary owned parent directories. The new exercise path must be absent; an existing path belongs in the resume branch, not a reset by deletion. Reject links, unfamiliar ownership or extra access indicators.
mkdir ~/linux-practice/change-owned-file-access
ls -ld ~/linux-practice/change-owned-file-access
Require the newly created ordinary owned directory. Then enter it and inspect its location and complete inventory:
cd ~/linux-practice/change-owned-file-access
pwd
ls -a
Record the exact exercise path. Continue only when pwd matches it and the
directory contains only . and ... The following redirection creates a
fresh named file; it must not overwrite an existing note.
printf '%s\n' 'Workshop access note.' 'Keep this text unchanged.' > access-note.txt
ls -ld access-note.txt
cat access-note.txt
The exact two newline-terminated lines are:
Workshop access note.
Keep this text unchanged.
Require a regular file owned by your effective UID, with one hard link.
For this fixture and its owned directory, require ordinary ten-character
type/mode fields with the expected type and only ordinary r, w, x, -
positions. Stop for an added ACL, context or unknown indicator, or special
bits (s, S, t, T or an extra nonzero special-mode digit). Do not strip
features or broaden permissions to force a different setup into this task.
The directory must have owner rwx; the note must initially have owner read
and write so both verification and restoration are meaningful.
Use the taught stat -c fields to inspect the full mode, numeric owner/group,
group name and link count. %a is the numeric mode; %A is its symbolic form;
%u/%g are numeric IDs; %G is the resolved group name; %h is link count.
stat -c 'path=%n mode=%a (%A) uid=%u gid=%g group=%G links=%h' -- . access-note.txt
id -g
id -G
id -g identifies the current effective primary GID. id -G includes all
current effective and supplementary groups. Compare numeric IDs:
- The note's actual original GID must appear in
id -G. Ownership alone does not guarantee that you could restore an arbitrary original group. - The target is the primary GID printed by
id -g, also present inid -G. - If original and target GIDs are equal, the group operation may make no change. Record that honest no-op; do not invent another group or membership.
Do not proceed if either membership condition fails. No account, group or membership changes are needed for this exercise.
Capture recovery values before the first change
The environment lesson taught assignment without spaces around =. Quoted
command substitution "$(...)" supplies a command's output as one value;
the identity lesson used it for a group query. Here each stat produces just
one numeric field. Assign those observed values to the two names proven absent:
An assignment containing this command substitution has the substitution's
status. Use the taught immediate $? observation before another command
replaces it. First capture the mode:
VECTA_LAB_ACCESS_MODE="$(stat -c '%a' -- access-note.txt)"
printf 'Mode capture status: %s\n' "$?"
Require status 0; otherwise stop before the next capture or any mutation.
Then capture the group:
VECTA_LAB_ACCESS_GID="$(stat -c '%g' -- access-note.txt)"
printf 'Group capture status: %s\n' "$?"
Require status 0 again. Only then inspect both captured values:
printf 'Recorded mode=%s group=%s\n' "$VECTA_LAB_ACCESS_MODE" "$VECTA_LAB_ACCESS_GID"
Require both nonempty numeric values to match the earlier complete stat
record exactly, with no special bits in the mode. A failed capture or mismatch
is a stop condition before any permission or group change. Keep a separate
written record of the original mode, GID, owner UID, text and exact path;
local variables do not survive closing the shell.
GNU chgrp accepts a leading + on a numeric group operand to force numeric
interpretation, avoiding a numeric-looking group-name ambiguity. The + is
part of this group operand; -- separately ends option parsing. In
"+$(id -g)", double quotes keep the plus sign and the command's numeric
result together in one argument. "+$VECTA_LAB_ACCESS_GID" similarly uses
the recorded original GID.
Understand the recovery sequence before changing anything: restore the
recorded group with chgrp -- "+$VECTA_LAB_ACCESS_GID" access-note.txt, then
restore the complete original mode with
chmod -- "$VECTA_LAB_ACCESS_MODE" access-note.txt. Restoration uses the
recorded values, not an assumed 644. The original group membership check
is what makes the group restoration a bounded unprivileged operation.
3. Apply the first policy and inspect the result
Predict the result first: the target group becomes the current primary GID; owner receives read/write, group read only, other no rights, and nobody gets an execute bit. The owner UID and both text lines must remain unchanged. A group operation that names the existing GID is allowed to have no effect.
From the verified exercise path, recheck that the note is the same ordinary owned single-link file and that the two recorded values remain intact. Apply the numeric group first:
chgrp -- "+$(id -g)" access-note.txt
stat -c 'path=%n mode=%a (%A) uid=%u gid=%g group=%G links=%h' -- access-note.txt
Require its GID to equal the recorded primary target, with the owner UID unchanged. A command's lack of output alone is not sufficient. If the command fails or the evidence differs, stop; inspect identity, membership and target ownership rather than adding privilege or changing a directory tree.
Now apply the first complete mode:
chmod -- 640 access-note.txt
stat -c 'path=%n mode=%a (%A) uid=%u gid=%g group=%G links=%h' -- access-note.txt
cat access-note.txt
Require numeric 640, symbolic -rw-r-----, the target GID, unchanged owner
and link count, and the exact two original lines. This is a mode/group policy
check. It does not test another account's process groups or the search rights
on a complete path, so do not report that another user has demonstrated access.
Diagnose the mismatch before changing more state
| Observation | Evidence to inspect | Appropriate response |
|---|---|---|
chgrp reports an operation is not permitted. | Current owner UID and id -G, including both original and target GIDs. | Stop and reconcile the baseline; do not use recursive or privileged repair. |
| GID is unchanged. | Whether target equals original. | Record the expected no-op if equal; otherwise investigate. |
| Mode is correct but a different user cannot read. | That process's group and each traversed directory's search rights, plus other access controls. | The note's mode alone does not settle whole-path access. |
| Text changed. | Exact original two lines and correct target path. | The exercise has not met its invariant; stop and diagnose. |
Restoring 644 seems convenient. | The actual captured original mode. | Restore the recorded value, even when it differs from familiar defaults. |
Independent task: meet a second explicit policy
Keep the same target GID and unchanged text. Your second policy gives the owner and owning group read/write, other no rights, and no execute bits. Choose either the taught numeric notation or complete symbolic class clauses. Before acting, write the expected numeric and symbolic modes and identify which bit differs from the first policy.
Use the same verified note and exercise location. Do not change its owner,
choose a different group, create another account or alter directory modes.
Apply your chosen mode, then use stat and cat to prove the complete target
and content invariant. Keep both original recovery values; the independent
target is not a new restoration baseline.
Success check: your second policy has the intended full mode and unchanged
target GID, owner, link count and two text lines. Explain why its group write
bit is a mode decision and why chown is not needed. Then restore the original
state using the answer's bounded recovery sequence before cleanup.
Hint 1
Read/write contributes 4 + 2 for a class. No execute bit contributes nothing.
Hint 2
The other class needs zero rights. Compare the middle digit of the first and second policies; the owner and other digits need not change.
Hint 3
Use 660 or the complete symbolic clauses u=rw,g=rw,o=. Verification must
still compare the GID, owner and text; a plausible mode is not the whole check.
Answer and Reasoning
From the verified exercise path and on the rechecked owned note, one solution is:
chmod -- 660 access-note.txt
stat -c 'path=%n mode=%a (%A) uid=%u gid=%g group=%G links=%h' -- access-note.txt
cat access-note.txt
The mode is 660, or -rw-rw----: group write is the only ordinary bit added
to 640. The equivalent chmod -- 'u=rw,g=rw,o=' access-note.txt states all
three classes symbolically. Either meets the same basic policy; there is no
need to run both. The target GID and owner must be unchanged, with both text
lines still exact. chgrp selects the owning group, chmod selects mode
rights, and chown would address a different ownership-transfer question.
Restore the actual original group and complete mode
First verify pwd is the recorded exercise path. Reinspect the note's
ordinary identity and confirm the stored mode/GID still match the original
written record. Check current id -G again: the original GID must still be
in the process's group set before attempting the restoration.
pwd
ls -ld access-note.txt
id -G
printf 'Original mode=%s group=%s\n' "$VECTA_LAB_ACCESS_MODE" "$VECTA_LAB_ACCESS_GID"
Only after those observations agree, restore group first and mode second. Read each result before the next line; an unexpected failure stops the sequence and leaves cleanup pending.
chgrp -- "+$VECTA_LAB_ACCESS_GID" access-note.txt
chmod -- "$VECTA_LAB_ACCESS_MODE" access-note.txt
stat -c 'path=%n mode=%a (%A) uid=%u gid=%g group=%G links=%h' -- . access-note.txt
cat access-note.txt
Compare the full mode, numeric GID, owner UID and link count with the original record, plus both exact text lines. The directory mode must also remain as recorded. This is the completion check for restoration. Success of a single command, or a familiar-looking mode, cannot substitute for that comparison.
Cleanup only after the original state is verified
Keep the restored note if you want to retain your evidence. Otherwise inspect the owned parents and workspace without trailing slashes:
ls -ld ~/linux-practice
ls -ld ~/linux-practice/change-owned-file-access
Require the known ordinary owned directories, without link or access-feature changes. Then enter and check the exact recorded exercise location:
cd ~/linux-practice/change-owned-file-access
pwd
Continue only when pwd matches that recorded path. Reinspect the inventory
and single-link note:
ls -a
ls -ld access-note.txt
The sole file must be the restored owned note. The following confirmation
uses the earlier interactive-removal contract: establish the program's actual
documented affirmative/negative responses before use. Shown y/n responses
assume English confirmations; stop before the prompt if unsure.
rm -i -- access-note.txt
Approve only that exact disposable file. If declining, leave it and stop filesystem cleanup. There is one target here; in a multi-target invocation, a declined prompt would not cancel later prompts.
ls -a
Require only . and ... Then return to the shared root and inspect location
before removing the empty exercise directory:
cd ~/linux-practice
pwd
Require the exact known shared-root path. Recheck the named exercise entry is the same ordinary owned empty directory, then remove that one entry:
ls -ld change-owned-file-access
rmdir -- change-owned-file-access
Keep the shared practice root and other lessons. Once restoration has been
verified, whether you kept or removed the note, discard only the two local
variables that this task created. The taught unset -v removes variables,
not a same-named function:
unset -v VECTA_LAB_ACCESS_MODE VECTA_LAB_ACCESS_GID
Do not unset preexisting names, change the creation mask or edit a profile.
Resume from evidence, not remembered defaults
An existing exercise directory is a resume condition. Inspect its location, ordinary ownership, note identity and current mode/group before doing more. Do not recreate the note with redirection. In the same shell, confirm the two capture variables still match the original written record; do not capture the changed state again as if it were the original.
After a new shell, the local captures may be absent. With a trustworthy
original record, first repeat the collision checks. Assign each absent name
its actual recorded numeric value, using the already taught assignment
form, and print/compare it before the recovery commands. Do not use 640,
660 or a guessed group as a substitute for lost history. If the record,
identity, membership or target cannot be established, keep the fixture and
pause restoration rather than changing an unknown file.
Recover the recorded group and mode, verify text and all metadata, then continue or perform the named cleanup. The guest's creation mask, profiles, accounts and memberships never changed as part of this task.
Sources and other study views
- S18 — Bash: assignments, command substitution, variable inspection and umask
- S19 — GNU Coreutils: chmod, chgrp and numeric group operands
- S24 — Ubuntu: user and group context
The target is the declared Ubuntu guest with GNU Coreutils 9.4 and Bash 5.2. The upstream Coreutils manual can describe newer releases; this lesson uses bounded basic mode/group operations, not a general access-control procedure.
Use Revision to reconstruct the policy and restoration sequence and Quick Reference to retrieve the syntax after study.