Linux self-study journey-basic commands (basic permission command chmod)

The use of basic permissions commands in Linux


Preface

1. In the previous section, we have finished talking about the role of the basic permission bits of the directory. In fact, it is divided into three parts like the permission bits of ordinary files, but the meaning of rwx may be different. If you haven't read it, please click the link below to enter: Linux directory permission bits

2. Now that we have finished talking about the role of the three-part permission bits of ordinary files and directory files, then at the beginning of this section we will talk about some commands for modifying permissions. In this section, we will only talk about a command to modify file or directory permissions. chmod


Tip: The following is the content of this article

1. chmod command (the usage of +-=)

In our Centos 6.6, if we want to modify some permissions on files or directories (for example, restrict others to only have r permissions on files), we have the corresponding chmod command to perform this operation .

  • Command name: chmod
  • The full name of the command: change file mode bits
  • Location: /usr/bin/chmod
  • Execution authority: all users
  • Function description: change file or directory permission mode
命令格式
chmod [选项] 权限模式 文件名
常用选项:
-R :递归设置权限,也就是给目录下所有文件进行权限设置

1. We know that a file permission bit is divided into three parts, namely the owner (u), the group (g), and other people (o) , then we will give a separate modification of the permissions of other people (o) example:

[root@student ceshi]# ll
总用量 4
drwxr-xr-x. 2 root root    6 1月  23 16:46 jh
-rw-r--r--. 1 root root 1911 1月  22 08:27 passwd
[root@student ceshi]# 

(我们接下来要修改passwd这个文件其他人的权限,让其他人不能拥有r权限)
[root@student ceshi]# chmod o-r passwd 
[root@student ceshi]# ll
总用量 4
drwxr-xr-x. 2 root root    6 1月  23 16:46 jh
-rw-r-----. 1 root root 1911 1月  22 08:27 passwd
[root@student ceshi]# 

(我们可以看到,我用chmod o-r的方式让passwd这个文件的其他人的r权限取消了;
因为我们其他人用字母表示是o嘛,所以直接o[+ -]权限的方式就可以对文件进行
权限的修改;那么现在我想让所属组拥有rwx三个权限,请看下方;;)
[root@student ceshi]# chmod g+wx passwd 
[root@student ceshi]# ll
总用量 4
drwxr-xr-x. 2 root root    6 1月  23 16:46 jh
-rw-rwx---. 1 root root 1911 1月  22 08:27 passwd
[root@student ceshi]# 

(不想让其他人拥有r权限我们可以o-r,如果我们想让其他人拥有r权限我们可以o+r
就可以了;
所以如上我们想让所属组(g)拥有rwx权限,因为这个文件所属组本身拥有r权限,
所以我们直接g+wx就可以让这个文件三个文件都有了)
[root@student ceshi]# chmod g=x passwd 
[root@student ceshi]# ll
总用量 4
drwxr-xr-x. 2 root root    6 1月  23 16:46 jh
-rw---x---. 1 root root 1911 1月  22 08:27 passwd
[root@student ceshi]# 

(当然,我们直接等于的话,是可以直接对文件进行赋予对应=的权限;

意思就是比如你这个文件所属组的权限是rwx,如果我直接chmod g=x的话,你的文件
的所属组的权限会直接变成x)

1.1: So let's summarize:

chmod u+rwx jj=jj the permission bits of the owner of the file plus the three permissions of rwx ;

chmod g-rx jj=jj this file belongs to the group permission bit minus the two permissions rx ;

chmod o=x jj=jj The permission bit of others in this file is directly equal to the permission x ;

In summary, if you want to add permissions to a file, just chmod permission bit + permission mode file name ; subtract some permissions
from the file to chmod permission bit-permission mode file name ;

(Recommended) If you want the file to be directly equal to a certain permission, just chmod permission bit = permission mode file name ;


Two, chmod command (the usage of numbers)

There is another usage of our chmod, which is to directly chmod the permission digital file name ;

命令格式
chmod 权限数字 文件名
权限数字:
r=4
w=2
x=1

(这种用法很简单,就是把rwx三个权限模式看成421三个对应数字)

for example:

[root@student ceshi]# chmod 644 passwd 
[root@student ceshi]# ll
总用量 4
drwxr-xr-x. 2 root root    6 1月  23 16:46 jh
-rw-r--r--. 1 root root 1911 1月  22 08:27 passwd
[root@student ceshi]# 

(这个数字赋予权限的方法取代了原本的那些u+rx鸭g-w鸭啥的赋予权限的方式;
你用这个数字的方式可以直接对ugo三个部分的权限进行设置;

我上面的644的意思就是:

所有者(6):我说过r权限看成数字4,然后w看成2,
所以这第一个6就可以看成是将所有者权限位的r的4加上w的2的权限,所以就是4+2=6,
所以就是所有者赋予rw两个权限;)

所属组(4):这个很好理解了吧,r=4,所以第二个位置就是所属组赋予r一个权限;

其他人(4):r=4,所以第三个位置就是其他人赋予r权限

In summary:
{ chmod digital file name; the order in which the numbers should be entered: first: owner, second: belonging to group, third: others




Numbers can be entered:
r=4
w=2
x=1

For example:
chmod 755 jj: The owner of the jj file has rwx permissions, and the group and others have rx permissions (because the owner permissions 4+2+1=7, then the group and others are 4+1, so That is, r+x is 5);

chmod 621 jj: The owner of the jj file has rw permissions, the group to which it belongs has w permissions, and others have x permissions (this is easy to understand, the first owner 4+2=6, all is r+w; the second The group belongs to 2, so it is w; the third other person is 1, so it is x)
}


to sum up

In this section we have learned
{

1. chmod can modify the permission mode of the permission bit of a file or directory

2. chmod can add the two permissions of rw to the owner permission bit through u+rw

3. chmod can subtract the permission of w from the permission bit of the group by gw

4. chmod can set the permissions of other people to only x by means of o=x

5. chmod can set the permissions of the owner, group, and others together to only have the permission of w through 222.
}

In the next section, we briefly review some knowledge of basic permission bits, then this section ends here.

This is Jiehua, see you next time.

Guess you like

Origin blog.csdn.net/qq313088385/article/details/113100717