Cyber Services / All lessons / CentOS / Linux Administration / Controlling Access to Files

Controlling Access to Files

Module 7 of 33 · 6 min read · Free to read

MODULES ↓ Command blocks have a COPY button

Permissions, Attributes, and Special Modes on a CentOS Server

The foundation of all security on a Linux system is its permission model. This model dictates who can read, who can write, and who can execute every single file and directory. As a system administrator, your ability to understand and manipulate these permissions with precision is not just a skill—it is your primary mechanism for enforcing the Principle of Least Privilege. This module provides a deep dive into standard permissions, special permission bits, and advanced file attributes.

The Linux Discretionary Access Control (DAC) Model

Linux uses a model called Discretionary Access Control (DAC). This means that the "owner" of a file has the discretion to change its permissions. Every file has one owner, one group, and a set of permissions for three distinct classes of users.

The Three Classes: User, Group, Other

  • User (u): The specific user account that owns the file. The owner has the most control.
  • Group (g): A specific group that has been assigned to the file. This is how you grant access to a team.
  • Other (o): Every other user on the system who is not the owner and is not in the group. This is the "world" permission.

The Three Permissions: Read, Write, Execute

  • Read (r): Allows viewing the contents of a file or listing the contents of a directory.
  • Write (w): Allows modifying or deleting a file, or creating/deleting files within a directory.
  • Execute (x): Allows running a file as a script/program, or entering a directory with `cd`.

Deconstructing `ls -l` Output

The ls -l command is your window into this model.

-rwxr-x---. 1 Mubasher developers 4.7K Aug 4 10:15 deploy.sh

Breakdown:

  • -: File Type (- = file, d = directory, l = link)
  • rwx: User 'Mubasher' can Read, Write, and Execute.
  • r-x: Members of the Group 'developers' can Read and Execute.
  • ---: Other users have no permissions.
  • Mubasher: The User (Owner).
  • developers: The Group.

Managing Permissions and Ownership

These are the core commands for manipulating the DAC model.

  • chown (Change Owner): Changes the user and/or group ownership.
    # Change the owner of a file to 'bob'
    sudo chown bob /var/www/html/index.html
    
    # Change the owner to 'bob' AND the group to 'webdev'
    sudo chown bob:webdev /var/www/html/index.html
  • chmod (Change Mode): Modifies the read, write, and execute permissions. There are two methods to use it:

Symbolic Method (Relative)

Uses letters (u,g,o,a for all) and operators (+ to add, - to remove, = to set) to modify permissions. It's intuitive for making small, specific changes.

CentOS Example: Make a script executable only by you, the owner.

chmod u+x my_script.sh

CentOS Example: Allow members of your group to read and write to a shared project directory.

chmod g+rw /srv/project_data

Octal Method (Absolute)

Uses numbers to represent permissions (r=4, w=2, x=1). You add them up for each class (User, Group, Other) to set the exact permissions absolutely.

  • 7 = rwx (4+2+1)
  • 6 = rw- (4+2+0)
  • 5 = r-x (4+0+1)
  • 4 = r-- (4+0+0)

CentOS Example: Set the standard, secure permissions for a web directory.

# User gets rwx, group gets r-x, others get nothing
sudo chmod 750 /var/www/my-app

Special Permissions: `setuid`, `setgid`, and the Sticky Bit

Beyond the basic `rwx`, there are three special permission bits that grant temporary, elevated privileges for specific tasks.

`setuid` (Set User ID)

What it does: When an executable file has the `setuid` bit set, it runs with the permissions of the file owner, not the user who executed it. It appears as an `s` in the user's execute permission field (-rwsr-xr-x).

Why it's essential: The `passwd` command is a perfect example. A regular user needs to change their own password, which requires writing to the protected /etc/shadow file (owned by root). The `passwd` executable is owned by root and has the `setuid` bit set. When you run `passwd`, it temporarily runs *as root* to make the change, then immediately drops those privileges.

The Risk: A vulnerable `setuid` program is a classic privilege escalation vector.

`setgid` (Set Group ID)

What it does: On a file, it's similar to `setuid`, but the process runs with the permissions of the file's group. On a directory, it's more powerful: any new file created inside that directory will automatically inherit the group ownership of the directory itself, not the user's primary group.

Why it's essential: This is the key to creating collaborative, shared directories. You can create a directory owned by the `developers` group, set the `setgid` bit, and now any file any developer creates inside it will automatically be owned by the `developers` group, allowing other team members to edit it.

The Sticky Bit

What it does: The sticky bit is only useful on directories. When it is set, it allows any user to create files within that directory, but it prevents them from deleting any files they do not own. It appears as a `t` in the "other" permissions field (drwxrwxrwt).

Why it's essential: The /tmp directory is the classic example. It needs to be world-writable so any user or service can create temporary files. Without the sticky bit, any user could delete any other user's temporary files, causing chaos. The sticky bit ensures that you can only delete your own files in a shared space.

Advanced Control: File Attributes (`chattr`)

File attributes provide a deeper layer of control that operates outside the standard `rwx` permission model.

Immutable and Append-Only

The chattr (change attribute) command can set special flags on files. Two of the most important for security are:

  • Immutable (i): A file with the immutable attribute set cannot be modified, deleted, renamed, or linked to by *any* user, not even `root`.
    Use Case: Protect a critical configuration file from accidental changes or modification by a compromised process.
    sudo chattr +i /etc/resolv.conf
  • Append-Only (a): A file with this attribute can only be appended to. It cannot be overwritten or deleted.
    Use Case: Perfect for critical log files. It ensures that an attacker cannot erase or alter existing log entries to cover their tracks; they can only add new lines.
    sudo chattr +a /var/log/secure

Use lsattr to view attributes and chattr -i or -a to remove them.

Comprehensive Practice Lab: Shared Project Folder

Scenario: You need to create a shared directory for the 'developers' group where team members can collaborate on files, but you need to protect the main log file from being altered.

  1. Create the group and a user: sudo groupadd developers and sudo useradd -G developers bob. Set a password for bob.
  2. Create the directory: sudo mkdir /srv/dev-project
  3. Set ownership: sudo chown root:developers /srv/dev-project
  4. Set `setgid` and permissions: This is the key step. We want the group to have full control, but not others.
    # 2=setgid, 7=user(rwx), 7=group(rwx), 0=other(---)
    sudo chmod 2770 /srv/dev-project
  5. Create a log file inside: sudo touch /srv/dev-project/app.log
  6. Set the append-only attribute: Protect the log file.
    sudo chattr +a /srv/dev-project/app.log
  7. Test: Log in as `bob`. Try to create a file in `/srv/dev-project` (should work). Check its group ownership (should be `developers`). Try to overwrite the `app.log` file with echo "test" > /srv/dev-project/app.log (should fail). Try to append to it with echo "test" >> /srv/dev-project/app.log (should succeed).
Modules (33) ← CentOS / Linux Administration