Managing Permissions
Theory Questions
1. Set the group ownership of all new files created during a session.
- Command(s):
newgrp sales
# OR
sg sales -c "$SHELL"
- Why: The
newgrpcommand initializes a new subshell session with the primary group GID temporarily reassigned to the specified group (sales). All files created within this subshell default to this new group owner. - Gotchas:
newgrpspawns a child shell session. Exiting the subshell (or closing the terminal) reverts your primary group GID to its original value configured in/etc/passwd. - Verify:
id -gn
(Returns sales as the active primary group name)
2. Find all files on a system owned by a specific user.
- Command(s):
find / -user joana 2>/dev/null
- Why:
find / -user <username>traverses the entire directory tree from root (/) looking for inodes owned by the target user account. Redirecting2>/dev/nullsuppresses permission denied warnings generated by pseudo-filesystems like/procor/sys. - Gotchas: Omitting
2>/dev/nullwhen running non-root searches, which clutters output with hundreds of access permission errors. - Verify:
find / -user joana -ls 2>/dev/null | head -n 10
3. Change group ownership of a file using various commands.
- Command(s):
chgrp sales /data/reports.txt
# OR
chown :sales /data/reports.txt
# OR
chown .sales /data/reports.txt
- Why: Both
chgrpandchownmodify inode ownership metadata. Prefixing the group name with a colon (:sales) or dot (.sales) inchowntargets group ownership exclusively while leaving user ownership unchanged. - Gotchas: Omitting the colon or dot when executing
chown sales /data/reports.txt, which attempts to set the user owner tosalesinstead of the group owner. - Verify:
ls -l /data/reports.txt
4. Set read and write permissions for user and group owners, with no permissions for others.
- Command(s):
chmod 660 /data/file.txt
# OR
chmod u=rw,g=rw,o= /data/file.txt
- Why: Octal calculation: User (
r+w= 4+2 = 6), Group (r+w= 4+2 = 6), Others (none= 0), resulting in660(rw-rw----). - Gotchas: Using octal
660on directories. Removing the execute (x) bit from a directory prevents users from traversing (cd) into it or accessing its contents. Directories require770for equivalent read/write access. - Verify:
ls -l /data/file.txt
5. Apply execute permission recursively to subdirectories only (not files).
- Command(s):
chmod -R a+X /data/shared/
# OR
find /data/shared/ -type d -exec chmod +x {} +
- Why: Capital
Xin relative mode applies the execute bit only to directories (or files that already have an execute bit set elsewhere), preventing regular non-executable text files from becoming executable binaries. - Gotchas: Executing
chmod -R +x /data/shared/with a lowercasex, which turns every standard text file inside the path executable. - Verify:
ls -ld /data/shared/*/
6. Set the SGID permission on a directory.
- Command(s):
chmod g+s /data/shared/
# OR
chmod 2775 /data/shared/
- Why: The Set Group ID (SGID, octal value
2000) flag on a directory forces all newly created files and subdirectories underneath it to inherit the directory’s group owner, rather than the primary group of the user creating the file. - Gotchas: Setting SGID on regular files vs. directories. On directories, it alters group inheritance; on binary executable files, it executes the binary with the file’s group owner privileges.
- Verify:
ls -ld /data/shared/
(Group permission bits display drwxrwsr-x with an s in the group execute position)
7. Interpret the SUID permission on the passwd program file.
- Command(s):
ls -l /usr/bin/passwd
- Why: The SUID (Set User ID, octal
4000) flag causes the executable file to run with the security context and privileges of the file’s owner (root) rather than the calling user, allowing unprivileged accounts to update/etc/shadow. - Gotchas: Attempting to set SUID on interpreted shell scripts (e.g.,
.shscripts). Linux kernel security policies ignore SUID flags on shell scripts to prevent environment variable hijacking. - Verify: Output displays
rwsr-xr-xwith ansin the user execute position:
-rwsr-xr-x. 1 root root 68240 /usr/bin/passwd
8. Set a different umask for root versus ordinary users.
- Command(s):
Configure persistent profile drop-in under
/etc/profile.d/umask.sh:
cat << 'EOF' > /etc/profile.d/umask.sh
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi
EOF
- Why: RHEL defaults assign
umask 002to unprivileged users with matching User Private Groups (UPGs), resulting in default file permissions of664(rw-rw-r--) and directory permissions of775(rwxrwxr-x). Root and system accounts receiveumask 022(files644, dirs755). - Gotchas: Setting umask inside
~/.bashrcaffects only interactive subshells for that specific account; global profile settings under/etc/profile.d/guarantee systemic application across all users. - Verify:
umask
9. Calculate the umask value for specific permission requirements.
- Command(s) / Calculation:
Base Directory Mode: 777 (rwxrwxrwx) - Target Dir Mode: 750 (rwxr-x---) = Umask: 027
Base File Mode: 666 (rw-rw-rw-) - Umask (027) = Target File Mode: 640 (rw-r-----)
Set umask:
umask 027
- Why: Umask acts as a subtraction bitmask against default base permissions (directories base
777, regular files base666). A umask of027strips group write (2) and all permissions for others (7). - Gotchas: Subtracting umask values from
777when calculating regular file permissions. Regular files never receive default execute permissions (+x) upon creation, so base666must be used. - Verify:
umask 027
touch testfile && mkdir testdir
ls -ld testdir testfile
10. Check all attributes currently set on a file.
- Command(s):
lsattr /data/reports.txt
- Why:
lsattrqueries extended ext4/XFS filesystem attributes (such asiimmutable,aappend-only,ssecure deletion) managed bychattr. - Gotchas: Standard
ls -lonly displays standard POSIX mode bits and ACL indicators (+), completely hidingchattrfilesystem attributes. - Verify:
lsattr /data/reports.txt
11. Use chown to set only the group owner of a file.
- Command(s):
chown :sales /data/file.txt
- Why: The leading colon (
:sales) tellschownto modify group ownership exclusively while leaving the user owner unchanged. - Gotchas: Running
chown sales /data/file.txtwithout the colon, which changes the user owner instead of the group owner. - Verify:
ls -l /data/file.txt
12. Find all files owned by a specific user.
- Command(s):
find /var -user beatrix 2>/dev/null
- Why: Uses the
-userpredicate to search target paths for files with matching user owner metadata. - Gotchas: Searching network mounts (e.g., NFS shares) without limiting search scope; use
find /var -xdev -user beatrixto constrain searches to local filesystems. - Verify:
find /var -user beatrix
13. Apply read, write, and execute permissions to all files in a directory for user and group, with no permissions for others.
- Command(s):
chmod -R 770 /data/account/
# OR
chmod -R u=rwx,g=rwx,o= /data/account/
- Why: Octal
770setsrwx(4+2+1=7) for user,rwx(7) for group, and---(0) for others recursively across all subdirectories and contents. - Gotchas: Granting execute bits (
7) indiscriminately to all regular data files inside a path. If files should not be executable, separate directory modes (chmod -R 770) from file modes (find /data/account/ -type f -exec chmod 660 {} +). - Verify:
ls -la /data/account/
14. Add execute permission to a file in relative permission mode.
- Command(s):
chmod +x /usr/local/bin/script.sh
# OR
chmod a+x /usr/local/bin/script.sh
- Why: Relative mode (
+x) appends the execute bit to existing mode flags without overriding or wiping read/write permissions. - Gotchas: Running absolute octal commands like
chmod 755when you only want to add execute permissions without altering existing user/group permissions. - Verify:
ls -l /usr/local/bin/script.sh
15. Ensure new files in a directory inherit the directory’s group owner.
- Command(s):
chmod g+s /data/sales/
- Why: The SGID bit (
g+s) forces new files created inside/data/sales/to automatically inherit the group ownership of/data/sales/regardless of who creates them. - Gotchas: Expecting SGID to retroactively update existing files inside the directory; SGID applies strictly to new files created post-configuration. Use
chgrp -Rfor existing files. - Verify:
ls -ld /data/sales/
16. Restrict file deletion so users can only delete their own files (or files in directories they own).
- Command(s):
chmod +t /data/shared/
# OR
chmod 1777 /data/shared/
- Why: The Sticky Bit (octal
1000) prevents unprivileged users from unlinking, deleting, or renaming files owned by other users within a shared writable directory. - Gotchas: Applying the sticky bit without write permissions for group/others (
w). The sticky bit works in conjunction with write permissions to restrict deletion access. - Verify:
ls -ld /data/shared/
(Permissions display drwxrwxrwt with a t in the others execute position)
17. Set a umask so that “others” never get any permissions on new files.
- Command(s):
umask 007
# OR
umask 027
- Why: A umask ending in
7(e.g.,007or027) strips read (4), write (2), and execute (1) bits for the “others” class during file and directory creation. - Gotchas: Setting umask in an active shell session only affects that session; persistent system-wide defaults must be declared in
/etc/profile.d/custom-umask.sh. - Verify:
umask
18. Prevent accidental deletion of a specific file.
- Command(s):
chattr +i /etc/important.conf
- Why: The immutable attribute (
+i) locks the file at the filesystem level. Even therootuser cannot delete, rename, modify, or create a hard link to an immutable file. - Gotchas: Forgetting why editing a file fails with
Operation not permittedeven when running asroot. You must remove the attribute (chattr -i) before making changes. - Verify:
lsattr /etc/important.conf
(Output displays ----i--------- /etc/important.conf)
19. Search for all files with the SUID permission set.
- Command(s):
find / -perm -4000 -type f 2>/dev/null
- Why: The predicate
-perm -4000matches files with the SUID bit (4000) enabled, regardless of any other permission bits set on the file. - Gotchas: Using
-perm 4000(without the hyphen), which searches for files matching only mode4000exactly (and no other permissions), returning zero results. - Verify:
find /usr/bin -perm -4000 -type f
20. Check if any extended attributes have been applied to a file.
- Command(s):
lsattr /data/file.txt
# AND/OR for POSIX ACLs and extended user attributes:
getfacl /data/file.txt
getfattr -d /data/file.txt
- Why:
lsattrchecks ext4/XFS filesystem flags (+i,+a),getfaclchecks Access Control Lists (+), andgetfattrdisplays extended user namespace attributes (user.*). - Gotchas: Confusing
chattrfilesystem attributes withsetfaclAccess Control Lists. - Verify:
getfacl /data/file.txt
Lab Tasks
Lab 7.1: Permissions Practice
Task: Create directories /data/account and /data/sales. Set sales as the group owner of /data/sales and account as group owner of /data/account. Configure permissions so root and group owners have full access, with no permissions for others. Ensure all new files inherit the directory’s group owner (SGID) and users can only remove files they own (sticky bit).
- Command(s):
# Step 1: Ensure group accounts exist
groupadd sales 2>/dev/null || true
groupadd account 2>/dev/null || true
# Step 2: Create directories
mkdir -p /data/account /data/sales
# Step 3: Set ownership (User: root, Group: account/sales)
chown root:account /data/account
chown root:sales /data/sales
# Step 4: Apply permissions (SGID=2000 + Sticky Bit=1000 -> Special Mode=3000; rwx for user/group=770)
chmod 3770 /data/account
chmod 3770 /data/sales
-
Why:
-
chown root:account: Assignsrootas user owner andaccountas group owner. -
Special Mode
3770(2000+1000+0770): -
2000(SGID): Enforces group ownership inheritance on all newly created child files/directories. -
1000(Sticky Bit): Prevents users from deleting or renaming files owned by other members within the directory. -
0770: Grantsrwx(full access) torootand the group owner, and---(zero access) to others. -
Gotchas:
-
Applying
2770instead of3770(forgets the sticky bit requirement). -
Leaving permissions open to others (
775or777), which breaks the requirement that others have no permissions. -
Forgetting that SGID must be applied to the directory before users create new files inside it.
-
Verify Permissions:
ls -ld /data/account /data/sales
(Output must display drwxrwsrwt. 2 root account and drwxrwsrwt. 2 root sales)
- Verify Functional Behavior & Persistence:
# Test SGID Inheritance (create test file as regular user in sales group):
su - joana -c "touch /data/sales/testfile_joana"
ls -l /data/sales/testfile_joana
(Confirms testfile_joana has group owner sales)
# Test Sticky Bit Deletion Restriction (attempt to delete another user's file):
su - john -c "rm -f /data/sales/testfile_joana"
(Returns Operation not permitted, confirming sticky bit deletion protection)
# Confirm persistence across system reboots:
reboot
# Post-reboot check:
ls -ld /data/account /data/sales
(Confirms special permissions 3770 and group ownership persist across system reboots)