File Permissions in Linux



Linux File Permissions



If you run the command

$ ls -l

you will get a list of files like below

-rw-r--r--  1  dev                 dev  1892  Jul 10  18:30 linux.txt

Here first part is the file permission symbol, 1 is the no of links of the file or in simple words showing there is one file linking to linux.txt. The file belongs to user dev and group dev or in simple word dev user and dev group owns the file. 1982 is the file size, after that the date and time the file has created. Last one is the file name.


Each file and directory has three user based permission groups:

Owner – It apply only the owner of the file or directory.
Group - The Group permissions apply only to the group that has been assigned to the file or directory.
All other users - The All other Users permissions apply to all other users on the system, this is the permission group that you want to watch the most.


File permissions in Linux
File permissions in Linux


File Permission Types:

Each file or directory has three basic permission types:

Read – You can read the contents of the file.
Write – You can write or modify a file or directory.
Execute – You can execute a file and for directory you can view the contents of that directory.

Now let’s see the first part of the ls –l output, it is

-rw-r--r-- 

The first char shows the file type in the example – indicates it is a file.
Following three characters are for user or owner permission (rwx).
Next three characters are for group permission (rwx).
The next three characters are for all other users permission (rwx).

Here r indicates for Read permission, w indicates Write permission and x indicates Execute permission.

If the r, w or x characters are there it means permission is on or you have that specific permission, if there is – in place of any character then shows it doesn’t have that specific permission.

For example
-rw-r--r--   says the file have read and write permission to owner, read permission to group and read permission to all others.


Modifying (Changing) the Permissions:


The permissions can be modified by using the chmod command. Permissions can be assigned explicitly or by using a binary reference.

Explicitly Defining Permissions:


For explicitly defining we use Permission Groups that are
u - Owner
g - Group
o or a – All other Users

And Permission Types that are
r - Read
w - Write
x - Execute

To add specific permission use + (plus) sign and to remove use – (minus) sign.

Example

To add the write permission to other user on linux.txt file

Chmod a+w linux.txt

To remove the same permission

Chmod a-w linux.txt

To remove both read write permission we can use single command

Chmod a+rw linux.txt


Binary References permissions:


Binary references permission can done by entering three integers or numbers.

The first number represents the Owner permission, the second represents the Group permissions and the last number represents the permissions for all other users. The numbers are a binary representation of the rwx string.

r = 4
w = 2
x = 1

You add the numbers to get the integer representing the permissions. You will need to include the binary permissions for each of the three permission groups.

Example

To set a permission –rwxrw-r—on file linux.txt use command

Chmod 764 linux.txt

Here 7 used for user for read write and execute permission, 6 for group for read and write permission and last 4 for read permission to others.



Getting help in Linux                                    suid sgid sticky bit

No comments: