shell命令以及运行原理
c
复制代码
Shell的最简单定义:命令行解释器(command Interpreter)
// 表现:你可以看到命令提示符,以及可以输入指令并且可以执行
// 用户 通过shell 来与操作系统交流
// 你 通过媒婆 与相亲对象交流
// shell存在的意义(拒绝非法指令)
// 变相的保护操作系统
// 执行命令,通过派生子进程的方式执行用户的指令
// 而shell本身并不执行对应的指令(一般而言)
// Linux操作系统 --> Linux内核
// shell --> Linux的外壳
// Linux操作系统 + shell = Linux
// windows的图形界面,本质也是一种外壳程序
// Linux的shell外壳程序与windows的图形界面类似
Linux权限的概念
1.基本的用户认识
c
复制代码
权限:
1.基本的用户认识
root用户 :超级管理员(不受权限约束的用户)
普通用户 :受权限约束的用户
// 普通用户到root用户的切换
[qwy@VM-4-3-centos lesson4]$ su // 登录root用户,但是还在当前目录下
Password:
[root@VM-4-3-centos lesson4]# pwd
/home/qwy/lesson4
[root@VM-4-3-centos lesson4]# exit // 退出root用户,回到普通用户(或者ctrl+d退出)
exit
[qwy@VM-4-3-centos lesson4]$ su - // 登录root用户,且是root下的目录
Password:
Last login: Tue Dec 27 03:55:52 CST 2022 on pts/0
Last failed login: Tue Dec 27 03:56:07 CST 2022 from 104.248.20.85 on ssh:notty
There were 3 failed login attempts since the last successful login.
[root@VM-4-3-centos ~]# pwd
/root
// root用户到普通用户的切换
[root@VM-4-3-centos ~]# ls /home
qwy
[root@VM-4-3-centos ~]# su qwy
[qwy@VM-4-3-centos root]$ whoami
qwy
// 如果是普通用户,想要用root的身份执行一条命令,但是不想变成root,怎么办?
// 使用 sudo指令
[qwy@VM-4-3-centos lesson5]$ whoami
qwy
[qwy@VM-4-3-centos lesson5]$ sudo whoami
[sudo] password for qwy:
root
[qwy@VM-4-3-centos lesson5]$ whoami
qwy
[qwy@VM-4-3-centos lesson5]$ sudo whoami // 第一次使用需要输入密码,第二次则不需要
root
// 如果出现报错
[qwy@VM-4-3-centos lesson5]$ sudo whoami
[sudo] password for qwy:
qwy is not in the sudoers file. This incident will be reported.
// 是由于系统不信任当前用户,需要将当前用户添加到 etc/sudoers 中
2.深度解析权限
2.1什么是权限?(是什么)
c
复制代码
1.什么是权限?(是什么)
(1) 权限是约束人的(一个或者某些群体)
(2) 没有对应的属性
如:电影网站没有听音乐的属性
如:音乐app没有看电影的属性
// 可以得出 :文件权限 = 人 + 文件属性
人:人有权限是因为其自身被赋予的角色;如校长是管理学校的,企业高管的管理公司的
// 在Linux中,人被分为3中角色
拥有者(owner)
所属组(group)
其他人(other)
// 为什么要存在所属组?
方便同一个团队的人相互访问,禁止团队外的人访问
文件属性:r(读权限) w(写权限) x(执行权限)
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 27 20:48 file.txt
// drwxrwxr-x
其中 d 代表文件类型
// window的文件类型主要是看文件的后缀
// Linux的文件类型与它的文件后缀无关;
Linux的属性,是通过ll指令,显示的众多属性列中的第一列的第一个字符区分文件类型的
// 如果要使用文件后缀,仅仅是给用户做一个提示符号来使用,后缀本质其实就是文件名的一部分
但是 gcc 等工具对文件可能有要求
Linux的文件类型:
// 必须掌握的
1. - :普通文件 (源代码,库文件,可执行程序,文档压缩包等等)
2. d :目录文件
// 了解即可
3. c :字符设备文件 (比如:键盘,显示器等等)
4. b :块设备 (比如:磁盘)
5. l :链接文件 (桌面的快捷方式本质就是一种链接文件)
6. p :管道文件
drwxrwxr-x
// rwx rwx r-x
拥有者 所属组 其他人(没有对应的w权限)
// - :代表没有权限(没有对应的r权限,w权限,x权限)
2.2如何操作权限?(怎么办)
c
复制代码
// 如何操作权限?(怎么办)
1.设置文件属性(只有文件的拥有者和root用户可以修改); 使用chmod
2.设置文件所属角色(只有文件的拥有者和root用户可以修改); chown/chgrp
// 1.设置文件属性; 使用chmod
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u-r test.txt // u代表owner; 去掉r权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
--w-rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u+x test.txt // u代表owner; 增加x权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
--wxrw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u-rwx test.txt // 去掉拥有者的所有权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
----rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u+rw test.txt // 增加拥有者的读写权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod g-rwx test.txt // 去掉所属组的所有权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw----r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod g+rw test.txt // 增加所属组的读写权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod o-r test.txt // 去掉其他人的读权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw---- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod o+rw test.txt // 增加其他人的读写权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-rw- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u-rwx,g-rwx test.txt // 去掉拥有者和所属组的所有权限
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-------rw- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod u+rw,g+rw,o-rwx test.txt
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw---- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod a-rwx test.txt // a代表全部(即拥有者,所属组,其他人)
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
---------- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod a+rwx test.txt
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rwxrwxrwx 1 qwy qwy 0 Dec 27 21:45 test.txt
// 演示(在没有对应权限时)
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-rw-- 1 qwy qwy 0 Dec 27 21:45 test.txt
[qwy@VM-4-3-centos lesson5]$ echo "hello qwy" > test.txt // 可写
[qwy@VM-4-3-centos lesson5]$ cat test.txt // 可读
hello qwy
[qwy@VM-4-3-centos lesson5]$ chmod u-rw test.txt // 去掉拥有者的读写权限之后
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
----rw-rw- 1 qwy qwy 10 Dec 27 22:11 test.txt
[qwy@VM-4-3-centos lesson5]$ cat test.txt // 不可以读
cat: test.txt: Permission denied
[qwy@VM-4-3-centos lesson5]$ echo "hello qwy1" > test.txt // 不可以写
-bash: test.txt: Permission denied
// 去掉拥有者qwy的权限,但是qwy也属于所属组,为什么不可以读写
// 匹配机制,先与拥有者匹配,如果匹配上,则不会去匹配所属组,匹配上所属组,则不会去匹配其他人
// root用户对于普通用户的文件权限操作(root不受权限约束)
[qwy@VM-4-3-centos lesson5]$ su
Password:
[root@VM-4-3-centos lesson5]# ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
----rw-rw- 1 qwy qwy 10 Dec 27 22:11 test.txt
[root@VM-4-3-centos lesson5]# chmod u+rw test.txt
[root@VM-4-3-centos lesson5]# ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-rw- 1 qwy qwy 10 Dec 27 22:11 test.txt
[root@VM-4-3-centos lesson5]# chmod a-rwx test.txt
[root@VM-4-3-centos lesson5]# ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
---------- 1 qwy qwy 12 Dec 27 22:22 test.txt
[root@VM-4-3-centos lesson5]# cat test.txt
hello qwy
[root@VM-4-3-centos lesson5]# echo "qwy qwy qwy" >test.txt
[root@VM-4-3-centos lesson5]# cat test.txt
qwy qwy qwy
// 使用root权限修改文件的拥有者
[root@VM-4-3-centos lesson5]# ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-rw- 1 qwy qwy 12 Dec 27 22:22 test.txt
[qwy@VM-4-3-centos lesson5]$ sudo chown QWY test.txt // 改变文件的拥有者
[sudo] password for qwy:
[root@VM-4-3-centos lesson5]# ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw-rw- 1 QWY qwy 12 Dec 27 22:22 test.txt
// 此时qwy属于所属组因此依旧可以读写;但是不是拥有者,因此无法修改任何角色的权限
[root@VM-4-3-centos lesson5]# cat test.txt
hello qwy
[root@VM-4-3-centos lesson5]# echo "qwy qwy qwy" >test.txt
[root@VM-4-3-centos lesson5]# cat test.txt
qwy qwy qwy
// 使用root权限修改文件的所属组
[qwy@VM-4-3-centos lesson5]$ sudo chgrp QWY test.txt // 改变文件的所属组
// 使用root权限修改文件的所属组和所属组
[qwy@VM-4-3-centos lesson5]$ sudo chown qwy:qwy test.txt // 改变文件的所属组
八进制位操作权限
c
复制代码
拥有者 所属组 其他人(没有对应的w权限)
// r w x r w x r - x
// 对应二进制位 1 1 1 1 1 1 1 0 1
// 转化为八进制位 7 7 5
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw----rw- 1 qwy qwy 12 Dec 27 22:22 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod 000 test.txt // 000为八进制位
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
---------- 1 qwy qwy 12 Dec 27 22:22 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod 777 test.txt // 777为八进制位
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rwxrwxrwx 1 qwy qwy 12 Dec 27 22:22 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod 660 test.txt // 660为8进制位
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-rw-rw---- 1 qwy qwy 12 Dec 27 22:22 test.txt
[qwy@VM-4-3-centos lesson5]$ chmod 444 test.txt // 100 100 100 为2进制
[qwy@VM-4-3-centos lesson5]$ ll
total 8
drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
-r--r--r-- 1 qwy qwy 12 Dec 27 22:22 test.txt
3.为什么要有权限?(为什么)
umask指令
c
复制代码
// umask 权限掩码:默认为0002(第一位为0; 后3位为八进制位,当前我们只关注后三位)
[qwy@VM-4-3-centos lesson5]$ umask
0002
[qwy@VM-4-3-centos lesson5]$ umask 0000 // 权限掩码是可以进行修改的
[qwy@VM-4-3-centos lesson5]$ umask
0000
c
复制代码
// 权限是为了便于我们系统进行安全管理的
1.为什么我们创建目录或者普通文件,默认权限是如下的样子呢?
// drwxrwxr-x 2 qwy qwy 4096 Dec 27 20:48 dir
// -rw-rw-r-- 1 qwy qwy 0 Dec 27 21:45 test.txt
rwxrwxr-x // 对应八进制位 775
rw-rw-r-- // 对应八进制位 664
Linux规定:
目录: 起始权限777(八进制位) --> 对应 111 111 111
普通文件: 起始权限666(八进制位) --> 对应 110 110 110
// 最终权限 = 起始权限 & (~umask)
002 转化为二进制位 为 000 000 010
再对其进行取反 为 111 111 101
目录的最终权限为
(111 111 111) & // 111 111 111 -->目录的起始权限
(111 111 101) // 111 111 101 -->~umask
得到 111 111 101 // 目录的最终权限为 775
普通文件的最终权限为
(110 110 110) &
(111 111 101)
得到 110 110 100 // 普通文件的最终权限为 664
// 如何设置umask
umask:默认要去掉的权限位是1,要保留的位为0
~umask:要去掉的权限位是0,要保留的位为1
file指令
c
复制代码
// 功能说明:辨识文件类型。
[qwy@VM-4-3-centos lesson5]$ file test.txt
test.txt: ASCII text
[qwy@VM-4-3-centos lesson5]$ file dir
dir: directory
目录的权限
c
复制代码
如果我们需要进入一个目录,需要什么权限?
答:经过验证我们发现必须要有x权限;
// 所有的目录被创建出来,一般都要能被进入(这也就是为什么目录的起始权限为777)
[qwy@VM-4-3-centos lesson5]$ ll
total 4
drwxrwxr-x 2 qwy qwy 4096 Dec 28 00:49 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 28 00:49 test.txt
// 去掉目录dir的读写权限
[qwy@VM-4-3-centos lesson5]$ chmod u-rw dir
[qwy@VM-4-3-centos lesson5]$ ll
total 4
d--xrwxr-x 2 qwy qwy 4096 Dec 28 00:49 dir
-rw-rw-r-- 1 qwy qwy 0 Dec 28 00:49 test.txt
// 依旧可以进入目录
[qwy@VM-4-3-centos lesson5]$ cd dir
[qwy@VM-4-3-centos dir]$ ll // 无法读(需要 r权限)
ls: cannot open directory .: Permission denied
[qwy@VM-4-3-centos dir]$ touch file.txt // 无法写 (需要 w权限)
touch: cannot touch 'file.txt': Permission denied
粘滞位
c
复制代码
当一个目录被设置为"粘滞位"(用chmod +t),则该目录下的文件只能由
1.超级管理员删除
2.该目录的所有者删除
3.该文件的所有者删除
// 背景
Linux系统中有很多人,我们需要在一个公共目录下,进行临时文件的操作(增删查改)
//公共目录是由root用户创建的
[qwy@VM-4-3-centos dir]$ ll /
drwxrwxrwt. 8 root root 4096 Dec 27 03:28 tmp
// tmp为进行临时文件操作的公共目录; 其中t表示对这个目录设置了粘滞位; 且这个目录的拥有者和所属组都为root
// 设置粘滞位的作用
在公共目录下,所有的普通用户都属于other;拥有者和所属组都为root
在公共目录下的文件和目录,文件和目录可以对other(这里的other指的是除目录或者文件拥有者和所属组的 other)设置rw(读写权限),允许或者禁止other的读写
但是在公共目录下的文件和目录是否能被删除,取决于公共目录的权限,因此我们需要设置粘滞位
设置粘滞位之后,则该目录下的文件只能由
1.超级管理员删除
2.该目录的所有者删除
3.该文件的所有者删除
演示
c
复制代码
// 在根目录下创建一个mytmp目录
[root@VM-4-3-centos /]# pwd
/
[root@VM-4-3-centos /]# mkdir mytmp
[root@VM-4-3-centos /]# ll
drwxr-xr-x 2 root root 4096 Dec 28 03:44 mytmp
// 将mytmp的权限设置为777,这样所有人都可以访问
[root@VM-4-3-centos /]# chmod 777 mytmp
[root@VM-4-3-centos /]# ll
drwxrwxrwx 2 root root 4096 Dec 28 03:44 mytmp
// 普通用户可以在mytmp中进行读写
[qwy@VM-4-3-centos dir]$ cd /mytmp // qwy普通用户
[qwy@VM-4-3-centos mytmp]$ pwd
/mytmp
[qwy@VM-4-3-centos mytmp]$ touch qwy1.txt
[qwy@VM-4-3-centos mytmp]$ touch qwy2.txt
[qwy@VM-4-3-centos mytmp]$ touch qwy3.txt
[qwy@VM-4-3-centos mytmp]$ touch qwy4.txt
[qwy@VM-4-3-centos mytmp]$ echo "hello qwy" >> qwy1.txt // >> 追加
[qwy@VM-4-3-centos mytmp]$ cat qwy1.txt
hello qwy
[qwy@VM-4-3-centos mytmp]$ su QWY // 切换到QWY普通用户
Password:
[QWY@VM-4-3-centos mytmp]$ ll
total 4
-rw-rw-r-- 1 qwy qwy 10 Dec 28 03:50 qwy1.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy2.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy3.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy4.txt
[QWY@VM-4-3-centos mytmp]$ cat qwy1.txt // 因为qwy1.txt允许other的读权限,所以可以读
hello qwy
[QWY@VM-4-3-centos mytmp]$ echo "hello QWY" >> qwy1.txt
bash: qwy1.txt: Permission denied // 因为qwy1.txt不允许other的写权限,所以不可以写
// 目录没有设置粘滞位,所以所有的普通用户都可以对公共目录中的文件进行删除
// 目录中的文件是否可以被删除,取决于目录所赋予的权限(也就是粘滞位)
[QWY@VM-4-3-centos mytmp]$ rm -rf qwy1.txt
[QWY@VM-4-3-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy2.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy3.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy4.txt
// 对公共目录mytmp设置粘滞位(必须是root权限下设置)
// 设置粘滞位的目的:让一个公共目录,大家各自可以进行rwx操作,但是禁止大家互删对方的文件
// 所以给公共目录设置粘滞位
[root@VM-4-3-centos /]# chmod +t /mytmp
[root@VM-4-3-centos /]# ll
drwxrwxrwt 2 root root 4096 Dec 28 03:55 mytmp // 默认给other设置的粘滞位
// 切换到QWY普通用户下
[root@VM-4-3-centos /]# su QWY
[QWY@VM-4-3-centos /]$ cd mytmp
[QWY@VM-4-3-centos mytmp]$ ll
total 0
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy2.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy3.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy4.txt
[QWY@VM-4-3-centos mytmp]$ rm -rf qwy2.txt // 设置粘滞位之后,则无法删除其他用户的文件
rm: cannot remove 'qwy2.txt': Operation not permitted
[QWY@VM-4-3-centos mytmp]$
// root为超级用户,且这个公共目录属于root,因此root可以删除公共目录下任何用户的文件
[root@VM-4-3-centos /]# cd mytmp
[root@VM-4-3-centos mytmp]# ll
total 0
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy2.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy3.txt
-rw-rw-r-- 1 qwy qwy 0 Dec 28 03:53 qwy4.txt
[root@VM-4-3-centos mytmp]# rm * -rf
[root@VM-4-3-centos mytmp]# ll
total 0