The third day of Linux study notes--modify permission operation

Linux modify permissions

chmod

In Linux, only root and the owner of the file can modify permissions. The command to modify permissions is chmod. There are two ways to modify this command.

1: chmod {ugo}+-={rwx} + file location

In this command, u is the owner, g is the group, and o is the other group. r stands for read permission, w stands for write permission, and x stands for execute permission. + represents adding a permission, - represents reducing a permission, = directly granting permission. for example:

chmod u + x ,gx,o=r means to add an execute permission to the " owner" , remove the execute permission to the "belonging group", so that "others" have only one read permission (regardless of his previous permissions). It should be obvious that way.

2: chmod 777 + file location

Before talking about this method, we need to talk about the numbers represented by the permissions

permission Numerical value
r 4
w 2
x 1

I don't think it's very clear, just give the order

For example: chmod 761 /tmp/test

Explanation: Give permission to the test directory, the owner permission is 7, that is to say, the owner has the permission to read, write and execute (7=4+2+1), and the group to which he belongs has the permission to read and write (6=4 +2), others have execute permission (1) which is drwxrw-r--. Because it is a directory, it starts with d.

chmod 700 /tmp/test

Explanation: Give permission to this test directory, the owner permission is 7, that is to say, the owner has the permission to read, write and execute (7=4+2+1), and the group and others have no permission. That is drwx------

Looking directly at two examples, it should be obvious.

Ok, finally let's talk about the rwx of the file and the rwx of the directory

permission document content
r Can view the content of the file Can list the contents of a directory
w file can be modified You can create, delete files in the directory, and change the name of the file
x executable file can enter the directory

Do a small test of the permissions above.

The administrator account creates a directory, test, and creates a file Test.t in the directory. Now, change the permissions of the directory test to 777 and the permissions of Test.t to 700. Q: Now create a new ordinary user, can he delete Test.t?

The permission of the file is 700, ordinary users do not have permission to modify the content of the file, and the permission of the directory where the file is located is 777, that is to say, everyone has permission to operate this directory, and ordinary users also have w permission, that is, it You can create or delete files in the directory, so it can be deleted.

chown

chown is used to modify the owner, format: chown + owner + operation object

But when you execute this command, you may find an error, because only root can change the owner .

You can see the yellow part. When switching to root, execute the chown command to change the owner.

chgrp

chgrp is used to modify the group: chgrp user group file or directory.

Each file or directory will have a default user group. The name of the default user group is the same as the owner, which is the default group (Emmmmmm, I am not very clear here). When you want to change the group of a file or directory, simply chgrp + group name + file location.

As shown in the figure, I first create a user group called hdg, and then change the group of hj0326 from mytest to hdg through chgrp hdg /tmp/hj0326, but it should be noted that I can change the group because I am currently logged in as root account.

umask

umask is a privilege management command.

One of the most commonly used functions: umask -S, look good, this is a capital S:

Execute umask -S, you will see u=rwx, g=rx, o=rx, which is easy to understand at a glance, the owner permission is rwx, the user group permission is rw, and the others are rx. In fact, the assignment of this permission is the default permission of your newly created directory or file . When we create a directory:

You will find that his permission is rwx r-x r-x , which is exactly what we see when we execute umask -S. Now that we can see the default permission assignment, of course, we can modify it as well.

Just use umask + number. But before we say that, there are a few things that need to be introduced.

If we have entered umask directly

Just like the picture above, you will see four numbers 0022. The first number, 0, represents special permissions. The specifics are... I don't know . However, these three 022s are related to our default permissions. Our default permissions are u=rwx, g=rx, o=rx, which is 755. Did you find it? One 022, one 755! ! ! ! Did you find it? I didn't find it , I knew it. In fact, the two of them add up to 777. That is, if you want your default permissions to be 755 (u=rwx,g=rx,o=rx), then you need to enter this command: umask 022. For example, if I want to change the default permissions to rwxr-xr--, that is, I need to change the permissions to 754, then I need to execute umask 023.

So when I create another directory, it will become rwxr-xr--. However, the default permissions of Linux are still very reasonable, and it is recommended that you do not modify them.

One more thing! When we create a directory, its default permissions are 755, which is rwxr-xr-x. But look at the picture below

The default permission is 022, but the permission of the file I created is rw-r--r--. If you compare it, you will find that everyone has one less permission x. This is because, in linux, the newly created The file does not have execute permission. So, when you create a new file, its permissions become rw-r--r--.

ok! Finished work

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326995354&siteId=291194637