Article Title: tip: Use the chmod command in Linux to change permissions. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
You can use the chmod command to change the permission. The following example shows how to use the chmodcommand to change the permissions of the readme.txt file.
The following is the initial permission settings for the readme.txt file:
-Rw-r -- 1 winda 39 August 11 12:04 readme.txt
If the file owner or logon is the root user identity, you can change the permissions of the owner, group, and others. When reading the logs, the owner and group of the readme.txt file can read and write files (rw-). Anyone outside the group can only read files (r --).
File Permission is a security measure. At any time, allowing others to read, write, or execute files poses a risk of file tampering or deletion. As a basic principle, read and write permissions should be granted to only those who really need these files.
In the following example, I want to grant each individual the permission to write the readme.txt file. Therefore, they can read the file, add it to it, and save the file. This means that the "Others" section of the file permission must be changed.
Enter the following command at the shell or terminal prompt:
Chmod o + w readme.txt
The o+wcommand system wants to grant other users the permission to write the file readme.txt. To view the results, list the details of the file again. In this case, the user access permission for this file is as follows (w is added in column 3rd ):
-Rw-1 winda 39 March 11 12:04 readme.txt
Now, everyone can read and write this file.
To delete the read and write permissions of the group Group and others from readme.txt, run the chmod command to cancel the read and write permissions.
The command is as follows:
Chmod go-rw readme.txt
The read and write permissions of group groups and other persons in readme.txt can be deleted by inputting the go-rw system. The results listed by the ls-1 command again are as follows:
-Rw -- 1 winda 39 March 11 12:04 readme.txt
To sum up, when you want to use the chmod command to change the permission, the file user identity mainly includes the following types:
U: Users (owners) with files );
G: group where the owner is located;
O: other people (group not the owner or owner );
A: Everyone or all (u, g, and o ).
The object access permission type is as follows:
R: read permission;
W: write permission;
X: execution right.
File Permission configuration behaviors include:
+: Add permissions;
-: Delete permissions;
=: Make it the only permission.
The command to delete all permissions (including the permissions of each person) from readme.txt is as follows:
Chmod a-rwx readme.txt
Now, let's see if the cat readme.txt command can be used to read the file. The output returned by the command should be as follows:
Cat: readme.txt: Permission denied
Deleting all permissions, including your own permissions, will successfully lock the file. However, because the file belongs to the user, you can use the following command to change its permissions at any time. The command is as follows:
Chmod u + rw readme.txt
Run cat readme.txt to check whether the file can be read as the file owner.
Here are some common examples that can be used in chmod command settings:
G + w: Add write permission to group;
O-rwx: delete all permissions of others;
U + x: allows the file owner to execute the file;
A + rw: allows everyone to read and write files;
Ug + r: allows the owner and group to read files;
G = rx: only group reading and execution are allowed (no write is allowed ).
Finally, we will introduce how to restore group permissions. The command is as follows:
Chmod ug + x tigger
Now, if you run the ls-dl command, you will find that only others (others) are denied access to the tigger directory.