chmod is used to change permissions of files or directories.
The chmod stands for change mode i.e. to change mode of operation of the file.
Syntax
chmod [options] permissions file_name
File ownership
- Each file is owned by single user.
- Each file also belongs to a single group.
- Same ownership principles apply to directories.
Three types of user:
- User (owner)
- Group
- Other
- Read
- Write
- Execute
Super user can override nine protection flags.
File Permissions
File permissions are rules defined for each file which determines who can access that file, and how it can be accessed.
1. Symbols (alphanumeric characters)
2. Octal numbers (0 to 7)
Changing
Permissions
Lets create abc.txt file and see some examples on chmod.
$ touch abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rw-r--r-- 1 32578 32578 0 May 13 09:41 abc.txt
Granting execute permission to user, group and others.
$ chmod +x abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rwxr-xr-x 1 32578 32578 0 May 13 09:41 abc.txt
Revoking execute permission.
$ chmod -x abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rw-r--r-- 1 32578 32578 0 May 13 09:41 abc.txt
Granting execute permission only to user/owner.
$ chmod u+x abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rwxr--r-- 1 32578 32578 0 May 13 09:41 abc.txt
Granting execute permission only to group members.
$ chmod g+x abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rwxr-xr-- 1 32578 32578 0 May 13 09:41 abc.txt
Granting execute permission only to others.
$ chmod o+x abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rwxr-xr-x 1 32578 32578 0 May 13 09:41 abc.txt
Revoking read permission from user, group and others.
$ chmod -r abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
--w------- 1 32578 32578 0 May 13 09:41 abc.txt
Revoking write permission from user, group and others.
$ chmod -w abc.txt
$ ls -lrt
total 4
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
---------- 1 32578 32578 0 May 13 09:41 abc.txt
Changing permission for user, members of group and others simultaneously.
$ chmod u=rwx,g=rwx,o=x abc.txt
$ ls -lrth
total 4.0K
-rw-r--r-- 1 32578 32578 978 May 13 09:28 add.txt
-rwxrwx--x 1 32578 32578 0 May 13 09:41 abc.txt
Changing permission for user to read, write, execute; members of group to read and execute and others to execute .
$ chmod 755 abc.txt
$ ls -lrth
total 4.0K
-rw-r--r-- 1 32578 32578 978 May 13 09:28 README.txt
-rwxr-xr-x 1 32578 32578 0 May 13 09:41 abc.txt