shell命令以及运行原理
Shell的最简单定义:命令行解释器(command Interpreter)
Linux权限的概念
1.基本的用户认识
权限:
1.基本的用户认识
root用户 :超级管理员(不受权限约束的用户)
普通用户 :受权限约束的用户
[qwy@VM-4-3-centos lesson4]$ su
Password:
[root@VM-4-3-centos lesson4]# pwd
/home/qwy/lesson4
[root@VM-4-3-centos lesson4]# exit
exit
[qwy@VM-4-3-centos lesson4]$ su -
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@VM-4-3-centos ~]# ls /home
qwy
[root@VM-4-3-centos ~]# su qwy
[qwy@VM-4-3-centos root]$ whoami
qwy
[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.
2.深度解析权限
2.1什么是权限?(是什么)
1.什么是权限?(是什么)
(1) 权限是约束人的(一个或者某些群体)
(2) 没有对应的属性
如:电影网站没有听音乐的属性
如:音乐app没有看电影的属性
人:人有权限是因为其自身被赋予的角色;如校长是管理学校的,企业高管的管理公司的
拥有者(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
其中 d 代表文件类型
Linux的属性,是通过ll指令,显示的众多属性列中的第一列的第一个字符区分文件类型的
但是 gcc 等工具对文件可能有要求
Linux的文件类型:
1. - :普通文件 (源代码,库文件,可执行程序,文档压缩包等等)
2. d :目录文件
3. c :字符设备文件 (比如:键盘,显示器等等)
4. b :块设备 (比如:磁盘)
5. l :链接文件 (桌面的快捷方式本质就是一种链接文件)
6. p :管道文件
drwxrwxr-x
拥有者 所属组 其他人(没有对应的w权限)
2.2如何操作权限?(怎么办)
1.设置文件属性(只有文件的拥有者和root用户可以修改); 使用chmod
2.设置文件所属角色(只有文件的拥有者和root用户可以修改); chown/chgrp
[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
[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
[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
[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@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@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
[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
[qwy@VM-4-3-centos lesson5]$ sudo chgrp QWY test.txt
[qwy@VM-4-3-centos lesson5]$ sudo chown qwy:qwy test.txt
八进制位操作权限
拥有者 所属组 其他人(没有对应的w权限)
[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
[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
[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
[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
[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指令
[qwy@VM-4-3-centos lesson5]$ umask
0002
[qwy@VM-4-3-centos lesson5]$ umask 0000
[qwy@VM-4-3-centos lesson5]$ umask
0000
1.为什么我们创建目录或者普通文件,默认权限是如下的样子呢?
rwxrwxr-x
rw-rw-r--
Linux规定:
目录: 起始权限777(八进制位) --> 对应 111 111 111
普通文件: 起始权限666(八进制位) --> 对应 110 110 110
002 转化为二进制位 为 000 000 010
再对其进行取反 为 111 111 101
目录的最终权限为
(111 111 111) &
(111 111 101)
得到 111 111 101
普通文件的最终权限为
(110 110 110) &
(111 111 101)
得到 110 110 100
umask:默认要去掉的权限位是1,要保留的位为0
~umask:要去掉的权限位是0,要保留的位为1
file指令
[qwy@VM-4-3-centos lesson5]$ file test.txt
test.txt: ASCII text
[qwy@VM-4-3-centos lesson5]$ file dir
dir: directory
目录的权限
如果我们需要进入一个目录,需要什么权限?
答:经过验证我们发现必须要有x权限;
[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
[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
ls: cannot open directory .: Permission denied
[qwy@VM-4-3-centos dir]$ touch file.txt
touch: cannot touch ‘file.txt’: Permission denied
粘滞位
当一个目录被设置为"粘滞位"(用chmod +t),则该目录下的文件只能由
1.超级管理员删除
2.该目录的所有者删除
3.该文件的所有者删除
Linux系统中有很多人,我们需要在一个公共目录下,进行临时文件的操作(增删查改)
[qwy@VM-4-3-centos dir]$ ll /
drwxrwxrwt. 8 root root 4096 Dec 27 03:28 tmp
在公共目录下,所有的普通用户都属于other;拥有者和所属组都为root
在公共目录下的文件和目录,文件和目录可以对other(这里的other指的是除目录或者文件拥有者和所属组的 other)设置rw(读写权限),允许或者禁止other的读写
但是在公共目录下的文件和目录是否能被删除,取决于公共目录的权限,因此我们需要设置粘滞位
设置粘滞位之后,则该目录下的文件只能由
1.超级管理员删除
2.该目录的所有者删除
3.该文件的所有者删除
演示
[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
[root@VM-4-3-centos /]# chmod 777 mytmp
[root@VM-4-3-centos /]# ll
drwxrwxrwx 2 root root 4096 Dec 28 03:44 mytmp
[qwy@VM-4-3-centos dir]$ cd /mytmp
[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
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
hello qwy
[QWY@VM-4-3-centos mytmp]$ echo "hello QWY" >> qwy1.txt
bash: qwy1.txt: Permission denied
[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
[root@VM-4-3-centos /]# chmod +t /mytmp
[root@VM-4-3-centos /]# ll
drwxrwxrwt 2 root root 4096 Dec 28 03:55 mytmp
[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@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