One-day learning Linux permission settings

Source: Internet
Author: User

We have discussed the above two sections.
Basic concepts of Linux file and directory permissions and Linux File Types and extensions. Next, let's take a look at how to modify these permissions.

Commands for modifying Permissions
Chgrp: Change the user group to which the file belongs
Chown: Change the user to which the file belongs
Chmod: Change File Permissions

Chgrp :( change group) change the user group to which the file belongs

 
To use this command, the user group to be followed must be in your/etc/group file; otherwise, an error is reported.
Important parameter:-R (recursive) recursive processing, which will be processed together with the directories in the directory. That is, all files in the directory are changed.

Let's look at an example.
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root 31537 Dec 15 install. log
Here, install. log belongs to the root user group.
[Root @ yufei ~] # Chgrp yufei install. log
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root yufei 31537 Dec 15 install. log
We have changed the user group of the install. log File to yufei.

Let's take a look at this-R parameter.
[Root @ yufei ~] # Mkdir testdir
[Root @ yufei ~] # Touch testdir/testfile
[Root @ yufei ~] # Ls-ld testdir/; ls-l testdir/testfile
Drwxr-xr-x. 2 root 4096 Jan 20 02: 01 testdir/
-Rw-r-. 1 root 0 Jan 20 02: 01 testdir/testfile
We can see that the directory testdir and testdir/testfile belong to the root user group.
[Root @ yufei ~] # Chgrp yufei testdir/
[Root @ yufei ~] # Ls-ld testdir/; ls-l testdir/testfile
Drwxr-xr-x. 2 root yufei 4096 Jan 20 testdir/
-Rw-r-. 1 root 0 Jan 20 02: 01 testdir/testfile
Only the user group to which the directory testdir/belongs is changed to yufei, but the files in the directory are not changed. Add the-R parameter to check the effect.
[Root @ yufei ~] # Chgrp-R yufei testdir/
[Root @ yufei ~] # Ls-ld testdir/; ls-l testdir/testfile
Drwxr-xr-x. 2 root yufei 4096 Jan 20 testdir/
-Rw-r-. 1 root yufei 0 Jan 20 0:01 testdir/testfile
Both have changed.

Chown :( change owner) change the user to which the file belongs
The user after this command must also be in/etc/passwd to change.
Here we also have a-R parameter, which is the same as above.
Look at the example
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root yufei 31537 Dec 15 install. log
[Root @ yufei ~] # Chown yufei install. log
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 yufei 31537 Dec 15 install. log
There is also an application that can be modified by both the user and user group.
[Root @ yufei ~] # Chown root: root install. log
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root 31537 Dec 15 install. log
As you can see, this command can also be used to change the user group.
[Root @ yufei ~] # Chown: yufei install. log
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root yufei 31537 Dec 15 install. log
Here we will not give an example of the-R parameter, which is the same as above.

Note: In the format of chown user: usergroup file, ":" is followed by a user and a user group. leave it blank if it is not changed. This method is more likely to be used.

Chmod :( change mode) change File Permissions
We can change the File Permission in two ways: rwx and numeric.

The first type is represented in numbers.

R: Read: 4
W: Write: 2
X: Execution: 1
As we have mentioned earlier, each of the three groups is a group with three groups: owner, group, and others. For your convenience, let's look at an example.
[Root @ yufei ~] # Ls-l install. log
-Rw-r-. 1 root yufei 31537 Dec 15 install. log
The first "-" indicates a common file.
Group 1: rw-= 4 + 2 + 0 = 6
Group 2: r-= 4 + 0 + 0 = 4
Group 3: r-= 4 + 0 + 0 = 4
Last point: SELinux secure file loading and downloading
Let's change this permission.
[Root @ yufei ~] # Chmod 666 install. log
[Root @ yufei ~] # Ls-l install. log
-Rw-. 1 root yufei 31537 Dec 15 install. log
This is relatively simple. The key is to perform more operations and understand more. For the meaning of these permissions, refer to the basic concepts of Linux file and directory permissions. Similarly, there is a-R parameter, and the effect is the same.

The second type is represented in rwx mode.

Before we start, let's introduce user/group/others. We can use u, g, o to represent the three, respectively, and a to represent all.
If you want to change the corresponding permissions, we can use the following method to change them.


Let's look at the example.
[Root @ yufei ~] # Ls-l install. log
-Rw-. 1 root yufei 31537 Dec 15 install. log
We changed the permission of this file to-rwxr-xr-x. We can set it like this.
[Root @ yufei ~] # Chmod u + x, g = r-x, o = r-x install. log
[Root @ yufei ~] # Ls-l install. log
-Rwxr-xr-x. 1 root yufei 31537 Dec 15 install. log
This achieves the desired effect.

Note:
1. u, g, and o are separated by commas (,).
2. If you do not have the permission, you can skip this step. For example, g = r-x, o = r-x can be written as g = rx, o = rx, and the effect is the same.

Significance of rwx on files and directories
In fact, the permission settings are relatively easy to master. The key is to have a deep understanding of what rwx means to files and directories. This is not something everyone can understand and master. Therefore, here, we will make a summary to help you learn.

Significance of rwx on files
R (read): reads the content in this file;
W (write): You can edit, add, delete, or modify the content in the file (but whether the file can be deleted depends on your permission for the Directory );
X (eXecute): The file has the permission to be executed by the system (such as some script files ).

 

Significance of rwx on directories
R (read contents in directory): read the contents in the directory.
Therefore, when you have the permission to read (r) a directory, You can query the files in this directory. So we can use the ls command to display the content list of this directory.
W (modify contents of directory): edit the contents in the directory.
It mainly includes
1. Create new files and directories
2. delete files and directories under the directory (this is important no matter what permissions the file has)
3. Rename the files and directories in the directory
4. Move the file and directory location in the directory
X (access directory): indicates whether the user can enter the directory to become a working directory.
This is whether the cd command can be used to enter a directory.

The following figure shows how to help you understand it.

.

 

 

I hope you can practice it more, especially to understand the significance of rwx on the directory.

From: yufei blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.