Introduction to chmod Command in Linux
In Linux/Unix like operating system, the chmod command is used to change the access mode of a file. It is used to change the permission for files and folders. These permissions are given to file/folder to provide a secure environment to the OS, efficient management of a file and high-level access to the users accessing the files/ folders.
The permissions to access the files or folders are mainly:
- Read (r): Read permissions are given to read a file only.
- Write (w): Write permissions are given to write/edit the content in a file.
- Execute (x): Execute permissions are given to run a script or execute any functionality in it.
Permissions are given to each file or folder to three sets of people. They are:
- Owner: The one who has created the files or folders.
- Group: Every file has its owner linked up to the group.
- Others: Any of the users that are not a part of either group or owner.
Below, we are talking about the chmod commands in Linux:
- d: Denotes the type of file. Either it is a directory or a file.
- rwx: Denotes the access given to the users.
- rwx: Denotes the access given to the group.
- rwx: Denotes the access given to others other than the user or group.
Syntax of chmod:
chmod [options].. [permission] file..
chmod [options].. numeric mode file..
chmod [options].. reference=file files..
Operators used in chmod Command in Linux
Operators are used to specifying which mode of access is to be given to a file/folder. These operators include:
4.5 (3,173 ratings)
|+||Adds the specified permission/mode to access|
|–||Removes the specified permission/mode to access|
|=||To replicate the specified permission to the other class of the owner/group/others|
To check the options that are available in chmod, we can do by using Linux command:
Examples of chmod Command in Linux
Following are the examples of chmod commands in Linux explained in detail.
1. chmod Command using Operator Method
Let us take an example where a file test_file.txt has full permission to the owner, group and other.
-rwxrwxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
Let us try now giving only read and write permission to the owner(Nikhil) and other users to have only read permission. We can do this by :
chmod u=rw,og=r test_file.txt
Output: -rw-r–r– 1 nikhil group users 0 Nov 29 04:50 test_file.txt
We can give execute permission to all (owner, group, others) at one shot by:
chmod a+x test_file.txt
Output: -rwxr-xr-x 1 nikhil group users 0 Nov 29 04:50 test_file.txt
We can remove read permission for the group by:
chmod g-r test_file.txt
Output: -rwx-wxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
2. Change Permission in Linux Through Numeric Code
Below are the Numeric Codes to be Known Before we Start Changing the Permissions:
- 0: no permission
- 1: execute
- 2: write
- 3: read
Permission Numbers are to be Known by Following the below Numeric Codes:
- 0 = —: no permission to any of the owner, group or others
- 1 = –x: only execute permission
- 2 = -w-: only write permission
- 3 = -wx: write and execute permission
- 4 = r–: read permission
- 5 = r-x: read and execute permission
- 6 = rw-: read and write permission
- 7 = rwx: read-write-execute permission
To give read, write, execute permissions to all owner, group and others, we can use the below syntax:
chmod 777 test_file.txt
Output: -rwxrwxrwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
To give read permission to others and removing all the permissions from the owner and group.
chmod 007 test_file.txt
Output: ——-rwx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
To give write and execute permission to all, we can use the below syntax:
chmod 333 test_file.txt
Output: –wx-wx-wx 1 nikhil group users 0 Nov 29 04:50 test_file.txt
3. chmod Permission for the Directory
We can use the chmod option for changing the permissions to the directory as well. We can use a similar syntax as we have done it earlier for files to change the permissions for the directory. A few examples are listed to explain the use of chmod.
Let us take a directory having all read-write execute permission on a directory.
drwxrwxrwx 2 nikhil group users 4096 Nov 29 05:48 test_directory
To remove all execute permission for the test_directory using operator code. We can use the below syntax:
chmod a-x test_directory/
Output: drw-rw-rw- 2 nikhil group users 4096 Nov 29 05:48 test_directory
To give read and execute permission to the owner, group, and others by numeric code. We can do by using the below syntax:
chmod 755 test_directory/
Output: drwxr-xr-x 2 nikhil group users 4096 Nov 29 05:48 test_directory
To remove write permission from others using operator function, we can use the below syntax:
chmod o-w test_directory/
Output: drwxrwxr-x 2 nikhil group users 4096 Nov 29 05:48 test_directory
chmod permission using –reference can copy the permission from one file to another file in Linux using –reference command.
chmod –reference ref_file target_file
Example: Let us take two files, file 1 having all read-write-execute permission and another file 2 having only read permission for owner, group and other.
-rwxrwxrwx 1 nikhil group users 0 Nov 29 06:02 file1.txt
-rw-r–r– 1 nikhil group users 0 Nov 29 06:02 file2.txt
Now if we have to copy the permission from file1 to file2, we can use the below syntax:
chmod --reference file1.txt file2.txt
-rwxrwxrwx 1 nikhil group users 0 Nov 29 06:02 file1.txt
-rwxrwxrwx 1 nikhil group users 0 Nov 29 06:02 file2.txt
Example: chmod command to give multiple permission to files at the same time. We can give multiple permissions to a file at the same time or in one command by using the below syntax:
chmod u=x,g-w file1.txt
Output: —xr-xrwx 1 nikhil group users 0 Nov 29 06:02 file1.txt
In this article, we have learned how to change permissions for a file or directory with the help of the chmod command with different methods like numeric, operators and reference. By granting permission, we can now secure the usability of files by other users.
This is a guide to chmod Command in Linux. Here we discuss the introduction and different operators of chmod command in Linux along with different examples and basic syntax. You may also look at the following articles to learn more-