Using Chmod command to change the usage of file permission in Linux and UNIX

Time:2020-1-22

After using the ls-l command, we can see that there is a string similar to – rwxrwxrwx in front of the file, which represents the permissions of the file
Three groups of three bits R W x represent the readable (R) / writable (W) / executable permission (x) of the owner / group / others respectively
e. G., ` – rwxrw-r — ‘means that the owner has read / write / execute permission. Group members have read / write permission, while others only have read permission
Next, I will talk about how to use the Chmod command to change the permissions of files

Use numbers to change permissions
Chmod uses numbers to change the format of permission to

Copy code

The code is as follows:

chmod NUM FILE

In Linux, the R W x above is assigned respectively. R is 4, W is 2, and X is 1. Then the corresponding user’s permission is the sum of the number represented by the permission
e. G. with read / write permission, it is 4 + 2 = 6. With read / execute permission, it is 4 + 1 = 5
If you convert ` – rwxrw-r — ‘to a number,’ 764`
In the above expression format, you can see [0-7], which is to use numbers to change command authority
e. G. assign all the permissions of document a to all users. The writing method is’ Chmod 777 a ‘
In this way, all users have read / write / execute permission
Use symbols to change file permissions

Chmod uses symbols to change the format of permissions to

Copy code

The code is as follows:

chmod USER OPTION AUTHORITY FILE

User can be:
U: owners
g: Group members
O: other people
A: everyone
The option block can be:
+: add permission
-: remove permission
=: set permissions
Authority can be:
r: Read permission
w: Writable permission
x: Executable rights
The operations of different objects are separated by commas, and spaces cannot be added
e. G. for the B file, add RWX permission to the owner, R permission to the group user and others, then w permission to the group, and then remove the readable permission of the owner

Copy code

The code is as follows:

chmod u=rwx,go=r,g+w,a-r b

Parameter introduction
-v. — verbose outputs a report for each involved file
-c. — changes is the same as above, but only output when modifying
-f. — silent, – quiet blocks most error messages
-R. — recursive recursive application, which applies the permission settings to the sub contents in the directory at the same time

Chmod command instance 1: make file properties read-only in UNIX
In this example of the Chmod command we will know how to make a file read-only to its owner. In the following example, you can also give group members or other types of members read permission while the file owner has write permission.

Copy code

The code is as follows:

[email protected]~/test ls -lrt stock_trading_systems
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*
#You can see that all user types have read, write, and execute permissions on the file stock trading systems
[email protected]~/test chmod 400 stock_trading_systems
#400 means 100 000 000, that is, R —— only the owner of the file has read permission
[email protected]~/test ls -lrt stock_trading_systems
-r——– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

#Now the file only has the read attribute, and only the file owner can read it “- R——–“

Chmod command instance 2: modify only the permissions of the owner, group member or other classes
In this Chmod command example, we will know how to change file permissions at the level of owner, group and other types. You can also simply change the permissions of any type of member to the file. If you use text format, ‘U’ for file owner, ‘o’ for other types, ‘g’ for group. At the same time, ‘R’ means readable, ‘W’ means writable, ‘x’ means executable. +Indicates add permission, – indicates remove permission.

Copy code

The code is as follows:

[email protected]~/test ls -lrt chmod_examples
-r——– 1 example Domain Users 0 Jul 15 11:42 chmod_examples
[email protected]~/test chmod u+w chmod_examples
[email protected]~/test ls -lrt chmod_examples
-rw——- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

Let’s use the Chmod command to modify only the file permissions of group members

Copy code

The code is as follows:

[email protected]~/test ls -lrt chmod_examples
-rw——- 1 example Domain Users 0 Jul 15 11:42 chmod_examples
[email protected]~/test chmod g+w chmod_examples
[email protected]~/test ls -lrt chmod_examples
-rw–w—- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

In this Chmod example, we will only change the permissions of other types to the file without affecting the permissions of the owner and the group of the owner.

Copy code

The code is as follows:

[email protected]~/test ls -lrt chmod_examples
-rw–w—- 1 example Domain Users 0 Jul 15 11:42 chmod_examples
[email protected]~/test chmod o+w chmod_examples
[email protected]~/test ls -lrt chmod_examples
-rw–w–w- 1 example Domain Users 0 Jul 15 11:42 chmod_examples

Chmod command instance 3: modify the permissions of all type members (file owner + group + others)
In the last UNIX Chmod command example, we know how to change permissions one by one for file owners, groups and other types of members, but sometimes we need to modify the permissions of all types of members rather than just one of them. If you use text format, we use ‘a’ to represent all, and ‘U’ to represent the owner of the file.

Copy code

The code is as follows:

[email protected]~/test ls -lrt linux_command.txt
-rw–w–w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt
[email protected]~/test chmod a+x linux_command.txt
[email protected]~/test ls -lrt linux_command.txt
-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*

Chmod command instance 4: use the number format of Chmod command to change permissions
In UNIX, the Chmod command not only allows the use of readable text format to change permissions, but also allows the use of octal format in digital format to represent permission combinations. For example, the first number in 777 refers to the file owner, the second to the user’s group, and the third to other types of users. Now if you convert the first number to binary format, it’s written as 111, where the first number represents the read attribute, the second number represents the write attribute, and the third represents the executable permission.

Copy code

The code is as follows:

[email protected]~/test ls -lrt unix_command.txt
-rw–w–w- 1 example Domain Users 0 Jul 15 11:42 unix_command.txt
[email protected]~/test chmod 777 unix_command.txt
[email protected]~/test ls -lrt unix_command.txt
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*

Chmod command instance 5: use Chmod command to remove permissions of files
In this example of using Chmod in UNIX, we will know how to remove various permissions from a file. You can easily remove read, write, or execute permissions from a file using the number format or text format of the Chmod command. The following example shows using – X’s text format to remove executable permissions.

Copy code

The code is as follows:

[email protected]~/test ls -lrt linux_command.txt
-rwx-wx-wx 1 example Domain Users 0 Jul 15 11:42 linux_command.txt*
[email protected]~/test chmod a-x linux_command.txt
[email protected]~/test ls -lrt linux_command.txt
-rw–w–w- 1 example Domain Users 0 Jul 15 11:42 linux_command.txt

Chmod command instance 6: use Chmod command to change directory permission and recursively change subdirectory permission
This is the most commonly used example of Chmod command, which provides us with the permission to change any directory and include all contents including subdirectories and files. The Chmod command in UNIX can change the permissions of any directory recursively by using the – R command parameter. The following example will show the Chmod example.

Copy code

The code is as follows:

[email protected]~/test ls -lrt
total 8.0K
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*
drwxr-xr-x+ 1 example Domain Users 0 Jul 15 14:33 stocks/
[email protected]~/test chmod -R 777 stocks/
[email protected]~/test ls -lrt
total 8.0K
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 unix_command.txt*
drwxrwxrwx+ 1 example Domain Users 0 Jul 15 14:33 stocks/
[email protected]~/test ls -lrt stocks
total 0
-rwxrwxrwx 1 example Domain Users 0 Jul 15 14:33 online_stock_exchanges.txt*

Chmod command instance 7: how to remove read and write permissions from files for users of the type used
So far we’ve learned to add read, write, and execute permissions to files and directories. Then we’ll learn the opposite: remove read, write, and execute permissions. Using text formatting is very simple, because we just need to use – instead of +. Just as + is used to indicate adding permissions, – is used to remove permissions.

Copy code

The code is as follows:

[email protected]~/test ls -lrt stock_trading_systems
-rwxrwxrwx 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*
[email protected]~/test chmod a-wx stock_trading_systems
[email protected]~/test ls -lrt stock_trading_systems
-r–r–r– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems

Chmod command instance 8: only set the executable permissions to the directory, but do not change the permissions of its containing files
Most of the time, we just want to provide executable permissions for directories or subdirectories so that they can be searched, but we don’t want to change the permissions of files in these directories. Before I direct this command, I usually find all directories and change their permissions. But in UNIX, we can use Chmod to do it in a better way. You can use the ‘x’ (uppercase x) parameter to only change the permissions of the file directory without affecting the file. Let’s take a look at this usage in the following example.

Copy code

The code is as follows:

[email protected]~/test ls -lrt
total 8.0K
-r–r–r– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems
drw-rw-rw-+ 1 example Domain Users 0 Jul 15 14:33 stocks/
[email protected]~/test chmod a+X *
[email protected]~/test ls -lrt
total 8.0K
-r–r–r– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems
drwxrwxrwx+ 1 example Domain Users 0 Jul 15 14:33 stocks/

Remember to use uppercase X. if you use lowercase, all files and directories will be affected

Chmod command instance 9: using Chmod command to change multiple permissions of a file or directory in UNIX and Linux
You can use the combination of user + groups or groups + other in a command to change the permissions of files and directories. In the following example, the Chmod command does the same thing as providing executable permissions to owners and group members.

Copy code

The code is as follows:

[email protected]~/test ls -lrt
total 8.0K
-r–r–r– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems
drwxrwxrwx+ 1 example Domain Users 0 Jul 15 14:33 stocks/
[email protected]~/test chmod u+x,g+x stock_trading_systems
[email protected]~/test ls -lrt stock_trading_systems
-r-xr-xr– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems*

Chmod command instance 10: how to copy the permissions of one file to another in UNIX
This is a very interesting example of the Chmod command in UNIX, which can grant permissions to one file and to another. With the following example, you can easily refer to the source file permission and copy all the permissions of this file to the target file:

Copy code

The code is as follows:

[email protected]~/test ls -lrt future_trading
-rwxrwxrwx 1 example Domain Users 0 Jul 15 15:30 future_trading*
[email protected]~/test ls -lrt stock_trading_systems
-r–r–r– 1 example Domain Users 0 Jul 15 11:42 stock_trading_systems
[email protected]~/test chmod –reference=stock_trading_systems future_trading
[email protected]~/test ls -lrt future_trading
-r–r–r– 1 example Domain Users 0 Jul 15 15:30 future_trading

Recommended Today

[reading notes] calculation advertising (Part 3)

By logm This article was originally published at https://segmentfault.com/u/logm/articles and is not allowed to be reproduced~ If the mathematical formula in the article cannot be displayed correctly, please refer to: Tips for displaying the mathematical formula correctly This article isComputing advertising (Second Edition)Reading notes. This part introduces the key technology of online advertising, which is […]