【Linux详解】——权限

📖 前言:前面我们已经知道。Linux下有两种用户:超级用户(root)、普通用户,以及相互之间的切换。本期将介绍Linux的权限相关知识


🕒 1. shell命令以及运行原理

Linux严格意义上说的是一个操作系统,我们称之为“核心(kernel) “ ,但我们一般用户,不能直接使用kernel。而是通过kernel的“外壳”程序,也就是所谓的shell,来与kernel沟通。如何理解?为什么不能直接使用kernel。

从技术角度, Shell的最简单定义:命令行解释器(command Interpreter)。
表现:你看到的命令行提示符,以及可以输入指令并且可以执行。
主要包括:

  • 将使用者的命令翻译给核心(kernel)处理。
  • 同时,将核心的处理结果翻译给使用者

对比windows 图形界面(GUI),我们操作windows 不是直接操作windows内核,而是通过图形接口,点击,从而完成我们的操作(比如进入D盘的操作,我们通常是双击D盘盘符或者运行起来一个应用程序 )

shell 对于Linux,有相同的作用,主要是对我们的指令进行解析,解析指令给Linux内核。反馈结果在通过内核运行出结果,通过shell解析给用户。

Linux shell命令行外壳 和 win 图形界面,是兄弟关系

感性的理解:举个相亲的例子,你是一个闷骚且害羞的程序员,那shell就像媒婆,操作系统内核就是你的心上人。你看上了她,但是又不好意思直接表白,那就让你家人找媒婆帮你提亲,所有的事情你都直接跟媒婆沟通,由媒婆转达你的意思给她,而我们找到媒婆姓王,所以我们叫它王婆,它对应我们常使用的bash。
换言之,媒婆是一种行业,而王婆是具体一个人,所以bash是shell的一种外壳程序,就像类和对象。

在这里插入图片描述
执行命令,通过派生子进程的方式执行用户的指令。shell本身并不执行对应的指令(一般而言)

安卓的本质是Linux内核
早期所谓的深度定制安卓系统,其实就是把相关库的图形、颜色等改了改。内核都一样
在这里插入图片描述

🕒 2. Linux权限的概念

Linux下有两种用户:超级用户(root)、普通用户

  • 超级用户:可以再linux系统下做任何事情,不受限制
  • 普通用户:在linux下做有限的事情。

超级用户的命令提示符是#,普通用户的命令提示符是$

权限是约束普通人的

命令: su [用户名]
功能:切换用户。
例如,要从root用户切换到普通用户user,则使用 su user(不需要密码)。 要从普通用户user切换到root用户则使用 su root(root可以省略),此时系统会提示输入root用户的密码。以root重新登陆:su -
注意:输入密码的时候shell会不显示,输完直接回车即可。

sudo:不切换用户,就让普通用户以root的身份,执行对应的指令。
pwd

[root@VM-12-13-centos testLinux]# su hins
[hins@VM-12-13-centos testLinux]$ whoami
hins
[hins@VM-12-13-centos testLinux]$ which whoami
/usr/bin/whoami
[hins@VM-12-13-centos testLinux]$ sudo whoami

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for hins: 
[hins@VM-12-13-centos testLinux]$ sudo whoami
root				# 已授权情况

hins is not in the sudoers file.  This incident will be reported.			# 需要将当前的普通用户,添加到/etc/sudoers中,后面会统一解答

🕒 3. Linux权限管理

🕘 3.1 文件访问者的分类

Linux下有三种角色,拥有者user(owner),所属组(grouper),和其他用户(other)

[hins@VM-12-13-centos testLinux]$ ll
total 8
drwxrwxr-x 3 root root 4096 Oct  9 09:38 First	# 目录
-rw-r--r-- 1 root root    0 Oct 12 23:47 test2.txt
-rw-r--r-- 1 root root   80 Oct 22 09:45 test.c

🕘 3.2 文件类型和访问权限(事物属性)

每一列的理解
在这里插入图片描述
文件类型

  • d :文件夹
  • - :普通文件
  • l :软链接(类似 Windows 的快捷方式)
  • b :块设备文件(例如硬盘、光驱等)
  • p :管道文件
  • c :字符设备文件(例如屏幕等串口设备)
  • s:套接口文件

