Linux的学习之路:4、权限

一、Linux权限的概念

权限我们都熟悉,最常见的就是在看电视时需要vip这个就是权限,然后在Linux就是有两个权限,就是管理员也就是超级用户和普通的用户

命令:su [用户名]

功能:切换用户。

例如,要从root用户切换到普通用户user,则使用 su user。 要从普通用户user切换到root用户则使用 suroot(root可以省略),此时系统会提示输入root用户的口令,测试如下。

root@VM-24-9-centos \~\]# su ly \[ly@VM-24-9-centos root\]$ su root Password: \[root@VM-24-9-centos \~\]# su ly \[ly@VM-24-9-centos root\]$ su Password: \^C \[ly@VM-24-9-centos root\]$ su Password: \[root@VM-24-9-centos \~\]#

二、Linux权限管理

1、文件访问者的分类

文件和文件目录的所有者:u---User

文件和文件目录的所有者所在的组的用户:g---Group

其它用户:o---Others

文件访问者就是这三类。

2、文件类型和访问权限

a、文件类型

d:文件夹

-:普通文件

l:软链接(类似Windows的快捷方式)

b:块设备文件(例如硬盘、光驱等)

p:管道文件

c:字符设备文件(例如屏幕等串口设备)

s:套接口文件

b、基本权限

i.读(r/4):Read对文件而言,具有读取文件内容的权限;对目录来说,具有浏览该目录信息的权限

ii.写(w/2):Write对文件而言,具有修改文件内容的权限;对目录来说具有删除移动目录内文件的权限

iii.执行(x/1):execute对文件而言,具有执行文件的权限;对目录来说,具有进入目录的权限

3、文件权限值的表示方法

如下面两个表格的表示

a、字符表示方法

|---------|---------|
| Linux表示 | 说明 |
| r-- | 只读 |
| -w- | 仅可写 |
| --x | 仅可执行 |
| rw- | 可读可写 |
| -wx | 可写和可执行 |
| r-x | 可读和可执行 |
| rwx | 可读可写可执行 |
| --- | 无权限 |

b、8进制数值表示方法

|------------|-----|-----|
| 权限符号(读写执行) | 八进制 | 二进制 |
| r | 4 | 100 |
| w | 2 | 010 |
| x | 1 | 001 |
| rw | 6 | 110 |
| rx | 5 | 101 |
| wx | 3 | 011 |
| rwx | 7 | 111 |
| --- | 0 | 000 |

4、文件访问权限的相关设置方法

使用chmod就可以修改文件的权限功能:设置文件的访问权限,格式:chmod [参数] 权限 文件名,常用选项有

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

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

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

格式:umask 权限值,说明:将现有的存取权限减去权限掩码后,即可产生建立文件时预设权限。超级用户默认掩码值为0022,普通用户默认为0002。

R -> 递归修改目录文件的权限

说明:只有文件的拥有者和root才可以改变文件的权限

①chmod命令权限值的格式

+:向权限范围增加权限代号所表示的权限

-:向权限范围取消权限代号所表示的权限

=:向权限范围赋予权限代号所表示的权限

用户符号:

u:拥有者

g:拥有者同组用

o:其它用户

a:所有用户

示例如下可以看到加上可执行去掉可执行。

root@VM-24-9-centos \~\]# ll total 132 drwxr-xr-x 5 root root 4096 Mar 24 18:50 d1 -rw-r--r-- 1 root root 179 Mar 24 18:58 d1.tgz -rw-r--r-- 1 root root 436 Mar 24 18:50 dd.zip -rw-r--r-- 1 root root 72 Mar 23 23:09 hello.c -rw-r--r-- 1 root root 29 Mar 24 17:21 t1.txt -rw-r--r-- 1 root root 29 Mar 24 17:32 t2.txt -rw-r--r-- 1 root root 108902 Mar 24 09:01 test.txt \[root@VM-24-9-centos \~\]# chmod u+x test.txt \[root@VM-24-9-centos \~\]# ll total 132 drwxr-xr-x 5 root root 4096 Mar 24 18:50 d1 -rw-r--r-- 1 root root 179 Mar 24 18:58 d1.tgz -rw-r--r-- 1 root root 436 Mar 24 18:50 dd.zip -rw-r--r-- 1 root root 72 Mar 23 23:09 hello.c -rw-r--r-- 1 root root 29 Mar 24 17:21 t1.txt -rw-r--r-- 1 root root 29 Mar 24 17:32 t2.txt -rwxr--r-- 1 root root 108902 Mar 24 09:01 test.txt \[root@VM-24-9-centos \~\]# chomd u-x test.txt bash: chomd: command not found \[root@VM-24-9-centos \~\]# chmod u-x test.txt \[root@VM-24-9-centos \~\]# ll total 132 drwxr-xr-x 5 root root 4096 Mar 24 18:50 d1 -rw-r--r-- 1 root root 179 Mar 24 18:58 d1.tgz -rw-r--r-- 1 root root 436 Mar 24 18:50 dd.zip -rw-r--r-- 1 root root 72 Mar 23 23:09 hello.c -rw-r--r-- 1 root root 29 Mar 24 17:21 t1.txt -rw-r--r-- 1 root root 29 Mar 24 17:32 t2.txt -rw-r--r-- 1 root root 108902 Mar 24 09:01 test.txt \[root@VM-24-9-centos \~\]#

②三位8进制数字

示例如下对照上面的表就可以更改权限了。

root@VM-24-9-centos \~\]# chmod 000 test.txt \[root@VM-24-9-centos \~\]# ll total 132 drwxr-xr-x 5 root root 4096 Mar 24 18:50 d1 -rw-r--r-- 1 root root 179 Mar 24 18:58 d1.tgz -rw-r--r-- 1 root root 436 Mar 24 18:50 dd.zip -rw-r--r-- 1 root root 72 Mar 23 23:09 hello.c -rw-r--r-- 1 root root 29 Mar 24 17:21 t1.txt -rw-r--r-- 1 root root 29 Mar 24 17:32 t2.txt ---------- 1 root root 108902 Mar 24 09:01 test.txt \[root@VM-24-9-centos \~\]# chmod 644 test.txt \[root@VM-24-9-centos \~\]# ll total 132 drwxr-xr-x 5 root root 4096 Mar 24 18:50 d1 -rw-r--r-- 1 root root 179 Mar 24 18:58 d1.tgz -rw-r--r-- 1 root root 436 Mar 24 18:50 dd.zip -rw-r--r-- 1 root root 72 Mar 23 23:09 hello.c -rw-r--r-- 1 root root 29 Mar 24 17:21 t1.txt -rw-r--r-- 1 root root 29 Mar 24 17:32 t2.txt -rw-r--r-- 1 root root 108902 Mar 24 09:01 test.txt \[root@VM-24-9-centos \~\]#

相关推荐
轻松Ai享生活10 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr11 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55511 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆11 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
2303_Alpha11 小时前
SpringBoot
笔记·学习
乌萨奇也要立志学C++11 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
萘柰奈11 小时前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
沐矢羽12 小时前
Tomcat PUT方法任意写文件漏洞学习
学习·tomcat
好奇龙猫12 小时前
日语学习-日语知识点小记-进阶-JLPT-N1阶段蓝宝书,共120语法(10):91-100语法+考え方13
学习
向阳花开_miemie12 小时前
Android音频学习(十八)——混音流程
学习·音视频