Introduction
Understanding how to change permissions of a file in linux is essential for anyone managing a Linux system, whether you are a beginner sysadmin or an experienced developer. File permissions control who can read, write, or execute a file, and adjusting them safely ensures security while allowing necessary access. This article walks you through the concepts, the commands, and practical examples so you can confidently modify file permissions using the chmod utility and related tools Easy to understand, harder to ignore..
Understanding Linux File Permissions
The Permission Model
Every file and directory in Linux is associated with three categories of permissions:
- Owner – the user who created the file.
- Group – a set of users that share the same group ID.
- Others – all other users on the system.
Each category can be granted three basic rights:
- Read (r) – allows viewing the file’s contents or listing directory entries.
- Write (w) – permits modifying the file or creating/removing files in a directory.
- Execute (x) – enables running the file as a program or entering a directory.
These rights are represented by the letters r, w, and x. When a permission is not granted, it is shown as a hyphen (-).
Numeric (Octal) Representation
Permissions are often expressed as an octal number, where each digit corresponds to the owner, group, and others. The values are:
- 4 = read
- 2 = write
- 1 = execute
Summing the values gives the permission level. Take this: 7 (4+2+1) means read, write, and execute are all allowed And it works..
Symbolic Representation
Alternatively, you can use symbolic notation to add or remove specific permissions:
- a – all users (owner, group, others)
- u – owner
- g – group
- o – others
Commands like +x (add execute) or -w (remove write) modify the selected categories It's one of those things that adds up..
How to Change Permissions of a File in Linux
Prerequisites
- You must have sufficient privileges to modify the file’s permissions.
- For files owned by another user, you typically need sudo rights.
Step‑by‑Step Guide
-
Check Current Permissions
Usels -lto view the existing permission string.ls -l /path/to/file.txtThe output looks like
-rw-r--r-- 1 user group 1234 Sep 25 12:00 file.txt.
Here, the owner (user) has read and write (rw-), the group has read only (r--), and others have read only (r--) That's the part that actually makes a difference. That's the whole idea.. -
Choose the Syntax
- Octal (numeric) mode – convenient for setting exact permissions.
- Symbolic mode – precise for adding or removing individual rights.
-
Apply Octal Mode
The general form is:chmodExample: To give the owner read, write, and execute; give the group read and execute; and give others read only:
chmod 754 /path/to/file.txt7→ owner: rwx (4+2+1)5→ group: r-x (4+1)4→ others: r-- (4)
-
Apply Symbolic Mode
The general form is:chmod [who][operator][permission]- who can be
u,g,o, ora. - operator can be
+(add),-(remove), or=(set exactly). - permission can be
r,w, orx.
Examples:
- Add execute permission for the owner:
chmod u+x /path/to/file.txt - Remove write permission for group:
chmod g-w /path/to/file.txt - Set exact permissions for all:
chmod a=rw /path/to/file.
- who can be
-
Use sudo When Needed
If the file belongs to another user or you lack rights, prependsudo:sudo chmod 640 /etc/example.conf -
Verify the Change
Re‑runls -lto confirm the new permissions are applied Not complicated — just consistent..
Common Scenarios
-
Make a script executable:
chmod +x script.shThis adds the execute bit (
x) for the owner Easy to understand, harder to ignore.. -
Restrict access to a configuration file:
chmod 600 /etc/secret.confOwner can read/write (
6), group and others have no permissions (0) Easy to understand, harder to ignore.. -
Give write access to a shared directory:
chmod 775 /srv/sharedOwner and group can read, write, and execute; others can read and execute Most people skip this — try not to. Worth knowing..
Scientific Explanation of Permission Changes
When you execute chmod, the kernel updates the file’s mode bits stored in the inode. These bits are read by the kernel during every access check. Changing permissions does not
Changing permissions does not affect the file’s data, its owner or group, nor does it impact any extended attributes or ACL entries. In practice, it merely modifies the bits that the kernel consults when evaluating access requests. The kernel reads these mode bits for every system call that attempts to open, read, write, or execute the file, so the effect is immediate and visible to all processes. Because the mode bits are stored in the inode, the change persists across reboots and is not lost when the file is moved between filesystems, provided the filesystem supports the same mode representation Took long enough..
Practical Tips and Gotchas
- Umask interaction – The user file‑creation mask (umask) can mask bits you intend to enable. Take this: a umask of 022 will turn a freshly created 777 into 755. To guarantee a specific outcome, either set the umask temporarily (
umask 000) or use symbolic mode that explicitly adds or removes the desired