Linux——文件权限理解、切换用户

Linux下有两种用户:超级用户、普通用户。

区别:
超级用户:可以在Linux下做任何事情,不受系统限制。
普通用户:只能做有限的事,受系统限制。
切换用户:
su [用户名]
1.普通用户切换超级用户

[zjp@VM-0-4-centos ~]$ su root
Password:                  //普通用户切换到超级用户需要输入密码

2.超级用户切换普通用户

[root@VM-0-4-centos zjp]# su zjp  //超级用户切换到普通用户不需要输入密码

1.Linux权限管理

在命令行中输入 ls -l 可以查看文件详细信息

[zjp@VM-0-4-centos test]$ ls -l
total 4
drwxrwxr-x 2 zjp zjp 4096 Feb  2 07:21 file
-rw-rw-r-- 1 zjp zjp    0 Feb  2 07:21 test

在这里插入图片描述

1.文件的访问者

1.user :文件拥有者,即创建文件的人
2.group:文件的拥有者所在的用户组,一般来说拥有者和所处组是同一个人
3.other:其它用户。
以上的三类人都是普通用户,因为文件的限制只针对普通用户,超级用户不受限制。

2.文件类型

文件类型 文件类型名
- 普通文件
d 文件夹
l 软连接(类似windows快捷方式)
d 块设备文件(硬盘、光驱)
p 管道文件
c 字符设备文件(屏幕等串口设备)
s 套接口文件

一般常见的就是前两个普通文件和文件夹。

3.文件的权限

文件权限:读、写、执行

权限 普通文件(-) 文件夹(d)
r:读权限 读取文件的内容 查看文件夹里的文件
w:写权限 修改文件的内容 删除或移动文件夹内的文件
x:执行权限 打开或执行文件 进入文件夹

注意:对于文件夹来说如果用户没有x权限可以认为不能进入该文件夹内,那当然就没有r(查看文件夹内文件)权限和w(删除或移动文件夹内文件)权限。总之,没有了x(执行权限)就相当于没有任何权限了,但是这仅仅只针对文件夹。

4.修改文件权限

chmod 可以修改文件权限
chmod 名(u,g,o)±权限(rwx) 文件名

[zjp@VM-0-4-centos file]$ ll
total 0
-rw-rw-r-- 1 zjp zjp 0 Feb  2 08:27 test //此时other只有r权限
[zjp@VM-0-4-centos file]$ chmod o+w test // 给test文件的other 增加 w 权限
[zjp@VM-0-4-centos file]$ ll
total 0
-rw-rw-rw- 1 zjp zjp 0 Feb  2 08:27 test //此时other有rw权限

5.修改文件拥有者或所处组

chown: 修改拥有者

[zjp@VM-0-4-centos file]$ sudo chown root test //sudo是提高成root权限
[sudo] password for zjp: 
[zjp@VM-0-4-centos file]$ ll
total 0
-rw-rw-rw- 1 root zjp 0 Feb  2 08:27 test

chgrp: 修改所处组

[zjp@VM-0-4-centos file]$ sudo chgrp root test
[zjp@VM-0-4-centos file]$ ll
total 0
-rw-rw-rw- 1 root root 0 Feb  2 08:27 test

5.粘滞位

Linux下有一个奇怪的现象,对于一个文件夹来讲用户对文件夹内的文件没有rw权限,但是对该文件夹有w权限。那就意味着用户不能查看修改文件夹内的文件,确可以删除文件。那这就不符合常理了。

[root@VM-0-4-centos test]# ls -l
total 4
drwxrwxr-x 2 root root 4096 Feb  2 07:21 file 
//对于该文件夹用户root有rwx权限,zjp 有r-x权限
[root@VM-0-4-centos test]# cd ./file
[root@VM-0-4-centos file]# touch test
[root@VM-0-4-centos file]# ls -l
total 0
-rw-r--r-- 1 root root 0 Feb  2 08:19 test
//使用超级用户的身份创建一个普通文件,可以看到其他用户只有r权限。
[root@VM-0-4-centos file]# su zjp //切换到普通用户
[zjp@VM-0-4-centos file]$ ls -l
total 0
-rw-r--r-- 1 root root 0 Feb  2 08:19 test
[zjp@VM-0-4-centos file]$ rm test 
rm: remove write-protected regular empty file ‘test’? y  
[zjp@VM-0-4-centos file]$ ls -l
total 0   
 // 可以删除该文件。刚刚这个文件的是root创建的,zjp是没有w权限的,但是zjp却可以删除改文件。那就变成了我不能修改你,但是我能删除,就很不符合常理了。

为解决这一矛盾就引入粘滞位这一概念。
chmod +t file
文件夹被设置为粘滞位后:
1.文件夹内的文件能被root删除
2.文件夹内的文件能被文件拥有者删除
3.文件夹的拥有者也可以删除

[zjp@VM-0-4-centos test]$ chmod +t file
[zjp@VM-0-4-centos test]$ ll
total 4
drwxr-xr-t 2 root root 4096 Feb  2 08:42 file //文件权限后面多了一个 t 

//以普通用户的身份进入文件夹,再删除文件时发现权限被拒绝了
-rw-r--r-- 1 root root 0 Feb  2 08:43 test
[zjp@VM-0-4-centos file]$ rm -f test
rm: cannot remove ‘test’: Permission denied

猜你喜欢

转载自blog.csdn.net/weixin_50168448/article/details/113533940