Open In App

How to Set File Permissions in Linux

Last Updated : 02 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Linux is a multi-user operating system, so it has security to prevent people from accessing each other’s confidential files. When you execute a “ls” command, you are not given any information about the security of the files, because by default “ls” only lists the names of files. You can get more information by using an “option” with the “ls” command. All options start with a ‘-‘. For example, to execute “ls” with the “long listing” option, you would type ls -l . When you do so, each file will be listed on a separate line in a long format. There is an example in the window below. 

How to Check the Permission of Files in Linux

ls -l
ls -l 

ls -l 

There’s a lot of information in those lines. 

  1. The first character = ‘-‘, which means it’s a file ‘d’, which means it’s a directory.
  2. The next nine characters = (rw-r–r–) show the security
  3. The next column shows the owner of the file. (Here it is `root`)
  4. The next column shows the group owner of the file. (Here it is `root` which has special access to these files)
  5. The next column shows the size of the file in bytes.
  6. The next column shows the date and time the file was last modified.
  7. Last Column = File_name or Directory_name. (For example, here are: prac, snap, test, example)

What are the three permission groups in Linux?

First, you must think of those nine characters as three sets of three characters (see the box at the bottom). Each of the three “rwx” characters refers to a different operation you can perform on the file.  

  1. Owners: These permissions apply exclusively to the individuals who own the files or directories.
  2. Groups: Permissions can be assigned to a specific group of users, impacting only those within that particular group.
  3. All Users: These permissions apply universally to all users on the system, presenting the highest security risk. Assigning permissions to all users should be done cautiously to prevent potential security vulnerabilities.
---     ---     ---
rwx rwx rwx
user group other

What are the three kinds of file permissions in Linux?

There are three kinds of file permissions in Linux Read, write, and execute.

Letters Definition
‘r’ “read” the file’s contents. 
 ‘w’ “write”, or modify, the file’s contents. 
‘x’  “execute” the file. This permission is given only if the file is a program.

Symbols:  `+`, `-` and `=`Option in Linux File Permission

Operators Definition
`+` Add permissions
`-` Remove permissions
`=` Set the permissions to the specified values

User, group, and others Option in Linux File Permission

Reference  Class    Description
`u` user The user permissions apply only to the owner of the file or directory, they will not impact the actions of other users. 
`g` group The group permissions apply only to the group that has been assigned to the file or directory, they will not affect the actions of other users. 
`o` others The other permissions apply to all other users on the system, this is the permission group that you want to watch the most. 
`a` All three All three (owner, groups, others)

Reading the Security Permissions in Linux

For example:  “rw-  r-x  r–“

  • “rw-“: the first three characters `rw-`. This means that the owner of the file can “read” it (look at its contents) and “write” it (modify its contents). we cannot execute it because it is not a program but a text file. 
  • “r-x”: the second set of three characters “r-x”. This means that the members of the group can only read and execute the files. 
  • “r–“: The final three characters “r–” show the permissions allowed to other users who have a UserID on this Linux system. This means anyone in our Linux world can read but cannot modify or execute the files’ contents.  

How to Change Permissions in Linux

The command you use to change the security permissions on files is called “chmod“, which stands for “change mode” because the nine security characters are collectively called the security “mode” of the file. 
An example will make this clearer. 
 

For example, if you want to give “execute” permission to the world (“other”) for file “xyz.txt”, you will start by typing. 

chmod o





Now you would type a ‘+’ to say that you are “adding” permission. 
 

chmod o+





Then you would type an ‘x’ to say that you are adding “execute” permission. 
 

chmod o+x





Finally, specify which file you are changing. 
 

chmod o+x xyz.txt





You can see the change in the picture below. 
 

chmod o+x xyz.txt

chmod o+x xyz.txt

You can also change multiple permissions at once. For example, if you want to take all permissions away from everyone, you would type. 
 

