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 ~]#

三、思维导图

相关推荐
K3njuan8 分钟前
《数据结构》学习系列
学习
玉树临风江流儿8 分钟前
Linux驱动开发(速记版)--设备模型
linux·驱动开发
结衣结衣.10 分钟前
C++ 类和对象的初步介绍
java·开发语言·数据结构·c++·笔记·学习·算法
杰哥在此21 分钟前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
枫叶丹42 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
刻词梨木2 小时前
ubuntu中挂载点内存不足,分配不合理后使用软链接的注意事项
linux·运维·ubuntu
limengshi1383922 小时前
通信工程学习:什么是RIP路由信息协议
网络·网络协议·学习·智能路由器·信息与通信
灯火不休ᝰ3 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
xiaobuding_QAQ3 小时前
自用Proteus(8.15)常用元器件图示和功能介绍(持续更新...)
单片机·嵌入式硬件·学习·proteus
wei_shuo4 小时前
偏标记学习+图像分类(论文复现)
学习·分类·数据挖掘