Linux的学习之路:4、权限

简介: 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 ~]#

三、思维导图

目录
相关文章
|
Linux 编译器 开发工具
【Linux快速入门(三)】Linux与ROS学习之编译基础(Cmake编译)
【Linux快速入门(三)】Linux与ROS学习之编译基础(Cmake编译)
659 2
|
6月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
392 3
|
存储 安全 Linux
|
Linux Shell
Linux系统文件默认权限
Linux系统文件默认权限
320 2
|
8月前
|
存储 IDE Linux
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)
本文详细介绍了在VMware虚拟机中安装CentOS 6.8的全过程。首先,需确保已安装VMware并开启V-CPU虚拟化功能,可通过BIOS设置或使用LeoMoon CPU-V工具检测。接着,下载CentOS镜像文件,并在VMware中新建虚拟机,配置CPU、内存、硬盘等参数。最后,加载ISO镜像启动虚拟机,按照提示完成CentOS的安装,包括语言、键盘、存储方式、地区、密码设置及硬盘分区等步骤。安装完成后,以root用户登录即可进入系统桌面,开始学习Linux命令和操作。
853 12
零基础保姆级教程!手把手教你免费玩转Linux CentOS安装+学习环境搭建(附避坑指南)
|
9月前
|
安全 Unix Linux
【Linux权限】—— 于虚拟殿堂,轻拨密钥启华章
25000多字详细讲解,深度剖析权限管理核心。从基础权限到复杂的特殊权限,逐一拆解,无论你是零基础小白还是经验丰富的运维人员,都能在这里找到提升技能的关键知识,全面掌握 Linux 权限管理。还不快来看看?
【Linux权限】—— 于虚拟殿堂,轻拨密钥启华章
|
8月前
|
安全 Linux 数据安全/隐私保护
【Linux】深入理解linux权限
本文深入解析Linux权限管理机制,涵盖权限概念、用户角色、文件属性及操作方法。文章分为前言、权限介绍、用户与角色、文件属性、权限修改及常见问题六大板块。详细说明了权限类型(r/w/x)、角色优先级、chmod/chown指令用法,以及目录权限、umask掩码、粘滞位等重点内容。掌握这些知识,可有效提升Linux系统安全性和灵活性,是管理员必备技能。喜欢的话别忘了点赞支持哦! ❤❤❤
479 6
|
9月前
|
安全 Linux 数据安全/隐私保护
Linux权限揭秘“Root与Sudo”
Root用户是Linux系统中的超级用户,拥有对系统的完全控制权。Root用户几乎可以执行任何命令,修改任何文件,甚至删除系统上的所有内容。因此,Root用户的使用需要非常谨慎,以避免潜在的安全风险。
448 6
|
Linux 数据安全/隐私保护
linux特殊权限!!
本文介绍了Linux系统中的特殊权限,包括suid、sgid和sbit。suid使普通用户在执行特定命令时获得root权限;sgid使用户在创建文件时继承目录的用户组权限;sbit确保用户只能删除自己在共享目录中创建的文件。此外,文章还讲解了chattr和lsattr命令,用于更改和查看文件的扩展属性,以及umask的概念和计算方法,帮助理解文件和目录的默认权限。
280 1
linux特殊权限!!
|
Linux Shell 数据安全/隐私保护
Linux 初学者必学的 10 个命令,学习!
【10月更文挑战第28天】
300 1
Linux 初学者必学的 10 个命令,学习!