File Permissions In Linux

What is file permission and why do you need it?

 
File permission is a way to protect files or directories from tampering against malicious attacks. It is also used to allow a particular user to read, write, or execute a specific file. File systems use permissions to regulate the level of interaction that system processes can have with files and directories.
 

How to view file permission?

 
To view file permission, go to the command line and type ls -l which will show up the listing of files along with permissions.
 
File Permissions In Linux
 
To focus on the first column,
 
drwxrwxr-x
 
d
rwx
rwx
r-x
The first one is basically the file type. Here, folder1 is a directory which is represented as d.
The permissions that the owner has over the file.
r- read
w-write
x-execute
The permissions that the group has over the file.
r- read
w-write
x-execute
The permissions that all the other users have over the file.
r- read
x-execute
The file cannot be modified by any other user except Sudeshna.
 
-rw-r--r--
 
-
rw-
r--
r--
The first one is basically the file type. Here, usr01.txt is not a directory but a file.
The permissions that the owner has over the file.
r- read
w-write
The file cannot be executed by any other user except root.
The permissions that the group has over the file.
r- read
The file cannot be modified or executed by any other user belonging to the group root.
The permissions that all the other users have over the file.
r- read
The file cannot be modified or executed by any other user. Others only have read permissions.
 

How to set file permissions via command line?

 
chmod is the command that allows changing the permissions of a file or a directory.
 

Symbolic method keywords


chmod WhoWhatWhich file|directory
  • Who is u, g, o, a (for user, group, other, all)
  • What is +, -, = (for add, remove, set exactly)
  • Which is r, w, x (for read, write, execute)

    File Permissions In Linux

Numeric method

 
chmod ### file|directory
  • Each digit represents an access level: user, group, other.
  • # is sum of r=4, w=2, and x=1.
For the user, rwx is calculated as 4+2+1=7. For the group, r-x is calculated as 4+0+1=5, and for other users, --- is represented with 0. Putting these three together, the numeric representation of those permissions is 750.
 
File Permissions In Linux

How to change file/directory user or group ownership?

 
chown (change owner) changes the owner of a file or directory. To grant ownership of the file example.txt to usr01, the following command could be used,
 
File Permissions In Linux
 
chown can be used with the -R option to recursively change the ownership of an entire directory tree.
 
The chown command can also be used to change the group ownership of a file by preceding the group name with a colon (:). For example, the following command will change the group root to Sudeshna,
 
File Permissions In Linux
 
The chown command can also be used to change both the owner and group, at the same time by using the syntax owner:group. For example, to change the ownership of example.txt to root and the group to user, use the following.
 
File Permissions In Linux
 
Note
Only root can change the ownership of a file. Group ownership can be set by root or the file's owner. We can also use chgrp command instead of chown to change the group ownership.
 
Thank you for reading! We will discuss more about file permissions, masking, and ACLs in the next article.