Linux Commands

Linux Chmod Command Tutorial for Beginners

Most of the fresh users to Linux are searching for a way via the command prompt to modify the file/directory permissions. Those users would be pleased to know that there is a command – dubbed or chmod, e.g. change mode – that help you do this quickly. Chmod modifies each document’s rights by mode, in which the mode specifies the privileges to be updated. You may designate a mode with octal numerical or letters.

Understand the Mode Privileges

To understand the chmod, you have first to understand simple things regarding it. Open the command terminal and list all the directories or folders to check the privileges assigned to owners, groups, and others. You have to use the list command followed by the –og keyword. It will display the folder type, e.g. d belongs to a directory, r means to read, w means to write, and x means execution.

$ ls

$ ls -og

Navigate to the Desktop directory using the cd command and list the privileges assigned to any particular file using the ls command. In our case, the file is index.html. First ‘-‘ means this is not a directory. After that rw, this file has read and writes privileges assigned to the owner but no execution rights. Next rw- refers to assigned privileges to the group and last r– means privileges assigned to others for this particular file index.html.

$ cd Desktop

$ ls –lart filename

Numeric Mode

You have to simply understand that numeric mode includes numbers to assign privileges to owners, groups, and others. Number 4 is for reading, number 2 is for write, number 3 is for execution, and number 7 is for all, e.g. read, write, and execution privileges. Now navigate to the Desktop directory and create a new file named sample.txt. List all the files residing in the Desktop directory using the simple list command and display the newly created file.

$ cd Desktop

$ touch filename

$ ls

Read Privileges

To assign read privileges only to the owner, you have to add number 4 in first place in the chmod command followed by the filename. When you execute the list command for this file, you will see that only read privileges have been assigned to an owner.

$ chmod 400 filenames

$ ls –lart filename

To give reading privileges to a group, add number 4 in second place. Read privileges have been assigned to a group.

$ chmod 040 filename

To allocate read privileges to others, add number 4 in the third location. Read privileges have been assigned to a group.

$ chmod 004 filename

To grant read privileges to the owner, group, and others at the same time, add number 4 at all locations.

$ chmod 444 filename

Writing Privileges

To offer the write privileges to the owner, you will have to add number 2 in first place in the chmod command. When you execute the list command for this file, you will see that only writing privileges have been assigned to an owner.

$ chmod 200 filenames

$ ls –lart filename

To allocate writing privileges to the group, add number 2 in the second residence. Read privileges have been assigned to a group.

$ chmod 020 filename

To assign writing privileges to others, add number 2 in third place. Read privileges have been assigned to a group.

$ chmod 002 filename

To grant writing rights to the owner, group, and others at the same time, add number 2 at all places.

$ chmod 222 filename

Execution Privileges

To grant the owner’s execution rights, you will have to add number 1 in the first place.

$ chmod 100 filenames

$ ls –lart filename

To allocate execution rights to the group, add number 1 in the second residence.

$ chmod 010 filename

To assign execution rights to others, add number 1 in third place.

$ chmod 001 filename

To grant execution rights to the owner, group, and others at the same time, add number 4 at all locations.

$ chmod 111 filename

To grant all read, write, and execution rights to the owner, group, and others simultaneously, you have to add 7 on all the places.

$ chmod 777 filename

Symbolic Mode

In this mode, you will be using symbols to give rights.

Use the +x symbol to give everyone the execution rights.

$ chmod +x filename

To take back, the execution rights add the -x symbol.

$ chmod -x filename

To grant execution rights only to the current user, which is the owner, add the u+x symbol.

$ chmod u+x filename

Similarly, groups use g+x, and others use o+x.

Take back the execution rights from all, add the ugo-x symbol. You can use a-x instead of ugo-x.

$ chmod ugo-x filename

$ chmod a-x filename

If you want to copy group rights and assign them to the user, you can use the g=u symbol.

$ chmod g=u filename

You can use the rights of one file as a reference for another file. For this, simply use the reference keyword followed by the names of files.

$ chmod --reference=filename1 filename2

If you want to assign all rights to the owner, no writing privileges to the group, and don’t want to assign any rights to others, you can simply do that by a simple concept of Read=4, write=2, and execution=1. Assign 7 to the owner as 4+2+1=7. Assign 5 to the group as 4+0+1 and assign 0 to others.

$ chmod 750 filename

To assign read permissions to all, add a+r symbol.

$ chmod a+r filename

To give read and write permissions to groups and others, add the go+rw symbol.

$ chmod go+rw filename

If you want to give everybody privileges for reading, write, execute and setup Group-ID,  add the =rwx,g+s symbol.

$ chmod =rwx,g+s filename

Conclusion

We had to bring up the basic concepts of chmod instruction in this tutorial and provided examples demonstrating how it will be used in possible situations.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.