注意:Linux类型与文件后缀无关,后缀本质是文件名的一部分,但我们自己可以用后缀进行主观区别。
在这里插入图片描述
gcc等工具,对文件后缀可能有要求

文件=内容+属性

基本权限

  1. 读( r/4 ): Read 对文件而言,具有读取文件内容的权限;对目录来说,具有浏览该目录信息的权限
  2. 写( w/2 ): Write 对文件而言,具有修改文件内容的权限;对目录来说具有删除移动目录内文件的权限
  3. 执行( x/1 ): execute 对文件而言,具有执行文件的权限;对目录来说,具有进入目录的权限
  4. “—” 表示不具有该项权限

🕘 3.3 文件权限值的表示方法

🕤 3.3.1 字符表示方法

 Linux表示   说明   Linux表示   说明  r- -  只读  -w-   仅可写  - -x  仅可执行   rw-   可读可写  -wx  可写和可执行   r-x   可读可执行   rwx   可读可写可执行  - - -  无权限  \begin{array}{|l|l|l|l|} \hline \text { Linux表示 } & \text { 说明 } & \text { Linux表示 } & \text { 说明 } \\ \hline \text {r- -} & \text { 只读 } & \text {-w- } & \text { 仅可写 } \\ \hline\text {- -x} & \text { 仅可执行 } & \text { rw- } & \text { 可读可写 } \\ \hline\text {-wx} & \text { 可写和可执行 } & \text { r-x } & \text { 可读可执行 } \\ \hline \text { rwx } & \text { 可读可写可执行 } & \text {- - -} & \text { 无权限 } \\ \hline \end{array}  Linux表示 r- -- -x-wx rwx  说明  只读  仅可执行  可写和可执行  可读可写可执行  Linux表示 -w-  rw-  r-x - - - 说明  仅可写  可读可写  可读可执行  无权限 

