首页 > 技术文章 > Linux文件权限

elijah-li 2021-12-15 20:08 原文

内容概要

  • 文件权限
  • umask
  • 系统优化

内容概要

一、文件权限

1、权限简介

1、权限分为3个部分
	可读(r)
	可写(w)
	可执行(x)
	没有对应权限(-)

2、权限位


-rwxr-xr-x
	
# 权限位
	权限位主要分为三个部分,分别是属主、属组以及其他人
	rwx		: 属主
	r-x		: 属组
	r-x		: 其他人

# 权限归属符号
属主 : u
属组 : g
其他用户 : o

权限不能限制超管用户 root
超管用户 root 超越权限

3、添加权限

权限代表数字

八进制

可读(r) : 4
可写(w) : 2
可执行(x)  : 1
无权限    : 0

# r w x 顺序不能乱
r-- : 4
rw- : 6
rwx : 7
r-x : 5
--- : 0

数字添加权限

# 新建一个文件
[root@localhost ~]# touch index
[root@localhost ~]# ll | grep index
-rw-r--r--  1 root root    0 Dec 15 14:49 index

# 命令格式: 
	chmod [参数(可选)] [权限数字] [文件路径]
	chmod 700 /root/index

[root@localhost ~]# chmod 700 /root/index
[root@localhost ~]# ll | grep index
-rwx------  1 root root    0 Dec 15 14:49 index

700 : 属主是7,即可读写执行,rwx
	   属组和其他用户权限位是0,用 --- 代替

用用户权限位符添加与去除

# 权限位符号
属主 : u
属组 : g
其他用户 : o

# 命令格式
	chmod [参数(可选)] [所属位置符 + 权限符] [文件路径]

# 给属组添加可读权限
	chmod g+r /root/index
[root@localhost ~]# chmod g+r /root/index
[root@localhost ~]# ll | grep index
-rwxr-----  1 root root    0 Dec 15 14:49 index

# 给其他用户添加可写权限
	chmod o+w /root/index
[root@localhost ~]# chmod o+w /root/index
[root@localhost ~]# ll | grep index
-rwxr---w-  1 root root    0 Dec 15 14:49 index

# 可以给多个权限位添加权限,用逗号隔开
	chmod g+x,o+x /root/index
[root@localhost ~]# chmod g+x,o+x /root/index
[root@localhost ~]# ll | grep index
-rwxr-x-wx  1 root root    0 Dec 15 14:49 index

# 去除三个权限位的可执行权限
	chmod u-x,g-x,o-x /root/index
[root@localhost ~]# chmod u-x,g-x,o-x /root/index
[root@localhost ~]# ll | grep index
-rw-r---w-  1 root root    0 Dec 15 14:49 index

给文件夹内所有文件添加权限(递归添加)

递归添加权限参数: -R

1、创建文件夹test,并在里面创建5个普通文件
	mkdir test
	echo {1..5}.txt | xargs touch

[root@localhost ~]# cd test
[root@localhost test]# echo {1..5}.txt | xargs touch 
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 15 15:10 1.txt
-rw-r--r-- 1 root root 0 Dec 15 15:10 2.txt
-rw-r--r-- 1 root root 0 Dec 15 15:10 3.txt
-rw-r--r-- 1 root root 0 Dec 15 15:10 4.txt
-rw-r--r-- 1 root root 0 Dec 15 15:10 5.txt

2、给test目录内所有文件添加可写权限
	数字设置 : chmod -R 111 /root/test

[root@localhost ~]# chmod -R 111 /root/test
[root@localhost ~]# ll /root/test
total 0
---x--x--x 1 root root 0 Dec 15 15:10 1.txt
---x--x--x 1 root root 0 Dec 15 15:10 2.txt
---x--x--x 1 root root 0 Dec 15 15:10 3.txt
---x--x--x 1 root root 0 Dec 15 15:10 4.txt
---x--x--x 1 root root 0 Dec 15 15:10 5.txt

	权限符设置:chmod -R u+r,g+r,o+r /root/test

