Changing File Permissions - File Permissions On Linux

Changing File Permissions - File Permissions On Linux

The chmod command is used to change the permissions of a file or directory. Only the root user or the user who owns the file is able to change the permissions of a file.

Why is the command named chmod ? Permissions used to be referred to as modes of access, so the command chmod really means change the modes of access.

We have two techniques of changing permissions, symbolic and octal
The symbolic method is good for changing one set of permissions at a time.
The octal or numeric method requires knowledge of the octal value of each of the permissions and requires all three sets of permissions (user, group, other) to be specified every time.

The Symbolic Method

Syntax:

chmod [<set><action><permission>] File

First we indicate which set of permission is being changed.
chmod [ <set> ] File

Next you have to specify an action symbol
chmod [ <action> ] File

Different Symbols

-> +> Add permission if needed

-> = > Used when you need to specify exact permission

-> - > Used when you want to remove a permission

After an action symbol, specify one or more permissions to be acted upon.
chmod [<PERMISSIONS>]... FILE

Different Permissions

  • r > read
  • w > write
  • x > execute

Finally, a space and the pathnames for the files to assign those permissions.
Lets work with an imaginary example below:
Assuming we have a file called hello.sh which is a script file

admin@localhost:~/Documents$ ls -l hello.sh                                  
-rw-r--r-- 1 admin admin 647 Feb 09  2021 hello.sh

How ever as you can see the execute permission is not set for any of the permission groups -rw-r--r-- 1 admin admin 647 Feb 09 2021 hello.sh
Now if we attempt to execute the script it will result in an error like below:

admin@localhost:~/Documents$ ./hello.sh                                      
-bash: ./hello.sh: Permission denied

Since the system is currently logged in as the admin user, and admin is the owner of this file, giving the user owner the execute permission should allow you to execute this script. In order for us to grant the owner permission we will need to use the chmod command with u to set the owner permission. We will also use the + to indicate permission is being added and x to represent execute permission, the command should look like below:

admin@localhost:~/Documents$ chmod u+x hello.sh

After pressing the enter key there should be no output, meaning the command was a success. To truly confirm that you will have to run ls -l command, it should show the changes:

admin@localhost:~/Documents$ ls -l hello.sh                                  
-rwxr--r-- 1 admin admin 647 Feb 09  2021 hello.sh

Now as you can see the user owner now has the execute permission listed:
-rwxr--r-- 1 admin admin 647 Feb 09 2021 hello.sh
If you try to execute the file again it should grant you access.

admin@localhost:~/Documents$ ./hello.sh

./ indicates the "command" should be run from the current directory.

If You have read this far I really appreciate, Help me to grow my community:

Connect With me at Twitter | Insta | YouTube | LinkedIn | GitHub

Do share your valuable opinion, I appreciate your honest feedback!

Check out my other Blogs too:

See you in my next Blog article, Be Safe and Take care!!
Enjoy Coding