🕤 3.3.2 八进制数值表示方法

 权限符号(  读 写 执行 )   八进制   二进制  r 4 100 w 2 010 x 1 001 r w 6 110 r x 5 101 w x 3 011 r w x 7 111 - - - 0 000 \begin{array}{|l|l|l|} \hline \text { 权限符号( \textcolor{green}{读}\textcolor{orange}{写}\textcolor{red}{执行}) } & \text { 八进制 } & \text { 二进制 } \\ \hline \mathrm{r} & 4 & 100 \\ \hline \mathrm{w} & 2 & 010 \\ \hline \mathrm{x} & 1 & 001 \\ \hline \mathrm{rw} & 6 & 110 \\ \hline \mathrm{rx} & 5 & 101 \\ \hline \mathrm{wx} & 3 & 011 \\ \hline \mathrm{rwx} & 7 & 111 \\ \hline \text {- - -} & 0 & 000 \\ \hline \end{array}  权限符号( 执行 rwxrwrxwxrwx- - - 八进制 42165370 二进制 100010001110101011111000

🕘 3.4 文件访问权限的相关设置方法

🕤 3.4.1 chmod(设置文件属性)

功能:设置文件的访问权限
格式chmod [参数] a(all)/u(user)/g(group)/o(other) +/-/= [r/w/x(可叠加)] 文件名
常用选项

  • R -> 递归修改目录文件的权限
  • 说明:只有文件的拥有者和 root 才可以改变文件的权限

例如:

[root@VM-12-13-centos testLinux]# ll
total 4
-rw-r--r-- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod u-r test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
--w-r--r-- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod u+x test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
--wxr--r-- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod u-rwx test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
----r--r-- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod g-rw test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
-------r-- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod g-rwx,o-rwx test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
---------- 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# chmod a+rwx test2.txt
[root@VM-12-13-centos testLinux]# ll
total 4
drwxr-xr-x 3 root root 4096 Oct  9 09:38 First
-rwxrwxrwx 1 root root    0 Oct 12 22:20 test2.txt
[root@VM-12-13-centos testLinux]# ll
total 8
drwxrwxr-x 3 root root 4096 Oct  9 09:38 First
-rw-r--r-- 1 root root    0 Oct 12 23:47 test2.txt
-rw-r--r-- 1 root root   80 Oct 22 09:45 test.c
[root@VM-12-13-centos testLinux]# cat test.c
#include<stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}

[root@VM-12-13-centos testLinux]# su guest
[guest@VM-12-13-centos testLinux]$ echo "I am here" >> test.c
bash: test.c: Permission denied
[root@VM-12-13-centos testLinux]# ll
total 4
drwxr-xr-x 3 root root 4096 Oct  9 09:38 First
-rw-r--r-- 1 root root    0 Oct 12 23:00 test3.txt
[root@VM-12-13-centos testLinux]# chmod a-rwx test3.txt
[root@VM-12-13-centos testLinux]# ll
total 4
drwxr-xr-x 3 root root 4096 Oct  9 09:38 First
---------- 1 root root    0 Oct 12 23:00 test3.txt
[root@VM-12-13-centos testLinux]# echo "you can see me " >> test3.txt
[root@VM-12-13-centos testLinux]# cat test3.txt
you can see me 				#(思考为什么权限全部关闭仍能访问)

root是权限至高者,无视权限约束。

八进制设置的话
chmod 664 test.txt
664解释为 110 110 100 分别对应用户所属组和other

当代码是777时,将会很危险(权限全部开放)
请添加图片描述

🕤 3.4.2 chown

功能:修改文件的拥有者
格式chown [参数] 用户名 文件名
实例

[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins3.txt
[root@VM-12-13-centos mytmp]# chown guest hins2.txt
[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 guest hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins 0 Oct 23 10:44 hins3.txt

🕤 3.4.3 chgrp

功能:修改文件或目录的所属组
格式chgrp [参数] 用户组名 文件名
常用选项-R 递归修改文件或目录的所属组
实例

[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins3.txt
[root@VM-12-13-centos mytmp]# chgrp guest hins2.txt
[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 hins guest 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins  0 Oct 23 10:44 hins3.txt

同时修改

[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins3.txt
[root@VM-12-13-centos mytmp]# chown guest:guest hins2.txt
[root@VM-12-13-centos mytmp]# ll
total 0
-rw-rw-r-- 1 guest guest 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins  0 Oct 23 10:44 hins3.txt

小结:

  • chmod(设置文件属性)
  • chown/chgrp(设置文件所属角色)

🕘 3.5 file 指令

功能说明 :辨识文件类型。
语法file [ 选项 ] 文件或目录 ...
常用选项

  • -c 详细显示指令执行过程,便于排错或分析程序执行的情形。
  • -z 尝试去解读压缩文件的内容。
[root@VM-12-13-centos testLinux]# ll
total 8
drwxrwxr-x 3 root root 4096 Oct  9 09:38 First
-rw-r--r-- 1 root root    0 Oct 12 23:47 test2.txt
-rw-r--r-- 1 root root   80 Oct 22 09:45 test.c
[root@VM-12-13-centos testLinux]# file First
First: directory
[root@VM-12-13-centos testLinux]# file test2.txt
test2.txt: empty
[root@VM-12-13-centos testLinux]# file test.c
test.c: C source, ASCII text
[root@VM-12-13-centos testLinux]# file -c test.c
cont	offset	type	opcode	mask	value	desc

🕘 3.6 umask

功能

  • 查看或修改文件掩码
  • 新建文件默认权限 =0666
  • 新建目录默认权限 =0777
  • 但实际上你所创建的文件和目录,看到的权限往往不是上面这个值。原因就是创建文件或目录的时候还要受到umask的影响。假设默认权限是 mask ,则实际创建的出来的文件权限是 : mask & ~umask

格式umask 权限值
说明 :凡是在umask中出现的权限,都必须在起始权限中去掉!root用户默认掩码值为 0022(000 010 010) ,普通用户默认为0002(000 000 010) 。

total 8
drwxrwxr-x 3 root root 4096 Oct  9 09:38 First		# 目录
-rw-r--r-- 1 root root    0 Oct 12 23:47 test2.txt
-rw-r--r-- 1 root root   80 Oct 22 09:45 test.c

实例

\# umask 755
\# umask // 查看
\# umask 044// 设置

问题:umask的原理是减法吗?

假定我们umask设置为111(输入是umask 0111)(即001 001 001),起始权限为666(即110 110 110)

[root@VM-12-13-centos testLinux]# umask 0111
[root@VM-12-13-centos testLinux]# touch file
[root@VM-12-13-centos testLinux]# ll
total 8
-rw-rw-rw- 1 root root    0 Oct 23 11:52 file

如果是减法的话,应该是-r-xr-xr-x(101 101 101),可看上面指令并非如此。其实原理是 最终权限 = 起始权限 & (~umask)

按公式计算一下:
起始权限:666 110 110 110
umask: 0111 001 001 001
~umask: 110 110 110
起始权限 & (~umask):
110 110 110
即 rw- rw- rw-

🕘 3.7 目录的权限

如果我们要进入一个目录,需要什么权限?

  • 可执行权限(x): 如果目录没有可执行权限, 则无法cd到目录中
  • 可读权限(r):如果目录没有可读权限, 则无法用ls等命令查看目录中的文件内容.
  • 可写权限(w):如果目录没有可写权限, 则无法在目录中创建文件, 也无法在目录中删除文件

所以这也是为什么系统规定目录的起始权限从777开始,所有的目录被创建出来,一般都要能够被进入。普通文件的起始权限是666。

那么问题来了,只要用户具有目录的写权限,用户就可以删除目录中的文件, 而不论这个用户是否有这个文件的写权限。 这样的话,张三创建的一个文件, 岂不是可以被李四删掉?

因此,为了避免这个不科学的操作,接下来我们模拟上面的情景,寻找解决方案。

🕘 3.8 粘滞位

Linux系统中有很多人,我们需要在一个公共的目录下,进行临时文件的操作(增删查改)

在这里插入图片描述
即我们通过ll /就会发现有一个这样的目录,这就是公共目录,即每一个用户都可以在这里放入各种文件,也可以删除各种文件,因此为了避免张三的文件被李四的操作删掉,我们就需要利用粘滞位。

因此按照这种步骤,我们模仿上述的场景,从创建一个类似于上面的公共目录开始:

  1. 创建公共目录
    在这里插入图片描述
  2. 以不同身份在mytmp公共目录中创建文件
[hins@VM-12-13-centos root]$ cd /mytmp
[hins@VM-12-13-centos mytmp]$ touch hins1.txt
[hins@VM-12-13-centos mytmp]$ touch hins2.txt
[hins@VM-12-13-centos mytmp]$ touch hins3.txt		# 以hins身份创建3个文件
[hins@VM-12-13-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 hins hins 0 Oct 23 10:43 hins1.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins3.txt
[hins@VM-12-13-centos mytmp]$ echo "Hello World" > hins1.txt
[hins@VM-12-13-centos mytmp]$ cat hins1.txt
Hello World
[guest@VM-12-13-centos root]$ cd /mytmp
[guest@VM-12-13-centos mytmp]$ touch guest1.txt
[guest@VM-12-13-centos mytmp]$ touch guest2.txt
[guest@VM-12-13-centos mytmp]$ touch guest5.txt		# 以guest身份创建3个文件
[guest@VM-12-13-centos mytmp]$ ll
total 4
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest1.txt
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest2.txt
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest5.txt
-rw-rw-r-- 1 hins  hins  12 Oct 23 10:45 hins1.txt
-rw-rw-r-- 1 hins  hins   0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins   0 Oct 23 10:44 hins3.txt
[guest@VM-12-13-centos mytmp]$ cat hins1.txt
Hello World			# guest对于hins来说是其他人,而在other组中权限为可读,因此能打印
[guest@VM-12-13-centos mytmp]$ cat "I am here" >> hins1.txt
bash: hins1.txt: Permission denied		# 不可写
  1. 以guest的身份删除hins所创建的文件
[hins@VM-12-13-centos root]$ cd /mytmp
[hins@VM-12-13-centos mytmp]$ chmod 600 hins1.txt	# 先把该文件改成仅自己可读写
[hins@VM-12-13-centos mytmp]$ ll
total 4
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest1.txt
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest2.txt
-rw-rw-r-- 1 guest guest  0 Oct 23 10:48 guest5.txt
-rw------- 1 hins  hins  12 Oct 23 10:45 hins1.txt
-rw-rw-r-- 1 hins  hins   0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins   0 Oct 23 10:44 hins3.txt
[root@VM-12-13-centos ~]# su guest
[guest@VM-12-13-centos root]$ cd /mytmp
[guest@VM-12-13-centos mytmp]$ cat hins1.txt
cat: hins1.txt: Permission denied	# 发现无法访问

我们发现,hins1.txt的权限不让guest这个用户去写,于是guest这个用户一气之下把hins1.txt这个文件给删除了,这也正是我们在引入粘滞位之前所说的情况。

[guest@VM-12-13-centos mytmp]$ rm hins1.txt
rm: remove write-protected regular file ‘hins1.txt’? y
[guest@VM-12-13-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest1.txt
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest2.txt
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest5.txt
-rw-rw-r-- 1 hins  hins  0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins  0 Oct 23 10:44 hins3.txt

一个不能以另一个用户读、写、执行的文件,竟然可以让另一个用户删除,这显然不合理。因此,创建和删除的权限是目录的权限,即在目录中创建和删除里面文件的权限不是文件本身的权限,而是目录的权限。

在这时候,我们就需要一个策略,让一个公共目录,大家可以进行rwx操作,但是禁止大家互删对方的文件,即去掉目录的这个权限,因此我们需要给这个公共目录设置粘滞位。

  1. 给目录设置粘滞位

对于设置粘滞位,是以root身份设置的,对于root创建的公共目录来说,无论是hins还是guest都属于Others,因此我们设置的粘滞位是给Others设置的粘滞位。

[root@VM-12-13-centos mytmp]# chmod +t /mytmp
[root@VM-12-13-centos mytmp]# ls /mytmp -dl
drwxrwxrwt 2 root root 4096 Oct 23 11:01 /mytmp		# other组那里多了个t

当我们给目录设置完粘滞位之后,就需要验证一下是否还可以删除:

  1. 验证是否还可以互删
[guest@VM-12-13-centos mytmp]$ cd /mytmp
[guest@VM-12-13-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest1.txt
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest2.txt
-rw-rw-r-- 1 guest guest 0 Oct 23 10:48 guest5.txt
-rw-rw-r-- 1 hins  hins  0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins  hins  0 Oct 23 10:44 hins3.txt
[guest@VM-12-13-centos mytmp]$ rm * -rf
rm: cannot remove ‘hins2.txt’: Operation not permitted
rm: cannot remove ‘hins3.txt’: Operation not permitted
[guest@VM-12-13-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins2.txt
-rw-rw-r-- 1 hins hins 0 Oct 23 10:44 hins3.txt

目前可以看到guest用户删不了hins用户的文件,但可以读hins的文件。对于自己的文件,增删读写都是没有问题的。

注意:对于root用户来说,无论怎样设置都可以删除,因此root的权限非常重要

小结:
当一个目录被设置为"粘滞位"(用chmod +t),则该目录下的文件只能由:
一、超级管理员删除(root)
二、该目录的所有者删除
三、该文件的所有者删除

🕒 4. 关于权限的总结

  • 目录的可执行权限表示你能否在目录下执行命令。
  • 如果目录没有-x权限,则无法对目录执行任何命令,甚至无法 cd 进入目录,即使目录仍然有-r读权限(这个地方很容易犯错,认为有读权限就可以进入目录读取目录下的文件)。
  • 而如果目录具有-x权限,但没有-r权限,则用户可以执行命令,可以cd进入目录。但由于没有目录的读权限。
  • 所以在目录下,即使可以执行ls命令,但仍然没有权限读出目录下的文档。

OK,以上就是本期知识点“权限”的知识啦~~ ,感谢友友们的阅读。后续还会继续更新,欢迎持续关注哟📌~
💫如果有错误❌,欢迎批评指正呀👀~让我们一起相互进步🚀
🎉如果觉得收获满满,可以点点赞👍支持一下哟~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值