Red Hat System Administration I · RH124

Chapter 11

Controlling Access to Files

rwx permissions · chmod and chown · umask · Special bits · ACLs
CIS126RH — Mesa Community College
Interpret and change standard file permissions and ownership, control default permissions with umask, and use special permissions and access control lists.

Key Commands

  • ls -l
  • chmod
  • chown
  • chgrp
  • umask
  • setfacl
  • getfacl
ClassApplies To
Owner (u)The user who owns the file
Group (g)Members of the file's group
Other (o)Everyone else on the system
PermissionOn a FileOn a Directory
Read rView file contentsList directory contents
Write wModify file contentsCreate, rename, or delete entries inside
Execute xRun the file as a program/scriptEnter (cd into) the directory
Exam NoteOn a directory, x is what lets you enter it and reach the files inside. r alone only lists the names.
$ ls -l notes.txt
-rw-r--r--. 1 sarah developers 482 Aug  5 09:14 notes.txt
FieldMeaning
-File type (- file, d directory, l symlink)
rw-Owner's permissions: read + write, no execute
r--Group's permissions: read only
r--Other's permissions: read only
sarahOwning user
developersOwning group
Tip — The Trailing DotA . after the permission string means the file has an SELinux context. That's normal on RHEL.

Combine a class (u, g, o, a), an operator (+, -, =), and permissions.

# Add execute permission for the owner
chmod u+x script.sh

# Remove write permission for group and other
chmod go-w notes.txt

# Set exact permissions for all three classes
chmod u=rw,g=r,o= notes.txt

# Add read permission for everyone
chmod a+r notes.txt
ValuePermissions
7rwx (4+2+1)
6rw- (4+2)
5r-x (4+1)
4r-- (4)
0--- (none)
# Owner: rwx, Group: r-x, Other: r-x
# common for scripts/directories
chmod 755 deploy.sh

# Owner: rw-, Group: r--, Other: ---
# common for private config files
chmod 640 secrets.conf

# Apply recursively
chmod -R 755 /var/www/html
Warning — -R Hits Files and Directories AlikeThey usually need different modes. Use find with -type f or -type d to be precise.
# Change the owning user
chown sarah notes.txt

# Change the owning group only
chgrp developers notes.txt

# Change both user and group at once
chown sarah:developers notes.txt

# Change ownership recursively
chown -R sarah:developers /home/sarah/project
NoteOnly root can change a file's owning user. Regular users can change a file's group, but only to a group they belong to.

The umask removes permissions from a maximum of 666 for files and 777 for directories.

# Show the current umask
umask

# Common default: 022
# New file:   666 - 022 = 644 (rw-r--r--)
# New dir:    777 - 022 = 755 (rwxr-xr-x)

# Set a stricter umask for the current shell session
umask 027
Note — Remove, Don't SubtractThe umask clears permission bits. With 027, new files get 640, not 639.
Tip — No Execute by DefaultNew regular files never get execute permission by default. Grant it on purpose with chmod.
BitSymbolOn a FileOn a Directory
SUID (4000)s in owner's execute slotRuns with the file owner's privileges, not the invoking user'sNo effect
SGID (2000)s in group's execute slotRuns with the file's group privilegesNew files inside inherit the directory's group
Sticky bit (1000)t in other's execute slotNo standard effectOnly the file's owner (or root) can delete files inside, even with directory write access
# Set SUID (e.g. why /usr/bin/passwd works for regular users)
chmod u+s /usr/bin/passwd

# Set SGID on a shared project directory so new files inherit its group
chmod g+s /srv/shared-project

# Set the sticky bit — this is exactly how /tmp is configured
chmod +t /tmp

# Numeric form: leading 4th digit
chmod 4755 /usr/bin/passwd
chmod 2775 /srv/shared-project
chmod 1777 /tmp
Warning — SUID Root Is PowerfulA SUID root program runs with full root privileges no matter who starts it. Careless SUID, especially on scripts, is a common privilege-escalation hole.

ACLs grant access to additional users or groups beyond the single owner and group, without changing group membership.

# Grant an additional user read+write access to a file
setfacl -m u:miguel:rw notes.txt

# Grant an additional group read access
setfacl -m g:auditors:r notes.txt

# View the ACL entries on a file
getfacl notes.txt

# Remove a specific ACL entry
setfacl -x u:miguel notes.txt

# Remove all ACL entries
setfacl -b notes.txt
Notels -l shows a + after the permission string when a file has ACL entries.
owner
The user assigned to a file
umask
Removes permissions from the maximum to set defaults for new files and directories
SUID
Makes an executable run with its owner's privileges
SGID
Runs an executable with its group's privileges, or makes new files in a directory inherit its group
sticky bit
Restricts deletion in a directory to each file's owner
ACL
Access control list: extra permissions for specific users or groups
chmod
Changes a file's or directory's permissions
chown / chgrp
Change a file's owning user / owning group
  1. What does execute permission allow on a directory, compared with a regular file?
  2. Write a numeric chmod command for owner read/write, group read-only, and no access for others.
  3. What is the difference between chmod and chown?
  4. With a umask of 022, what permissions does a new file get?
  5. What does SGID do on a shared directory?
  6. What is the sticky bit, and where is it typically set on a stock RHEL system?
  7. How would you give one more user access to a file without changing its group? Which feature and command?