chmod ugo-rwx xyz.txt





The code above revokes all the read(r), write(w), and execute(x) permission from all user(u), group(g), and others(o) for the file xyz.txt which results in this. 
 

multiple use

multiple use

Another example can be this: 
 

chmod ug+rw,o-x abc.mp4





The code above adds read(r) and write(w) permission to both user(u) and group(g) and revoke execute(x) permission from others(o) for the file abc.mp4. 

Something like this: 
 

chmod ug=rx,o+r abc.c





assigns read(r) and execute(x) permission to both user(u) and group(g) and add read permission to others for the file abc.c. 

There can be numerous combinations of file permissions you can invoke revoke and assign. You can try some on your Linux system. 

The octal notations  in Permissions in Linux

chmod o





Now you would type a ‘+’ to say that you are “adding” permission. 
 

chmod o+





Then you would type an ‘x’ to say that you are adding “execute” permission. 
 

chmod o+x





Finally, specify which file you are changing. 
 

chmod o+x xyz.txt





You can see the change in the picture below. 
 

chmod o+x xyz.txt

chmod o+x xyz.txt

You can also change multiple permissions at once. For example, if you want to take all permissions away from everyone, you would type. 

chmod ugo-rwx xyz.txt

The code above revokes all the read(r), write(w), and execute(x) permission from all user(u), group(g), and others(o) for the file xyz.txt which results in this. 
 

multiple use

multiple use

Another example can be this: 
 

chmod ug+rw,o-x abc.mp4





The code above adds read(r) and write(w) permission to both user(u) and group(g) and revoke execute(x) permission from others(o) for the file abc.mp4. 

Something like this: 
 

chmod ug=rx,o+r abc.c

assigns read(r) and execute(x) permission to both user(u) and group(g) and add read permission to others for the file abc.c. 

There can be numerous combinations of file permissions you can invoke revoke and assign. You can try some on your Linux system. 

You can also use octal notations like this. 

 octal notations

octal notations

Using the octal notations table instead of ‘r’, ‘w’, and ‘x’. Each digit octal notation can be used for either of the group ‘u’, ‘g’, or’o’. 

So, the following work is the same. 
 

chmod ugo+rwx [file_name]
chmod 777 [file_name]

Both of them provide full read write and execute permission (code=7) to all the group. 

The same is the case with this. 
 

chmod u=r,g=wx,o=rx [file_name]
chmod 435 [file_name]

Both the codes give read (code=4) user permission, write and execute (code=3) for the group and read and execute (code=5) for others. 

And even this… 

chmod 775 [file_name]
chmod ug+rwx,o=rx [file_name]

Both the commands give all permissions (code=7) to the user and group, read and execute (code=5) for others. 

How to Set File Permissions in Linux – FAQs

How do I change file permissions in Linux using the command line?

To change file permissions in Linux, you can use the `chmod` command followed by the desired permission settings.

For example: If we want to grants read, write, and execute permissions to the owner, and read and execute permissions to the group and others.

chmod 755 filename

Can I change file permissions for multiple files at once?

Yes, you can change file permissions for multiple files simultaneously using wildcards with the `chmod` command.

For instance to sets read and write permissions for the owner and read-only permissions for the group and others for all text files in the directory.

chmod 644 *.txt

How do I change the owner of a file in Linux?

To change the owner of a file, you can use the `chown` command.

For example : If we want to changes the owner to “newowner” and the group to “newsgroup.”

 chown newowner:newgroup filename 

What are the symbolic and octal representations in file permissions?

File permissions can be expressed in both symbolic (e.g., u=rw, g=r, o=r) and octal (e.g., 644) representations. Symbolic representations offer a more intuitive way to specify permissions, while octal representations provide a concise numerical format.

How can I recursively change permissions for all files and directories in a directory?

To change permissions recursively, use the `-R` option with the `chmod` command.

