How to Modify the File’s and Directories Permission in Linux

File/Directory Permissions


Filer Permission is a very important concept in Linux, not only in Linux but also in all flavors of UNIX), In this topic, we are going to cover, how to how to modify the files and directory permission, How the default permissions are getting assigned for file and directory and much more…..

When you run the ls command with -l option, you would see the following details of the file.

1.Type of the file(i.e, Whether it’s a regular file or directory or link file or special file …)

2.Owner of the file

3.Group owner(i.e, The file/dir belongs to which group)

4.Size of the file

5.Date and time stamp Pieces of information (When and at what time the file created)

 

#touch   /test
#ls  -l  /test
Sample Output:  -rw-r--r--   1  root  root   0   Sep 7   19:54    /myfile.txt

 

From the above screen shot you can see the details of the file, You would see a symbol  “-“ before the permission, it indicates  what type of file it is(i.e, a regular file or directory or link file etc……) in our case, since we have created a regular file that’s why we have got the symbol is “-“ before the permission..

In UNIX everything is a file, if it is not a file then it’s  process.

Types of File Components :

So now you can easily identify the file types from the ls  -l  output,  – means regular file,  d- means directory and so on…

Note 1: Block device and special file concepts I will be posting in our upcoming topics.

Note 2: Hard-Linked files looks like a regular file the symbol is “-“, so to identify the hard linked files use the command  “ls  –li to get the inode number along with the output(i.e, the files which are having the same inode number are hard linked files).

Permission Types:

The following explains the permissions for the regular file type:

1.read “r” permission:  Grants the permission to open and read the contents

2.write “w” permission: Grants the permission to edit and delete the files

3.execute “x”:  Grants the permission to run a file as a program(ex: a script file).the users those who all have the execute access can run a file as a program

Methods to modify the files and directories permissions:

We have two methods to modify the default files and directories permissions, let us see  one by one

1)Numeric method
2)Symbolic method

Numeric:  In numeric method  the following numeric values  we have to use to assign the permissions for the files/directories

for read- r–>4
for write-w–>2
for execute-x–>1

Symbolic: In this method, we have to use some operators to set and remove the permission from the file/directory

operators: + –> Add the permission to a file/directory
                   –  —>Remove the permission from the file/directory
                   = —>Sets the designated permission

 

What is the default permission for the newly created files and directories?

Once you have created a file/directory,  check with the ls -l  command to view the details of the file, you would seerw-r–r– in the first line from the output, it says the default permission for the newly created file for the owner, group and other users.

-rw-r–r–

owner=read&write access,

group=read only access,

others=read only access

Note:     The default permission for newly created file is 644(rw-r–r–)
                 The default permission for newly created directory is 755(rwxr-xr-x)

Let us see what is that 644  here in detail…

Owner(6) =6means (4+2) read and write access
Group(4)= means only read access
Other users(4)= means only read access

The same for directory also:

Owner(7) =7means (4+2+1) read and write access
Group(5)= means only read access
Other users(5)= means only read access

 

Most of them would get little confused in assigning or understanding the permission values, let me give you a reference table for both the methods (numeric and symbolic), this would really help you in understanding the permission values.

 

How to modify the File/Directory Permissions?

As I said already in two ways we can modify the permissions either by using Numeric or by symbolic method.

By using the “chmod “(Change Modification) command we can modify the file/directory permissions,Let us see one by one in detail,

 

Method:1(Numeric)

Ex:1 Task: Assign execute permission for other users(other) for the file whatsapp.txt

Syntax to modify the file/directory permission

#chmod   <permission>  <file/directory>

Let me create a file,

#touch  /whatsapp.txt

Check the default permission for the file

#ls  -l  /whatsapp.txt

#chmod  654   /whatsapp.txt

By default, the group will have the read permission, in our task we need to add execute permission along with that, so Here I have added execute permission for the group (4+1=5), Now the group will have read and execute access.

Check whether the given permissions are updated on the file

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-xr--

You will see r-x  in group filed

 

Ex:2 How to give no permission to other users?

Method: Numeric

Task: Set no permission to other users(other) to the file /whatsapp.txt

Before modify, Check the current permission details

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-xr--

 

From the output, we have confirmed other users are having only read permission.

Now it’s time to  give “no permission” for other users by using the following command,

#chmod   650  /whatsapp.txt

“0” means it will set to no permission(check the reference table above )

Now check whether the given permission is updated on the file,

#ls  -l  /whatsapp.txt
Sample Output: -rw-r-x---

