Linux严格意义上说的是一个操作系统(os),我们称之为“核心(kernel)“ ,但我们一般用户,不能直接使用kernel。而是通过kernel的**“外壳”程序**,也就是所谓的shell,来与kernel沟通。如何理解?为什么不能直接使用kernel?
从技术角度,Shell的最简单定义:**命令行解释器(command Interpreter)**主要包含:
对比 windows GUI,我们操作windows 不是直接操作windows内核,而是通过图形接口,点击,从而完成我们的操作(比如进入D盘的操作,我们通常是双击D盘盘符.或者运行起来一个应用程序)。
shell 对于Linux,有相同的作用,主要是对我们的指令进行解析,解析指令给Linux内核。反馈结果在通过内核运行出结果,通过shell解析给用户。
shell 与 bash 的区别
shell : 是所有外壳程序的统称 bash : 是一种具体的 shell (Linux 下的)
Linux下有两种用户:超级用户(root)、普通用户。
例如,要从 root用户 切换到 普通用户user,则使用 su user,但是无需输入密码。 要从 普通用户user 切换到 root用户 则使用 su root**(root可以省略),此时系统会提示输入root用户的密码**。
**注: ** 如果我们单纯的想提升当前指令的权限,可以给指令带上 sudo
问题:为什么要有所属组的存在呢?
一般在公司中,都是分部门的,有些公司规定可能有几个部门一起研发一样东西,那么这几个部门就存在了竞争。 那在一个部门里面,用 Linux 写文件的时候,部门内的成员是需要一起看文件或者写文件的,而对于其他部门,这些文档可能是不允许别人看到的。 如果这个时候将文件设置为其他人不允许读或者写的话,那么只有拥有者可以看到,那么对于同一个部门里面的成员,也无法读文件和写文件,就造成了麻烦。 为了解决这种麻烦,就引入了 所属组,在所属组中的成员与拥有者一样拥有对文件的权限,而只有在所属组外的其他人,才会没有权限,这就很好的解决了这个问题。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uB3Q4KAw-1658632333312)(…/img/image-20220718134602943.png)]
文件类型:
# d :文件夹
# - :普通文件
# l :软链接(类似于Windows的快捷方式)
# b : 块设备文件(例如硬盘、光驱等)
# p : 管道文件
# c : 字符设备文件(例如屏幕等串口设备)
# s : 套接口文件
基本权限:
注:在权限的三位中,第一位是 r ,第二位是 w ,第三位是 x ,若某个位置上是 - ,则表示无该权限。
a、字符表示方法
Linux表示 | 说明 | Linux表示 | 说明 | Linux表示 | 说明 |
---|---|---|---|---|---|
r– | 只可读 | rw- | 可读可写 | rwx | 可读可写可执行 |
-w- | 只可写 | r-x | 可读可执行 | — | 无权限 |
–x | 只可执行 | -wx | 可写可执行 |
b、八进制数值表示方法
权限符号 | 八进制 | 二进制 |
---|---|---|
r | 4 | 100 |
w | 2 | 010 |
x | 1 | 001 |
rw | 6 | 110 |
rx | 5 | 101 |
wx | 3 | 011 |
rwx | 7 | 111 |
— | 0 | 000 |
功能:设置文件的访问权限 **格式:**chmod [参数] 权限 文件名
常用选项:
chmod 命令权限值的格式:
举例:
#给拥有者进行权限操作
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod u+x tmp.txt #给user加上可执行权限
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rwxrw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod u-rwx tmp.txt #将user的权限都去掉
[liren@VM-8-2-centos tmp1]$ ll
total 0
----rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ cat tmp.txt
cat: tmp.txt: Permission denied #访问权限等受阻了
#给所属组与其他人进行权限操作
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod g-rw tmp.txt #去掉所属组的rw权限
[liren@VM-8-2-centos tmp1]$ chmod o-r tmp.txt #去掉其他人的r权限
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw------- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod g+rw,o+r tmp.txt #也可以同时给多个角色设置权限
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
# 若没有指明给哪个用户设置权限,则默认为全体用户,即 a 或 all
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod +x tmp.txt #没有指定哪个用户
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rwxrwxr-x 1 liren liren 0 Jul 18 14:44 tmp.txt
注:root 超级用户不会受到任何的权限限制,而普通用户会。 普通用户若要让自己获得文件的权限, 需要获得拥有者的同意,或者用 sudo 进行短暂的权限提升。
参考笔记的文件权限值的表示方法的八进制数字。
举例:
[liren@VM-8-2-centos tmp1]$ clear
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rwxrwxr-x 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chmod 664 tmp.txt #使用八进制进行修改
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
功能:修改文件的**拥有者** 格式:chown [参数] 用户名 文件名 常用选项:-R 递归修改文件或目录的拥有者
实例:
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chown root tmp.txt #若不加sudo则弹出禁止修改的提示
chown: changing ownership of ‘tmp.txt’: Operation not permitted
[liren@VM-8-2-centos tmp1]$ sudo chown root tmp.txt #加了sudo后即可修改拥有者
[sudo] password for liren:
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 root liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ sudo chown liren tmp.txt
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
注:如果要一次性将拥有者与所属组修改,可以用:分隔开
举例:
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 root root 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ sudo chown liren:liren tmp.txt #使用 :分割开即可
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
功能:修改文件或目录的**所属组** 格式:chgrp [参数] 用户组名 文件名 常用选项:-R 递归修改文件或目录的所属组
实例:
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ chgrp root tmp.txt #若不加sudo则弹出禁止修改的提示
chgrp: changing group of ‘tmp.txt’: Operation not permitted
[liren@VM-8-2-centos tmp1]$ sudo chgrp root tmp.txt #加了sudo后即可修改所属组
[sudo] password for liren:
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren root 0 Jul 18 14:44 tmp.txt
功能:查看或修改文件掩码
新建文件夹默认权限=0666 ,新建目录默认权限=0777 。但实际上你所创建的文件和目录,看到的权限往往不是上面这个值。原因就是创建文件或目录的时候还要受到umask的影响。假设默认权限是mask,则实际创建的出来的文件权限是: mask & (~umask)
格式:umask 权限值 说明:将现有的存取权限减去权限掩码后,即可产生建立文件时预设权限。超级用户默认掩码值为0022,普通用户默认为0002。
也就是说,凡是在 umask 中出现的权限位,都不应再在默认权限中出现,还有要注意到的是,umask 调整的是掩码,而不是默认权限。
实例:
# umask 755 //修改为755
# umask //查看
# umask 044 //修改为044
[liren@VM-8-2-centos tmp1]$ ll
total 0
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ umask 755
[liren@VM-8-2-centos tmp1]$ touch file1
[liren@VM-8-2-centos tmp1]$ ll
total 0
-----w--w- 1 liren liren 0 Jul 18 16:59 file1
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
[liren@VM-8-2-centos tmp1]$ umask 044
[liren@VM-8-2-centos tmp1]$ touch file2
[liren@VM-8-2-centos tmp1]$ ll
total 0
-----w--w- 1 liren liren 0 Jul 18 16:59 file1
-rw--w--w- 1 liren liren 0 Jul 18 16:59 file2
-rw-rw-r-- 1 liren liren 0 Jul 18 14:44 tmp.txt
语法: file [选项] 文件或目录… 功能: 辨识文件类型
常用选项:
举例:
[liren@VM-8-2-centos code]$ file sort.c
sort.c: C source, ASCII text
**语法:**chomd +t 文件名 **功能:**防止其他人(除了root)删除了不该删除的。
当一个目录被设置为 “粘滞位”(用chmod +t), 则该目录下的文件只能由
注:如果目录本身就是你的,粘滞位就没有效果。