_____ _ ___ _________ __ |_ _|__ _ __ _ __ ___ (_)_ __ __ _| \ \ / / ____\ \/ / | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | |\ \ / /| _| \ / | | __/ | | | | | | | | | | | (_| | | \ V / | |___ / \ |_|\___|_| |_| |_| |_|_|_| |_|\__,_|_| \_/ |_____/_/\_\
In Linux and other Unix-based operating systems, file permissions are one of the fundamental building blocks of security. Every file and directory has a set of permissions that determine who can read, write, and execute it. This system guarantees data protection and system integrity in multi-user environments.
To view the permissions of a file, we use the ls -l command:
$ ls -l script.sh
-rwxr-xr-- 1 ali developers 2048 Jan 20 14:30 script.sh
The first 10 characters in this output carry the permission information. Let us now examine what each of these characters means in detail.
The permission string consists of 10 characters in the format: -rwxrwxrwx
- regular file, d directory, l symbolic link, b block device, c character device.Each permission triplet contains three different permission types:
# Example permission breakdown
-rwxr-xr--
Type: - (regular file)
Owner: rwx (read + write + execute)
Group: r-x (read + execute)
Others: r-- (read only)
In Linux, every file has an owner and a group. Permissions are determined according to these three categories:
$ ls -l projects/
drwxrwxr-x 2 ali developers 4096 Jan 20 14:30 web-app
-rw-rw-r-- 1 ali developers 1024 Jan 20 14:30 README.md
# Owner: ali
# Group: developers
# web-app directory: owner and group full access, others read+execute
# README.md: owner and group read+write, others read only
Permissions can be expressed with numbers (octal) as well as letters (symbolic). Each permission type is assigned a numeric value:
r = 4w = 2x = 1These values are summed for each triplet:
rwx = 4 + 2 + 1 = 7
rw- = 4 + 2 + 0 = 6
r-x = 4 + 0 + 1 = 5
r-- = 4 + 0 + 0 = 4
--- = 0 + 0 + 0 = 0
This produces a three-digit number. For example:
-rwxr-xr-- = 754
-rw-rw-r-- = 664
-rwxr-xr-x = 755
-rw-r--r-- = 644
-rwx------ = 700
The chmod (change mode) command is used to change the permissions of files and directories. It supports two different syntaxes: symbolic and numeric.
# Owner: rwx, Group: r-x, Others: r-x
$ chmod 755 script.sh
# Owner: rw-, Group: r--, Others: r--
$ chmod 644 document.txt
# Owner: rwx, Group: ---, Others: ---
$ chmod 700 secret-script.sh
# Owner: rw-, Group: rw-, Others: ---
$ chmod 660 shared-file.txt
# Apply to directory and all contents (-R: recursive)
$ chmod -R 755 public/
In symbolic notation, you specify the target (u=owner, g=group, o=others, a=all) and the operation (+=add, -=remove, ==set exactly):
# Add execute permission for owner
$ chmod u+x script.sh
# Remove write permission from group
$ chmod g-w file.txt
# Remove all permissions for others
$ chmod o-rwx secret.txt
# Give read permission to everyone
$ chmod a+r document.txt
# Owner read+write, group read, others nothing
$ chmod u=rw,g=r,o= file.txt
# Multiple changes at once
$ chmod u+x,g+r,o-w file.txt
The chown (change owner) command changes the owner and group of files and directories. This command generally requires root privileges.
# Change owner
$ sudo chown ali file.txt
# Change owner and group
$ sudo chown ali:developers file.txt
# Change group only
$ sudo chown :developers file.txt
# or using the chgrp command
$ sudo chgrp developers file.txt
# Change ownership of directory and all contents
$ sudo chown -R ali:developers project/
# Change ownership of the symbolic link itself
$ sudo chown -h ali link.txt
Here are the permission configurations you will frequently encounter in practice:
# Web server files
$ chmod 644 index.html # Everyone can read, owner can write
$ chmod 755 cgi-bin/ # Everyone can enter directory, owner full access
# Shell scripts
$ chmod 755 deploy.sh # Everyone can execute
$ chmod 700 admin-script.sh # Only owner can execute
# Configuration files
$ chmod 600 .env # Only owner can read/write
$ chmod 644 config.yaml # Everyone can read, owner can write
# SSH keys (very important!)
$ chmod 700 ~/.ssh # Only owner can access
$ chmod 600 ~/.ssh/id_rsa # Private key: owner only
$ chmod 644 ~/.ssh/id_rsa.pub # Public key: everyone can read
# Shared project directory
$ chmod 775 /shared/project/ # Owner and group full access
In addition to the standard rwx permissions, there are three special permission bits:
When SUID is set on a file, the file runs with the owner's privileges when executed. The passwd command is an example of this: regular users can change their passwords because passwd runs with root privileges.
$ chmod u+s program
$ chmod 4755 program
$ ls -l program
-rwsr-xr-x 1 root root 1234 Jan 20 program
When SGID is set on a directory, files created in that directory inherit the directory's group. This is very useful for team collaboration.
$ chmod g+s /shared/project/
$ chmod 2775 /shared/project/
$ ls -ld /shared/project/
drwxrwsr-x 2 ali developers 4096 Jan 20 /shared/project/
When the sticky bit is set on a directory, only the file owner or root can delete files in the directory. The /tmp directory is an example of this.
$ chmod +t /shared/
$ chmod 1777 /tmp
$ ls -ld /tmp
drwxrwxrwt 15 root root 4096 Jan 20 /tmp
.env files, database configurations, and private keys must have 600 or more restrictive permissions.find / -perm -4000 to regularly check for SUID-enabled files.Understanding and correctly applying file permissions is essential for managing a secure Linux system. By applying the knowledge in this guide to your daily work, you can both protect your files and facilitate team collaboration.