Understanding File and Directory Permissions

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
Search for a command to run...

Postgraduate in Communications Engineering with working experience in the Support Desk and self-study in software development.
A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...
The git log command is used to view the commit history of a Git repository. You can customize the output format to show specific details in a more readable or structured way. Below are some useful variations and formatting options for git log: To sh...

Overview This article will teach us how to: Install Ubuntu Server 24.04 on a laptop from scratch Install Ubuntu server by using Vagrant tool with ISO image Prerequisites: A laptop without any OS installed; A virtualization software Virtualbox t...

The /etc/passwd and /etc/shadow files are the backbone of Linux user management. Together, they store user account information and handle authentication securely. This article provides a hands-on guide to understanding these files, their structure, a...

In Linux, links are powerful tools that allow you to create references to files and directories. There are two main types of links: hard links and soft links (also known as symbolic links or symlinks). Understanding the differences between these two ...

A hostname is a human-friendly name given to a computer. It is a unique identifier that allows us to identify the machine in various network communications, making it easier to locate and manage devices. Type: hostname To see where it is stored, typ...

File permissions are core to the security model used by Linux systems. The file permission in Linux specifies how much power each user has over a given file or directory.
There are two ways of viewing file permissions in Linux:
Using a graphical user interface (GUI) by right-clicking the file/directory -> Properties -> Permissions

Using a command-line interface (CLI) by typing ls -l command.

The output of this command is explained further.
Each file and directory in Linux can be accessed or modified by three different classes of users.
User (u): The user who owns the file.
Group : The group that the file belongs to.
Others: All other users.

Each file or directory has three basic permission types:
Read (r) - allows to read the contents of the file.
Write (w) - allows to write or modify a file or directory.
Execute (x) - allows to execute a file or view the contents of a directory.

The permissions are represented as a string of 10 characters, like -rw-rw-r--.
These characters can be broken down as follows:
The first character indicates the type of file
- for regular files,
d for directories,
l for symbolic links,
The next three characters indicate the permissions for the user/owner of the file/directory (rw- read, write).
The following three characters indicate the permissions for the group the file/directory belongs to (rw- read, write).
The last three characters indicate the permissions for others (r-- read).
| Permission string | directory | file |
| read (r) | The user can read the contents of the directory. In other words, he can view the listing of the directory. | The user can read the contents of the file. |
| write (w) | The user can add/remove files to/from the directory. | The user can write content to the file. |
| execute (x) | The user can go inside the directory. The user cannot read the directory's contents if it is not set. | The user can execute the file as if it were a program/script. |
So, the file above weather-report.service is owned by the user vagrant and belongs to the group vagrant . Its owner vagrant and all group members vagrant can read and write to the file while all others can only read the file.
Changing permissions in Linux can be done using the chmod command in 2 ways:
Symbolic mode;
Numeric (Octal) Mode:
Symbolic mode allows to change file and directory permissions using symbolic representations of the
User classes
User (u),
Group (g),
Others (o)
All (user, group, and others) (a)
Permissions types
Read (r),
Write (w),
Execute (x).
Operators
+: Adds the specified permissions to the existing ones without altering other permissions.
-: Removes the specified permissions.
=: Sets the specified permissions and removes any permissions not listed.
To add read, write, and execute permissions for the user:
chmod u+rwx file
To set read and write permissions for the group:
chmod g=rw file
To remove execute permission for others:
chmod o-x file
When chmod command is used with numerical values three digits are specified. The first digit is for the user, the second is for the group and the third is for others. Each digit represents a different set of permissions. The digits are calculated by adding up the values of:
r (read) = 4,
w (write) = 2,
x (execute) = 1,
no permission = 0
The numbers are summed up and depicted by one number. Therefore, the possibilities are:
7 - for read, write, and execute permission.
6 - for read and write privileges.
5 - for read and execute privileges.
4 - for read privileges.
To give the owner read and write permissions, the group read permissions, and no rights for all others, type.
chmod 640 filename
To change the permission for every file all in one shot:
chmod 640 Download/*
Aside from changing file and directory permissions, we can also change user ownership and group ownership of the file and directory. Both of these tasks require superuser privileges.
To change the owner of a file or directory:
sudo chown username /Downloads/
To change both the owner and the group:
sudo chown username:groupname filename
To change only the group:
sudo chown :groupname filename
In addition to the standard read, write, and execute permissions, Linux also supports special permissions that provide additional security and functionality features for files and directories.
Setuid (Set User ID) - When set on an executable file, the file runs with the permissions of the file's owner, not the person running it.

In order setuid to work, the file should be executable and owned by root in order to grant temporary elevated privileges to the person running it.
Setgid (Set Group ID) - Similar to setuid, the setgid permission allows a user to execute a file with the permissions of the file's group, rather than the permissions of the user executing the file. setgid is often used for directories to ensure that files created within the directory inherit the group ownership of the directory.
Sticky Bit: When the sticky bit is set on a directory, it restricts the deletion or renaming of files within that directory to only the file owner, the directory owner, or the root user, even if other users have write permissions on the directory. This is commonly used on directories like /tmp, where many users have write access, to prevent users from deleting or renaming each other's files. The sticky bit is more useful in shared directories where multiple users have write access.
