Linux chmod command is used to change access permissions of files and directories. In this article, you will learn how to change permissions of any file or directory with chmod command. We have already described the Linux file permissions.
Syntax:
chmod [PERMISSIONS] [FILE]...
Role & Permission Types
To understand file permission you must know about Roles and Permission types. There are three types of roles available in Linux systems (User, Group, and Others). Each role has 3 types of permissions (Read, Write, and Execute).
Roles:
- User (Owner)
- Group (All group members)
- Other (All other users
Permissions:
- Read (r) – Member can read the file content or List files in a directory
- Write (w) – Member can write content to file or Create, list, rename, delete file in a directory
- Execute (x) –< Member can execute any file like sheel script or enter to the directory, and access files and directories
Changing File Permissions in Linux
The chmod command allows users two ways to set permission on any file. Read below about both options:
Using Symbolic Notation
Just for the reminder, the following symbols are used for file permissions. Here roles are User(u)
, Group(g)
, Others(o)
and the permissions are Read(r)
, Write(w)
, Execute(x)
.
Roles:
- u – User
- g – Group
- o – Other
Permissions:
- r – read permission
- w – write permission
- x – execute permission
You can combine any symbols to set permission together like User+Group (ug), User+Group+Other (ugo), User+Other(uo).
Similarly, you can do the same with permissions like Read+Write (rw), Read+Execute (rx), Read+Write+Execute (rwx).
User => Read + Execute
chmod u+rx filename
User + Group => Read + Execute
chmod u+rx,g+rx filename chmod ug+rx filename
User => All, Group => Read + Execute, Other => Read
chmod u+rwx,g+rx,o+r filename
User => All, Group + Others => Read + Execute
chmod u+rwx,go+rx filename
All permission to everyone (not recommended)
chmod ugo+rwx filename
Using Octal Notation:
Using the octal notation you can set permissions in number between 0-7. Each number is calculated with the sum of read (4), write (2) and execute (1).
For example, if you set permission 6, it means 4+2 (read + write). If you set permission 5 means 4+1 (read + execute).
The permissions are set in a sequence user, group, others. For example if you set permission 754, it means user => 7, group => 5 and other => 4.
Let’s have some examples.
- Read (r) – 4
- Write (w) – 2
- Execute (x) – 1
Possible combinations as as follows:
7 - 4+2+1 (rwx) (Read + Write + Execute) 6 - 4+2 (rw-) (Read + Write) 5 - 4+1 (r-x) (Read + Execute) 4 - 4 (r--) (Read) 3 - 2+1 (-wx) (Write + Execute) 2 - 2 (-w-) (Write) 1 - 1 (--x) (Execute) 0 - 0 (---) (None)
Example:
User => read+write+execute, Group => read+execute, Other => read
chmod 754 filename
Here:
- 7 is for user is combined with read-4 + write-2 + execute-1
- 5 is for group is combined with read-4 + execute-1
- 4 is for other is read-1 only.
User => read+write, Group => read+write, Other => read
chmod 664 filename