Change file(s) and Directory/Folder ownership – chmod command in Linux

The Linux command utility chmod is used to set permissions on files and directories

The command chmod, or Change Mode, is widely used to modify the access permissions of files and directories, This facilitates the users to keep the data secure and properly organized.
There are three types the file permissions can be modified using chmod command:

1) Symblolic Notation

2) Octal Notation

3) Reference operator

The Owners, Groups and Others have different permissions to access a particular file.
Lets see the Sample syntax of chmod

$ chmod [option] [mode] < file | directory>

The Users categories are symbolically represented as

-u is used for the User/Owner

g is used for the Group

o is used for the Others

The Permission modes used to modify the Read, Write and Execute permissions respectively on a file or Directory and are symbolically represented as follows

r is for Read permission

w is for Write permission

x is for Execute permission

The Octal notation is simpler interms of representation and execution with r=4 w=2 x=1 total adding up to numerical aggregate value of 7 taking the place holders of Owner(-u) Group(-g) Others(-o) respectively.

Lets see the file permissions for a sample file with complete access to every user category i.e owner, group and others, Meaning that any person who has access to the file can modify,execute(run) or delete the newfile.txt

[vamshi@node02 linuxcent]$ ls -l file.txt
-rwxrwxrwx. 1 vamshi vamshi 3 Apr 10 19:13 file.txt

From the above listed permissions we would like to release the “others” from writing this file but only limit them to read it.

The command will be to delete the Write permissions from others:

[vamshi@linuxcent ~]$ $ chmod o-w file.txt
[vamshi@linuxcent ~]$ ls -l file.txt
-rwxrwxr-x. 1 vamshi vamshi 3 Apr 10 19:13 file.txt

We use the + and - operators to provide the Read,Write and Execute permissions to user categories, It’s Demonstrated as follows:

[vamshi@node02 linuxcent]$ chmod og-rw file.txt
[vamshi@node02 linuxcent]$ ls -l file.txt
-rwx------. 1 vamshi vamshi 3 Apr 10 20:23 file.txt

Combining options such read operation for other and group, write for group and execute permission being removed for owner.

[vamshi@node02 linuxcent]$ chmod og+r,g+w,u-x file.txt
[vamshi@node02 linuxcent]$ ls -l file.txt
-rw-rw-r--. 1 vamshi vamshi 3 Apr 10 20:23 file.txt

Creating a new file newfile.txt and working with it

[vamshi@node02 linuxcent]$ echo "Welcome to LinuxCent" > newfile.txt

Here are the file’s permissions when it’s newly created. It is resultant of the umask

Lets look at a Linux practical example to setup only execute permission for all the user categories

$ chmod +x  /etc/profile.d/

Listing out the System defined Default umask in Symbolic notation.

The Default Umask permissions can be demonstrated as below:

[vamshi@node02 linuxcent]$ umask -S

Umask in Octal Notation as below:

[vamshi@node02 linuxcent]$ umask

Now the new lets see the file permissions:

[vamshi@node02 linuxcent]$ ls -l newfile.txt
-rw-rw-r--. 1 vamshi vamshi 21 Apr 10 19:49 newfile.txt

This is the inverse notation of the umask Octal value(002) set at the system wide and our file has the 664 Octal Notation.

The Octal mode of Permissions

These permissions carry a numerical value all adding up to a sum of 4 and categorized as follows:

r=4 w=2 x=1

For Example chmod 754 file1.txt

[vamshi@node02 linuxcent]$ ls -lth newfile.txt
-rwxr-xr--. 1 vamshi vamshi 3 Apr 10 19:13 newfile.txt

How to apply reference permissions in Linux using chmod ?

We shall be using the option --reference which takes the reference from the existing file and applies the permission

Lets see the demonstration below:

vamshi@linuxcent ~]$ ls -l anotherfile.txt
-rwxrwx---. 1 vamshi vamshi  33 Apr 8 19:52 anotherfile.txt
vamshi@linuxcent ~]$ chmod --reference=anotherfile.txt newfile.txt
[vamshi@linuxcent ~ ]$ ls -l newfile.txt 
-rwxrwx---. 1 vamshi vamshi 21 Apr 10 19:49 newfile.txt

With this reference operator, the nexfile.txt permissions are modified identical to anotherfile.txt


Leave a Comment