For example : If we want to execute permissions for the owner, read and execute permissions for the group, and no permissions for others, applying these changes to all files and subdirectories within the specified directory.

chmod -R 750 directory

Conclusion

In this article we discussed how to change file permission in linux which is vital for security. The system’s multi-user nature requires a nuanced understanding of read, write, and execute permissions for owners, groups, and others. The chmod command facilitates precise control, allowing users to modify permissions symbolically or through octal values. Essential commands like chown enable ownership changes. Whether granting or revoking access, users must exercise caution, especially when applying universal permissions. Mastering file permissions is fundamental for maintaining a secure and organized Linux system.



Similar Reads

SetUID, SetGID, and Sticky Bits in Linux File Permissions
As explained in the article Permissions in Linux, Linux uses a combination of bits to store the permissions of a file. We can change the permissions using the chmod command, which essentially changes the 'r', 'w' and 'x' characters associated with the file. Further, the ownership of files also depends on the uid (user ID) and the gid (group ID) of
4 min read
Advance File Permissions in Linux
The Linux file permissions are not limited to "rwx" bits, there are 3 special permissions apart from these "rwx" permissions which are SUID,SGID,The Sticky Bit. This article is about the 3 special file permissions and how to set and remove those permission bits. Set-user-ID (SUID) In Linux by default when a user executes a file, The file gets execu
4 min read
Finding Files With SUID and SGID Permissions in Linux
SUID(Set-user Identification) and SGID(Set-group identification) are two special permissions that can be set on executable files, and These permissions allow the file being executed to be executed with the privileges of the owner or the group. SUID: It is special file permission for executable files. This enables other users to run the file with th
3 min read
Bash Script - File Permissions
In this article, we will discuss file permission in Bash Script To understand the scenario let's take an example. Let's consider there is a system admin A for company XYZ he designs a script that is to be executed by a user at 8:00 PM daily to send a report. He designs the script but forgets to give permission to the user to execute the script. Whe
5 min read
Python | User groups with Custom permissions in Django
Let's consider a trip booking service, how they work with different plans and packages. There is a list of product which subscriber gets on subscribing to different packages, provided by the company. Generally, the idea they follow is the level-wise distribution of different products. Let's see the different packages available on tour booking servi
4 min read
Granting Permissions to Roles in Cassandra
In this article, we are going to discuss how we can granting permission to roles in Cassandra. First, we will create a new role and show how it can access the database. Creating a new role: In this step, we are going to create a new role such that user_access is a new role and want to access the database. To create a new role using the following cq
2 min read
Shell Script to List Files that have Read, Write and Execute Permissions
In this article, We will learn how to list all files in the current directory that have Red, Write and Execute permission. Suppose, we have the following files in our current directory : Here, We have a total of 8 files in our current directory. Out of 8, we have Read, Write and Execute permission on 6 files and 2 have only Read and Write permissio
3 min read
How to Fix - Cp: Cannot Create Regular File 'File': File Exists
In Linux, while performing copying operations using the cp command, the error "Cp: Cannot Create Regular File 'File': File Exists" may occur when attempting to copy a file to a destination where a file with the same name already exists. This error is a safeguard to prevent accidental overwriting of files and potential data loss. To resolve this iss
3 min read
Linux Virtualization : Linux Containers (lxc)
Operating-system-level virtualization is a server virtualization method in which an operating system's kernel allows multiple isolated user-space instances, instead of just one. Such instances, which are sometimes called containers, software containers, virtualization engines (VEs), or jails (FreeBSD jail or chroot jail), may look and feel like a r
6 min read
Difference Between Arch Linux and Kali Linux
Arch Linux is an open-source Linux-based Operating System that is freely available for use. It belongs to the Pacman-based Linux family. It is a light weighted OS for daily use and could even be used by professionals. It was first released in March 2002. It is a beginner-friendly Linux Operating System. Features of Arch Linux: Minimalist approach:
4 min read