Chapter 5: File Permissions

Foreword

We recommend that you do not use AI to do the exercises since you are in the learning phase.

Introduction

In this section, we will cover file management under Linux. More specifically, we’ll talk about permissions and access management on files.

Prerequisites

Same old story. 😉




File Management

File permissions

General information

Under Linux, file permissions are an essential barrier against unauthorized access.

These permissions can be viewed using the ls -l command, which displays them in the following symbolic form:

-rw-rw-r-- 1 widal widal 244 avril 23 01:48 pubspec.yaml

Let’s break down each part of this line:

  1. -rw-rw-r--: This is the file’s permissions.
    • The first character (-) indicates that this is a regular file (rather than a directory or a symbolic link, for example). Below are all the file types with their character.

         `-`: regular file
         `d`: Directory
         `c`: Character device
         `b`: Block device
         `l`: Symbolic link
         `p`: Named pipe (IPC)
         `s`: Local socket (IPC)
      
    • The first three characters (rw-) indicate the permissions for the file’s owner (here, “widal”). The owner can read and write to the file, but cannot execute it.
    • The next three characters (rw-) indicate the permissions for the file’s group. The “widal” group can also read and write to the file, but cannot execute it.
    • The last three characters (r--) indicate the permissions for other users (those who are neither the owner nor members of the group). These users can only read the file, but cannot write to it or execute it.
  2. 1: This indicates the number of hard links (that is, the number of file names pointing to this file) on the system. Generally, for a simple file, this is 1 (one).

  3. widal: The name of the file’s owner, here “widal”.

  4. widal: The name of the group associated with the file. Here too, the group is “widal”.

  5. 244: The size of the file in bytes. This file weighs 244 bytes.

  6. avril 23 01:48: The date and time of the file’s last modification. Here, the file was modified on April 23 at 01:48.

  7. pubspec.yaml: This is the file’s name.

Summary:

The pubspec.yaml file belongs to the user “widal” and to the group “widal”; it is readable and writable by both of these entities, but only readable by others. It weighs 244 bytes and was last modified on April 23 at 01:48.

Changing file permissions

Under Linux, file and directory permissions define who can read, write, or execute a file. You can change these permissions with the chmod command. Changing permissions can be done in two main ways: using numbers or symbols.

1. Using Numbers with chmod

Each type of permission (read, write, execute) is represented by a digit:

Permissions are assigned via a three-digit number representing, respectively, the permissions for the owner, the group, and other users.

Examples:

2. Using Symbols with chmod

Permissions can also be specified using symbols:

Examples:

3. Using umask (ℹ️ Good to know!)

The umask command determines the default permissions of files and directories when you create them. The umask is a kind of “mask” that removes certain permissions from created files. By default, when a file is created, it has broad permissions (like 777 for a directory or 666 for a file), and the umask will adjust these permissions according to the specified values.

The umask command can be used to display or change the default permissions mask.

Example:

4. Bonus: Managing permissions when copying a file ( cp -p )

The cp command is used to copy files or directories. The -p option of cp lets you preserve the source file’s attributes during the copy, such as:

For example:

cp -p source_file.txt copy_file.txt

This creates a copy of the file while keeping the same permissions and other metadata as the original file.

Table of permissions and chmod values

Permission Numeric value Description Associated symbols
Read (r) 4 Read the file r
Write (w) 2 Modify the file w
Execute (x) 1 Execute the file x
None 0 No permission  



Command Owner (u) Group (g) Others (o) Typical use case
chmod 777 file.txt rwx rwx rwx All rights for everyone (generally to be avoided)
chmod 750 file.txt rwx r-x Full access for the owner, read-only for the group, no access for others
chmod 640 file.txt rw- r– File readable/writable by the owner, readable by the group only
chmod 755 file.txt rwx r-x r-x Classic case for a shared script or executable
chmod 644 file.txt rw- r– r– Classic case for a non-executable text/config file
chmod 000 file.txt No access for anyone (except root)

Practical example

Let’s imagine you have a file script.sh with the following permissions:

-rw-r--r-- 1 user group 1000 avril 23 01:48 script.sh

If you want to give execute permissions to all users, you can do:

chmod +x script.sh

This will change the permissions to give execute rights:

-rwxr-xr-x 1 user group 1000 avril 23 01:48 script.sh

You can also use cp -p to copy a file while preserving its permissions and owner:

cp -p script.sh /path/to/new_script.sh

This will keep the same metadata and permissions in the copy.


Managing a file’s owner and group

Below is a table showing how a file’s owner and group are managed.

Command Description
chown alice file.txt Changes the file’s owner to alice.
chown alice:dev file.txt Changes the owner to alice and the group to dev.
chown -R alice:dev /path/to/folder Recursively changes the owner and group in a directory.
chgrp dev file.txt Changes the file’s group to dev.
chgrp -R dev /path/to/folder Recursively changes the group in a directory.
ls -l file.txt Displays the file’s permissions, owner, and group.


The Sticky Bit ( Good to know!)

The sticky bit is a special permission that can be set on a directory. When enabled, this bit changes the behavior of files placed in that directory. It essentially lets you restrict file deletion to the file’s owner. In other words, only the owner of a file or the administrator (root) can delete or rename that file in a directory marked with the sticky bit, even if other users have write permissions on the directory.

Usefulness of the Sticky Bit

The sticky bit is often used on shared directories, such as /tmp, where many users can create and modify files, but where it’s essential that users cannot delete or modify other users’ files.

Typical use case:

How do you set and check the Sticky Bit?

1. Setting the Sticky Bit

To set the sticky bit, you use the chmod command with +t. This is generally done on directories. For example:

chmod +t /tmp

This command adds the sticky bit to the /tmp directory. If you want to check that the sticky bit is enabled, you can use the ls -ld command to display the directory’s permissions:

ls -ld /tmp

The output might look like this:

drwxrwxrwt 10 root root 4096 avril 23 10:00 /tmp

Here, the t in the rwxrwxrwt permissions (instead of x at the end) indicates that the sticky bit is enabled.

2. Removing the Sticky Bit

If you want to remove the sticky bit from a directory, you can use the following command:

chmod -t /tmp

This removes the sticky bit, and users with write permissions on that directory can then delete or modify other users’ files.

Summary - Sticky Bit Behavior


SETUID and SETGID

SETUID (Set User ID) and SETGID (Set Group ID) are special bits that can be set on executable files under Linux. When enabled, these bits change how the system handles the execution of files. Their main purpose is to grant temporary permissions for running a program, allowing a user to execute a file with the permissions of another user (usually a privileged user, such as root).

SETUID (Set User ID)

Example:

chmod u+s /path/to/file

This adds the SETUID bit to an executable file.

Important point: Modern Linux systems do not allow SETUID on shell scripts (like .sh files) for security reasons. The script will therefore run with the privileges of the current user, not those of the owner (root), even if the SETUID bit is active.


To test (without being root) 👨🏾‍💻👩🏾‍💻:

SETGID (Set Group ID)

Example:

chmod g+s /path/to/file

This adds the SETGID bit to an executable file or a directory.

In summary:

These two bits are mainly used for managing temporary permissions when running programs, and must be used with caution to avoid security issues.

Important point: Modern Linux systems do not allow SETUID and SETGID on shell scripts (like .sh files) for security reasons. The script will therefore run with the privileges of the current user, not those of the owner (root), even if the SETUID bit is active. See the screenshot below.




Training ⚔️

Exercise 1

Exercise 2 (🧪)

Run the script to start the challenge like a boss 😉.

Exercise 3 (Deep Dive)



Feedback

Let us know what you think of this chapter.

👉🏾 Click here