Site icon Technology and Trends

Permission and Ownership on Linux

Once you start working on Linux and writing a Shell script, you will notice that all files and directories on Linux have a standard set of access permissions. These access permissions control who can access what files, and provide a fundamental level of security to the files and directories in a system. Let’s go through this topic in this blog post.

On Linux, each file and directory have three owners: User, Group, and Other.

Changing Mode on Linux

Permissions are known as modes on Linux. That’s why on Linux we use a command called chmod or “change mode”. The below changemode command can be used to change the permission of the file/shell script/directory for protection.

chmod {options} filename/Diretory/Shell Script
chmod 754 file1.sh # For File
chmod 755 test1  # directory 

Permission Modes On Linux

On, Linux, we have three permission modes that are applicable in Files and directories.

Give executable permission to Scripts

If you are writing any code using scripting languages like Linux Shell/ Perl/Python, you need to make it executable before using them. Let us take an example of the below scripts which I just created for this exercise.

[maria_dev@sandbox-hdp bash_tut]$ ls -ltr
total 8
-rw-rw-r-- 1 maria_dev maria_dev 113 Oct 18 03:50 helloworld.pl
-rw-rw-r-- 1 maria_dev maria_dev  32 Oct 18 03:55 helloWorld.sh

I will use the chmod a+x command to make it executable, where a is for adding permission and x is executable permission. Use of the plus + sign means you are adding the executable permission. Once you change the permission level, you will be able to run the code.

[maria_dev@sandbox-hdp bash_tut]$ ls -ltr
 total 8
 -rwxrwxr-x 1 maria_dev maria_dev 113 Oct 18 03:50 helloworld.pl
 -rwxrwxr-x 1 maria_dev maria_dev  32 Oct 18 03:55 helloWorld.sh

Changing file ownership

We can use the chown or change the owner command to change the file ownership of a file or a directory. One needs to be logged in as a root or a super used for this task. If we want to change the group ownership of the file, we can use chgrp the command.

chown <user_name> <file_name/directory_name>  

Let’s take the below example where I will change the permission of a directory to root user which was created originally by another user. I have a bunch of directories and files in this directory.

[root@sandbox-hdp bash_tut]$ ls -ltr
total 12
-rwxr-xr-- 1 maria_dev maria_dev  314 Oct 18 03:18 loop_through_comma_sep_values.sh
drwxrwxr-x 2 maria_dev maria_dev 4096 Oct 18 03:23 test1
drwxrwxr-x 2 maria_dev maria_dev 4096 Oct 18 03:23 test2

Now we will change the permissions of the test1 folder to the root user. As you can see below, the user has been changed to root user from maria_dev.

[root@sandbox-hdp bash_tut]# chown root test1
[root@sandbox-hdp bash_tut]# ls -ltr
total 12
-rwxr-xr-- 1 maria_dev maria_dev  314 Oct 18 03:18 loop_through_comma_sep_values.sh
drwxrwxr-x 2 root      maria_dev 4096 Oct 18 03:23 test1
drwxrwxr-x 2 maria_dev maria_dev 4096 Oct 18 03:23 test2

Commonly Used Permissions

This table gives the list of commonly used permissions on Linux for files/directories from lower to a higher level.

SymbolOctal NumberDescription
-rw-r--r--644This permission allows all the users on the system to read the file, but only the owner can edit it.
-rw-rw----660This permission allows certain groups of people to modify the file. But other users who are not in that group cannot read it.
-rw-rw-r--664This permission allows certain groups of people to modify the file. But other users on the system can read it.
rwxr-xr-x755This permission allows all users in the system to execute the file. But only the owners can edit it.
-rwx------700This permission allows for the file to be read/edited and executed by the owner. Other users in the system cannot access it.
Exit mobile version