[root@localhost ~]# chmod -R u+r,g+r,o+r /root/test
[root@localhost ~]# ll /root/test
total 0
-r-xr-xr-x 1 root root 0 Dec 15 15:10 1.txt
-r-xr-xr-x 1 root root 0 Dec 15 15:10 2.txt
-r-xr-xr-x 1 root root 0 Dec 15 15:10 3.txt
-r-xr-xr-x 1 root root 0 Dec 15 15:10 4.txt
-r-xr-xr-x 1 root root 0 Dec 15 15:10 5.txt

4、测试权限

测试权限对于用户的意义

​ 1、普通用户是严格遵守权限的
​ 2、root用户是高于权限
​ 3、权限需要重新登才生效(su和su - 都可以)

​ 如果一个文件(1.txt)只有 test9 属组才有可写的权限,那么 可以在 /etc/passwd 目录下把 test10 用户添加进 test9 的属组,重新登录一下,那么 test10 用户也能写入 1.txt 文件了

su 和 su - 的区别:

​ 他们的加载环境变量不一样

环境变量文件:
	etc/profile
	etc/bashrc
	~/.bashrc
	~/.bash_profile

环境变量文件夹:
	etc/profile.d

su <用户名> : 切换用户

加载的环境变量:
	/etc/profile.d --> /etc/bashrc --> ~/.bashrc

su - <用户名> : 重新登录用户

加载的环境变量与重启系统加载的环境变量文件一致:
	etc/profile.d --> etc/profile --> etc/bashrc --> ~/.bashrc --> ~/.bash_profile

测试权限对于目录的意义

文件夹 a/b/c
普通用户执行命令:
	chmod -R 000 ./a
会导致 a 文件夹的全选被清空之后,就没有可读权限继续修改下一层文件夹的权限了
而超管账户 root 就不会收到权限的限制, 可以执行这个命令

​ 1、文件可读,路径的最小权限是必须拥有可执行权限。
​ 2、文件可写,路径的最小权限是必须拥有可执行权限。
​ 3、文件可执行,路径的最小权限是必须拥有可读可执行权限。文件本身也要可读可执行

1、普通用户创建叠层的文件夹和里面的可执行文件 1.sh
    [elijah@localhost tmp]$ mkdir -p a/b/c
    [elijah@localhost tmp]$ touch a/b/c/1.sh
2、在 root 账户下修改 a 文件夹的所有文件权限都为空
	[root@localhost tmp]# chmod -R 000 a
3、给 a 文件夹下所有文件添加可执行权限 x  (如果只给 a 文件夹添加可执行文件,普通用户仍然无法读写 a/b/c/1.sh 文件)
	[root@localhost tmp]# chmod -R u+x a
4、这时,用户就可以对 a/b/c/1.sh 文件进行读写操作
[elijah@localhost tmp]$ cat a/b/c/1.sh
echo "i can read"
[elijah@localhost tmp]$ echo 'heihei' >> a/b/c/1.sh
5、如果普通用户要执行 a/b/c/1.sh 文件,那么所属的目录文件夹至少都需要有可读可执行权限
[root@localhost tmp]# chmod +R u+r,u+x a

[elijah@localhost tmp]$ ./a/b/c/1.sh
i can read and executable

umask

创建文件的默认权限规则

创建文件夹的默认权限是从哪里来的?
	在Linux中,常用的文件的权限是666, 目录的权限是777

1、文件的权限是跟 umask 值相减,遇到奇数加一;遇到偶数则不变。
2、文件夹的权限只要跟 umask 值相减即可。

umask 值

知识储备:

Linux下有一些判断比较的逻辑,下面解释一下相关关键字的意思:

-gt是大于的意思。

-eq是等于的意思du。

-ne是不等zhi于的意思。

-ge是大于等于的意思。

-lt是小于的意思。

-le是小于等于的意思。


'''
root 用户的 uid 是 0
系统用户的 uid 值在1-999内
普通用户的 uid 在 1000 以上
'''

	&&   and		# 并且
	||   or			# 或者

文件默认权限的由来:

umask 默认值存放在 /etc/profile 目录下
	cat /etc/profile
[root@localhost etc]# cat profile
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi



# $UID -gt 199 表示 uid 大于 199
# /usr/bin/id -gn --> 查询当前组名称
# /usr/bin/id -un --> 查询当前用户名称
1、判读 root 用户的 umask
[root@localhost ~]# /usr/bin/id -gn
root
[root@localhost ~]# /usr/bin/id -un
root
[root@localhost ~]# id root
uid=0(root) gid=0(root) groups=0(root)

root 用户不符合 [ $UID -gt 199 ] 条件 --> umask 是 022

2、判断 elijah 用户的 umask
[elijah@localhost ~]$ /usr/bin/id -gn
elijah
[elijah@localhost ~]$ /usr/bin/id -un
elijah
[elijah@localhost ~]$ id elijah
uid=1000(elijah) gid=1000(elijah) groups=1000(elijah)

elijah 用户符合 [ $UID -gt 199 ] 和 [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ] 条件 --> umask 是 022

	root : umask 022		# root 用户
	elijah : umask 002		 # 普通用户

elijah 创建的文件文件夹

elijah 创建的文件文件夹:
	默认文件权限:664
	默认的文件夹权限:775
[elijah@localhost ~]$ touch 1.txt
[elijah@localhost ~]$ ll
total 0
-rw-rw-r-- 1 elijah elijah 0 Dec 15 21:11 1.txt

[elijah@localhost ~]$ mkdir aaa
[elijah@localhost ~]$ ll
total 0
-rw-rw-r-- 1 elijah elijah 0 Dec 15 21:11 1.txt
drwxrwxr-x 2 elijah elijah 6 Dec 15 21:16 aaa

root 创建的文件文件夹

root 创建的文件文件夹:
	默认文件权限:644
	默认的文件夹权限:755

[root@localhost ~]# touch 1.txt
[root@localhost ~]# ll
total 12
-rw-r--r--  1 root root    0 Dec 15 21:14 1.txt

[root@localhost ~]# mkdir aaa
[root@localhost ~]# ll
total 12
-rw-r--r--  1 root root    0 Dec 15 21:14 1.txt
drwxr-xr-x  2 root root    6 Dec 15 21:15 aaa

二、文件时间信息

stat 查看文件原始时间

[root@localhost ~]# stat 1.txt
  File: ‘1.txt’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 135050311   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-15 21:14:54.654308113 +0800
Modify: 2021-12-15 21:14:54.654308113 +0800
Change: 2021-12-15 21:14:54.654308113 +0800
 Birth: -

文件访问时间

[root@localhost ~]# cat 1.txt

[root@localhost ~]# stat 1.txt
  File: ‘1.txt’
  Size: 0         	Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d	Inode: 135050311   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-15 21:41:58.204343017 +0800		< -- 访问时间
Modify: 2021-12-15 21:14:54.654308113 +0800
Change: 2021-12-15 21:14:54.654308113 +0800

文件内容被修改时间

[root@localhost ~]# echo 'i love ' >> 1.txt
[root@localhost ~]# stat 1.txt
  File: ‘1.txt’
  Size: 14        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 135085779   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-15 21:43:30.721345006 +0800
Modify: 2021-12-15 21:44:33.174346349 +0800			< -- 文件内容被修改时间
Change: 2021-12-15 21:44:33.174346349 +0800			< -- 属性被修改时间

文件属性(权限)被修改时间

[root@localhost ~]# chmod u-w 1.txt
[root@localhost ~]# stat 1.txt
  File: ‘1.txt’
  Size: 14        	Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 135085779   Links: 1
Access: (0444/-r--r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-15 21:43:30.721345006 +0800
Modify: 2021-12-15 21:44:33.174346349 +0800
Change: 2021-12-15 21:45:17.128347294 +0800			< -- 属性被修改时间

三、系统优化

whoami	: 当前窗口登录的用户
who		:当前用户登录系统的终端

推荐阅读