From the above  screen shot, we have confirmed the given permissions are updated for other users(check the other user field)

 

Tips and tricks: You can also modify the perm like this chmod 5 /whatsapp.txt,now here this will update the other users field(i.e, with read and execute) and if you give chmod 56 /whatsapp.txt, this will update the other and group fields, so when you are using numeric method to modify the permissions give all the values(owner, group, others) .

Note: chmod  5  /whatsapp.txt (This method will update it from the reverse field (i.e, other, group, owner) and in our case, this will set read and execute for other user’s and also this will set no permission for the remaining fields (ie, for group and owner in our case) by default.

Let me show you this with some couple of examples,

#chmod  5  /whatsapp.txt

 

#chmod  64   /whatsapp.txt

This will update the other and group filed

I hope now you have clearly understood how to modify the permissions by using different syntax.

Don’t use this syntax in your production environment unless you have full access to revert it back to default if anything went wrong.

Ex:3 How to set permissions to all the files in a single syntax?

For a single file, we can easily modify the permissions, for example, to change the permissions for all the files inside  /backup directory and assume that directory holds some 50,100 files.would it be possible for you to change the permissions for all the files immediately? yes, you can do that……

As we all know about the special wild card character “*” which select all the files alone from the directory, by using this wild card we can change the permissions for all the files,…..Here you go

Task: OWNER=full access,GROUP=add execute,OTHER’S=add write

Let me first create a directory and inside that directory going to create some 10 files.

#mkdir   /backup
#cd  /backup
#touch  f1  f2  f3  f4  f5  f6  f7  f8 f9  f10
#ls  -l
Sample Output:f1 f2 f3 f4 f5 f6 f7 f8 f9 f10

 

Now change the permissions for all the files from the directory /backup

Task: OWNER=give full access,GROUP=add execute,OTHER’S=add write

#chmod  -v 756  /backup/*

-v –>verbose option (not mandatory always, use  only if you want to view the detailed output on the screen)

Now verify all the files permissions have changed or not,

#cd  /backup
#ls  -l

Symbolic method:

Ex:4 Changing the file/directory permission by using symbolic method

Task: Set read and write access to other users

Method: Symbolic

When you are using the symbolic method to modify the permissions, keep this in your mind

owner=u
group=g
other=o

Check the current permission set for the file /whatsapp.txt

#ls  -l /whatsapp.txt
Sample Output: rw-r-----

 

Now add the permission,

#chmod  o+rw   /whatsapp.txt
#ls  -l  /whatsapp.txt
Sample Output: rw-r-xrw-

 

Ex 5: How to Set permissions for owner, group, others in a single syntax?

Task: Remove write permission for other’s, add the write permission for the group, add execute permission for the owner.

You have to use comma ,between the fileds to assign permission for all .

#chmod  u+x,g+w,o-w   /whatsapp.txt
#ls  -l  /whatsapp.txt
Sample Output: -rwxrw-r--

From the output, we have successfully removed the write permission for other’s and inserted write permission for the group &also gave the execute permission for owner

Ex:6 How to set the designated permissions?

The same as above here we have to use the operator =, procedure everything is same as above examples

Task: For Owner give  r&w and for group give read &for other’s give read permission

#chmod   u=rw,g=r,o=r   /whatsapp.txt
SampleOutput:-rw-r--r--

 

 How are the default permissions assigned to the files and directories?

When a user created a file/directory you can see the permissions as 644 for file and 755 for the directory, Wonder!! how these permissions are getting assigned, the umask value is responsible to assign the permissions.

Umask, is used to set the default permissions for the newly created files and directories, your umask value is different  for root and normal user

1.If you Logged in as root user:umask value is 022

2.If you Logged in as normal user:umask value is 002

Note: The maximum permission for a file is 666(This is because only scripts and binaries should have executed permission, normal and regular files should have just read and write permission.)

The maximum permission for the directory is 777(Directory require 777 for viewing the contents in it, so they can have 777 as max permission.

Let me show you how the system calculates  to give the default permissions

Maximum permission   –   your umask value  =  default permission

For example, if you logged in as a root user to the server(ur umask is 022)

#touch  /data.txt
#ls  -l   /data.txt
sample output:-rw-r--r--

For file, the default permission is 644, Now let me  show you  how this permission automatically assigned

666-022=644

The same for directory also

777-022=755

################################################################################

I hope you have enjoyed this article and We will see more about the permissions in our Linux topic.

If you found this article useful, Kindly Subscribe here👉🏿👉🏿Click this link to Subscribe

 

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *