Linux之权限管理

本文详细介绍了Linux系统中的权限管理,包括UGO基本权限、文件权限设置、属主属组的变更、chmod与chown的使用,以及ACL高级权限设置。内容涵盖了文件的读、写、执行权限,目录权限的意义,umask对新建文件和目录权限的影响,以及suid、sgid和sticky位的使用。还探讨了如何通过setfacl进行ACL权限设置,确保特定用户对指定目录及其内容的访问权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件权限

基本权限 UGO

文件权限设置: 可以赋于某个用户或组 能够以何种方式 访问某个文件

文件权限管理之: UGO设置基本权限(r、w、x)
在这里插入图片描述

权限对象:

属主: u

属组: g

其他人: o

基本权限类型:

读:r 4

写:w 2

执行: x 1

===设置权限

更改文件的属主、属组

=chown:

[root@xiaochen ~]# chown alice.hr file1 //改属主、属组

[root@xiaochen ~]# chown alice file1 //只改属主

[root@xiaochen ~]# chown .hr file1 //只改属组

[root@xiaochen ~]# chown -R alice.hr dir1

=chgrp:

[root@xiaochen ~]# chgrp it file1 //改文件属组

[root@xiaochen ~]# chgrp -R it dir1 //改文件属组

更改权限

使用符号

                               对象                 赋值符        权限类型

                                 u                       +                      r

chmod g - w file1

                                 o                      =                       x

                                  a

[root@xiaochen ~]# chmod u+x file1 //属主增加执行

[root@xiaochen ~]# chmod a=rwx file1 //所有人等于读写执行

[root@xiaochen ~]# chmod a=- file1 //所有人没有权限

[root@xiaochen ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读

[root@xiaochen ~]# ll file1 //以长模式方式查看文件权限

-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果

使用数字

[root@xiaochen ~]# chmod 644 file1

[root@xiaochen ~]# ll file1

-rw-r–r-- 1 alice it 17 10-25 16:45 file1

UGO权限示例

===设置权限示例

针对hr部门的访问目录设置权限,要求如下:

boss用户和hr组的员工可以读、写、执行

其他用户没有任何权限

重要: r、w、x权限对文件和目录的意义

对于文件:

r----------读 cat head tail less more (root用户不受r权限的限制)

w----------写 vim > >> (root用户不受w权限的限制)

x----------执行 ./ 绝对路径 普通执行(rx) 管理员(x)

对于目录: (root用户均不受权限的限制)

r----------ls r 只能读文件名 r-x 文件详细信息

w----------touch rm -wx 创建文件 rwx 删除所有文件

x----------cd

示例

示例1: 对文件的影响

[root@xiaochen ~]# mkdir /dir10

[root@xiaochen ~]# touch /dir10/file1

[root@xiaochen ~]# chmod 777 /dir10/file1

[root@xiaochen ~]# ll -d /dir10/

drwxr-xr-x. 2 root root 4096 3月 11 18:37 /dir10/

[root@xiaochen ~]# ll /dir10/file1

-rwxrwxrwx. 1 777 root 0 3月 11 18:37 /dir10/file1

[alice@xiaochen ~]$ cat /dir10/file1

[alice@xiaochen ~]$ rm -rf /dir10/file1

rm: 无法删除"/dir10/file1": 权限不够

想要删除目录下的文件就必须要对文件所在的目录有写的权限。

示例2: 对目录有w权限

[root@xiaochen ~]# chmod 777 /dir10/

[root@xiaochen ~]# chmod 000 /dir10/file1

[root@xiaochen ~]# ll -d /dir10/

drwxrwxrwx. 2 root root 4096 3月 11 18:37 /dir10/

[root@xiaochen ~]# ll /dir10/file1

----------. 1 root root 0 3月 11 18:37 /dir10/file1

[alice@xiaochen ~]$ cat /dir10/file1

cat: /dir10/file1: 权限不够

[alice@xiaochen ~]$ rm -rf /dir10/file1

[alice@xiaochen ~]$ touch /dir10/file2

如果对上一级目录有写的权限,那么是可以在目录下新建和删除文件的,但是,如果对文件本身没

有什么权限,那么就没有办法看这个文件。如果对文件本身有r的权限,那么就可以看文件了。

基本权限ACL

UGO设置基本权限: 只能一个用户,一个组和其他人

ACL 设置基本权限: r,w,x

=ACL基本用法=

设置:

[root@xiaochen ~]# touch /home/test.txt

[root@xiaochen ~]# ll /home/test.txt

-rw-r–r-- 1 root root 0 10-26 13:59 /home/test.txt

[root@xiaochen ~]# getfacl /home/test.txt

[root@xiaochen ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限

[root@xiaochen ~]# setfacl -m u:jack:-,g:hr:rw /home/test.txt //增加用户jack权限

[root@xiaochen ~]# setfacl -m o::rw /home/test.txt

查看/删除:

[root@xiaochen ~]# ll /home/test.txt

-rw-rw-r–+ 1 root root 0 10-26 13:59 /home/test.txt

[root@xiaochen ~]# getfacl /home/test.txt

[root@xiaochen ~]# setfacl -m g:hr:r /home/test.txt

[root@xiaochen ~]# setfacl -x g:hr /home/test.txt //删除组hr的acl权限

[root@xiaochen ~]# setfacl -b /home/test.txt //删除所有acl权限

=查看帮助=

[root@xiaochen ~]# man setfacl

/EXAMPLES

[root@xiaochen ~]# getfacl file1 |setfacl --set-file=- file2 //复制file1的ACL权限给file2

=ACL高级用法=

mask决定最高权限:

用于临时降低用户或组(除属主和其他人)的权限

建议:为了方便管理文件权限,其他人的权限置为空

示例:mask值影响用户权限的举例

[root@xiaochen ~]# touch /home/file1

[root@xiaochen ~]# setfacl -m u:gougou:rw /home/file1

[root@xiaochen ~]# getfacl /home/file1

getfacl: Removing leading ‘/’ from absolute path names

file: home/file1

owner: root

group: root

user::rw-

user:gougou:rw-

group::r–

mask::rw-

other::r–

[root@xiaochen ~]# setfacl -m m::r /home/file1

[root@xiaochen ~]# getfacl /home/file1

getfacl: Removing leading ‘/’ from absolute path names

file: home/file1

owner: root

group: root

user::rw-

user:gougou:rw- #effective:r–

group::r–

mask::r–

other::r–

[root@xiaochen ~]# su - gougou

[gougou@xiaochen ~]$ cat /home/file1

[gougou@xiaochen ~]$ vim /home/file1

使用vim打开文件之后无法对文件进行修改

[root@xiaochen ~]# setfacl -m o::rwx /home/file1

[root@xiaochen ~]# getfacl /home/file1

getfacl: Removing leading ‘/’ from absolute path names

file: home/file1

owner: root

group: root

user::—

user:alice:rw-

group::—

mask::rw-

other::rwx

[root@xiaochen ~]# setfacl -m m::r /home/file1

[root@xiaochen ~]# getfacl /home/file1

getfacl: Removing leading ‘/’ from absolute path names

file: home/file1

owner: root

group: root

user::—

user:alice:rw- #effective:r-- 有效权限:r

group::—

mask::r–

other::rwx

[alice@xiaochen ~]$ cat /home/file1

333

[alice@xiaochen ~]$ vim /home/file1

使用vim打开文件之后无法对文件进行修改,因为alice用户受到的mask值的影响,有效权限只有r

[root@xiaochen ~]# setfacl -m m::- /home/file1

[root@xiaochen ~]# getfacl /home/file1

getfacl: Removing leading ‘/’ from absolute path names

file: home/file1

owner: root

group: root

user::—

user:alice:rw- #effective:—

group::—

mask::—

other::rwx

[root@xiaochen ~]# su - alice

[alice@xiaochen ~]$ cat /home/file1

333

ddd

[alice@xiaochen ~]$ vim /home/file1

使用vim打开文件的时候就可以对文件的内容进行修改了,因为mask值为空,alice会继承other的身份

总结:

mask的值不为空的时候,单独设置用户或者是组的acl权限会受到mask值的影响产生有效权限,不包

括user(属主)和other(其他人),但是如果将mask的值设置为空,单独设置过acl权限的用户会继承other的

权限,如果想要临时的将设置过acl权限的所有用户的权限降为最低,需要将other的权限设置为空,同时

将mask的值设置为空。

umask

文件权限的掩码:umask [权限]

umask默认值:root用户的是022,普通用户的是002。

umask的功能:会影响到用户创建的新文件、目录的最终权限。

实验测试:用mkdir在根目录下创建/dir目录,用touch创建文件/file,然后分别查看/dir、/file的详细信息,最后删除文件和目录。

umask 查看当前的umask值,它表示要去掉的权限

mkdir /dir

touch /file

ls -ld /dir 查看/dir目录的详细信息,也可以ll -d /dir

ls -l /file 查看/file文件的详细信息,也可以ll /file

rm -rf /dir /file

文件、目录最终权限的算法:满权限-umask权限=最终权限

目录的默认满权限:777 例如:777-022=755 777-031=746 rwx rwx rwx

文件的默认满权限:666 例如:666-022=644 666-031=646 rw- rw- rw-

满权限 777 666 666
umask 022 022 031
计算过程 777-022 rwx rwx rwx — -w- -w- 666-022 rw- rw- rw- — -w- -w- 666-031 rw- rw- rw- 满权限 — -wx --x 要去掉的权umask限
权限结果 rwx r-x r-x 即755 rw- r-- r-- 即644 rw- r-- rw- 即646

在shell进程中创建文件

[root@xiaochen ~]# umask //查看当前用户的umask权限

0022

[root@xiaochen ~]# touch file800

[root@xiaochen ~]# mkdir dir800

[root@xiaochen ~]# ll -d dir800 file800

drwxr-xr-x. 2 root root 4096 3月 11 19:40 dir800

-rw-r–r--. 1 root root 0 3月 11 19:40 file800

root: 文件 644 umask: 0022

目录 755

普通用户: 文件 664 umask: 0002

目录 775

修改shell umask值(临时)

[root@xiaochen ~]# umask 000

[root@xiaochen ~]# mkdir dir900

[root@xiaochen ~]# touch file900

[root@xiaochen ~]# ll -d dir900 file900

drwxrwxrwx. 2 root root 4096 3月 11 19:44 dir900

-rw-rw-rw-. 1 root root 0 3月 11 19:44 file900

修改shell umask值(永久)

[root@xiaochen ~]# vim /etc/profile

if [ $UID -gt 199 ] && [ “id -gn” = “id -un” ]; then

umask 002

else

umask 022

fi

[root@xiaochen ~]# source /etc/profile //立即在当前shell中生效

通过umask决定新建用户HOME目录的权限

[root@xiaochen ~]# grep -i umask /etc/login.defs

UMASK 077

[root@xiaochen ~]# useradd gougou

[root@xiaochen ~]# ll -d /home/gougou/

drwx------. 4 gougou gougou 4096 3月 11 19:50 /home/gougou/

[root@xiaochen ~]# vim /etc/login.defs

UMASK 000

[root@xiaochen ~]# useradd yangyang

[root@xiaochen ~]# ll -d /home/yangyang/

drwxrwxrwx. 4 yangyang yangyang 4096 3月 11 19:53 /home/yangyang/

例如vsftpd进程 /etc/vsftpd/vsftpd.conf

local_umask=022

小知识:subshell

[root@localhost ~]# cd /home/;ls

alice centos tom

[root@localhost home]# pwd

/home

[root@localhost home]# cd

[root@localhost ~]# (cd /home/;ls)

alice centos tom

[root@localhost ~]# pwd

/root

[root@xiaochen ~]# umask 077; touch file60 //当前shell生效

[root@xiaochen ~]# (umask 077; touch file70) //()表示在子shell生效 subshell

[root@xiaochen ~]# ll file70

-rw------- 1 root root 0 10-26 14:31 file70

[root@xiaochen ~]#

[root@xiaochen ~]# umask

0022

总结:

当umask值是022时:

目录权限是755时,目录的主人对目录有rwx可读可写可执行权限,同组的人对目录有rx可读可执行权限,其他人对目录有rx可读可执行权限。

文件权限是644时,文件的主人对文件有rw可读可写权限,同组的人对文件有r可读权限,其他人对文件有r可读权限。

当umask为002时:

目录权限是775时,目录的主人对目录有rwx可读可写可执行权限,同组的人对目录有rwx可读可写可执行权限,其他人对目录有rx可读可执行权限。

文件权限是

664

时,文件的主人对文件有

rw

可读可写权限,同组的人对文件有

rw

可读可写权限,其他人对文件有

r

可读权限。

default: 继承(默认)

要求: 希望alice能够对/tmp/dir100下的文件以及以后在/tmp/dir100下新建的文件有读、写、执行权限

[root@xiaochen tmp]# mkdir dir100

[root@xiaochen tmp]# touch dir100/file1 dir100/file2

思路:

步骤一: 赋予alice对/tmp/dir100以及目录下以存在的文件和文件夹读、写、执行权限

[root@xiaochen ~]# setfacl -R -m u:alice:rwx /tmp/dir100

[root@xiaochen tmp]# getfacl dir100/file1

file: dir100/file1

owner: root

group: root

user::rw-

user:alice:rwx

group::r–

mask::rwx

other::r–

[root@xiaochen tmp]# getfacl dir100/file2

file: dir100/file2

owner: root

group: root

user::rw-

user:alice:rwx

group::r–

mask::rwx

other::r–

[root@localhost tmp]# touch dir100/file3

[root@localhost tmp]# getfacl dir100/file3

file: dir100/file3

owner: root

group: root

user::rw-

group::r–

other::r–

步骤二: 赋予alice对以后在/tmp/dir100下新建的文件有读、写、执行权限 (使alice的权限继承)

[root@xiaochen ~]# setfacl -m d:u:alice:rwx /tmp/dir100

[root@localhost tmp]# getfacl dir100/

file: dir100/

owner: root

group: root

user::rwx

user:alice:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:alice:rwx

default:group::r-x

default:mask :rwx

default:other::r-x

[root@localhost tmp]# touch dir100/file4

[root@localhost tmp]# getfacl dir100/file4

file: dir100/file4

owner: root

group: root

user::rw-

user:alice:rwx #effective:rw-

group::r-x #effective:r–

mask::rw-

other::r–

注意:文件的x权限不能随便给的,所以系统会自动的将文件的x权限

减掉

[root@localhost tmp]# mkdir dir100/dir200

[root@localhost tmp]# getfacl dir100/dir200/

file: dir100/dir200/

owner: root

group: root

user::rwx

user:alice:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:alice:rwx

default:group::r-x

default:mask :rwx

default:other::r-x

[root@localhost tmp]# mkdir dir100/dir200/dir300

[root@localhost tmp]# getfacl dir100/dir200/dir300

file: dir100/dir200/dir300

owner: root

group: root

user::rwx

user:alice:rwx

group::r-x

mask::rwx

other::r-x

default:user::rwx

default:user:alice:rwx

default:group::r-x

default:mask rwx

default:other::r-x

[root@localhost tmp]# touch dir100/dir200/dir300/file1

[root@localhost tmp]# getfacl dir100/dir200/dir300/file1

file: dir100/dir200/dir300/file1

owner: root

group: root

user::rw-

user:alice:rwx #effective:rw-

group::r-x #effective:r–

mask::rw-

other::r–

问题1:

[root@xiaochen ~]# ll /root/install.log

-rw-r–r--. 1 root root 46571 6月 1 23:37 /root/install.log

[alice@xiaochen ~]$ cat /root/install.log

cat: /root/install.log: 权限不够

问题2: alice能删除/下的任何文件吗?

[root@xiaochen ~]# chmod 777 /

[root@xiaochen ~]# ll -d /

drwxrwxrwx. 27 root root 4096 6月 4 11:32 /

[alice@xiaochen ~]$ rm -rf /etc

高级权限 suid,sgid,sticky

问题1: 为什么会失败!

[root@xiaochen ~]# ll /root/install.log

-rw-r–r--. 1 root root 46571 6月 1 23:37 /root/install.log

[alice@xiaochen ~]$ cat /root/install.log

cat: /root/install.log: 权限不够

分析:

alice /bin/cat /root/install.log

普通用户alice将/bin/cat程序运行起来产生一个进程,而这个进程的拥有者是alice,alice对于/root目

录没有权限,所以就无法查看install.log文件,那运行这个进程的用户我们无法改变,想看的文件也无法改

变,但是我们可以改变进程的所有者,想办法让进程的所有者变成root就可以了。

[root@xiaochen ~]# ps aux |grep passwd

root 7135 0.0 0.0 103256 856 pts/2 S+ 17:24 0:00 grep --color passwd

[alice@xiaochen ~]$ passwd

更改用户 alice 的密码 。

为 alice 更改 STRESS 密码。

(当前)UNIX 密码:

[root@xiaochen ~]# ps aux |grep passwd

root 9379 0.1 0.0 168188 2140 pts/1 S+ 19:28 0:00 passwd

root 9382 0.0 0.0 103256 860 pts/2 S+ 19:28 0:00 grep --color passwd

[root@xiaochen ~]# ll /usr/bin/passwd

-rwsr-xr-x. 1 root root 30768 2月 22 2012 /usr/bin/passwd

l /usr/bin/passwd

高级权限的类型

suid 4

sgid 2

sticky 1 粘滞位

设置特殊权限

a、字符

chmod u+s file

chmod g+s dir

chmod o+t dir

b、数字

chmod 4777 file

chmod 7777 file

chmod 2770 dir

chmod 3770 dir

suid 普通用户通过suid提权

作用:任何用户在执行拥有suid权限的命令时,都以该命令的拥有者的身份执行(必须作用于二进制的可执行

文件)

[root@xiaochen ~]# chmod u+s /bin/cat

[root@xiaochen ~]# chmod u+s /bin/rm

[alice@xiaochen ~]$ cat /root/install.log

sticky 用户只能删除自己的文件 <针对目录>

作用:任何用户在拥有t权限的目录下,只能删除自己的文件

[root@xiaochen ~]# mkdir /home/dir1

[root@xiaochen ~]# chmod 777 /home/dir1

测试:user1在/home/dir1建立文件, user2尝试删除!

[root@xiaochen ~]# chmod o+t /home/dir1

测试:user1在/home/dir1建立文件, user2再尝试删除!

[root@xiaochen ~]# ll -d /home/dir1

rwxrwxrwt 2 root root 4096 09-02 02:26 /home/dir1

以下两种情况o+t的权限不生效:

1.root用户和文件夹的拥有者不受sticky权限的限制

2.针对与rm命令设置过u+s的权限

sgid 新建文件继承目录属组 <针对目录>

作用:任何用户在拥有sgid的目录下新建的文件,都要继承该目录的属组

[root@xiaochen ~]# mkdir /home/hr

[root@xiaochen ~]# chgrp -R hr /home/hr/

[root@xiaochen ~]# chmod g+s /home/hr

[root@xiaochen ~]# ll -d /home/hr/

drwxr-sr-x. 2 root hr 4096 Dec 5 16:03 /home/hr/

[root@xiaochen ~]# touch /home/hr/file9

[root@xiaochen ~]# ll /home/hr/

-rw-r–r--. 1 root hr 0 Dec 5 16:03 file9

小知识:注意以下目录的正确权限,否则会导致程序不能正常运行

[root@wangcy ~]# ll -d /tmp /var/tmp/

drwxrwxrwt 14 root root 4096 07-26 10:15 /tmp

drwxrwxrwt 2 root root 4096 07-24 19:02 /var/tmp/

/tmp目录是全局可写的,这就意味这每个进程都可以写,所以需要加限制,否则进程之间会受到影响,一

个进程产生的文件可能别另外一个进程删掉。

文件属性 chattr[扩展]

设置文件属性,针对所有用户,包括root,属性凌驾于权限之上

[root@xiaochen ~]# touch file100 file200

[root@xiaochen ~]# lsattr file100 file200

--------------- file100

--------------- file200

[root@xiaochen ~]# man chattr

[root@xiaochen ~]# chattr +a file100 //仅仅允许追加内容

[root@xiaochen ~]# chattr +i file200 //不可变的,不允许追加,覆盖和删除,包括移动

[root@xiaochen ~]# lsattr file100 file200 file300

-----a--------- file100

----i---------- file200

a属性测试

[root@xiaochen ~]# echo 111 > file100 //以覆盖的方式写入

bash: file100: Operation not permitted

[root@xiaochen ~]# rm -rf file100

rm: cannot remove `file100’: Operation not permitted

[root@xiaochen ~]# echo 111 >> file100 //以追加的方式写入,例如日志文件

i 属性测试

[root@xiaochen ~]# echo 111 > file200

bash: file200: Permission denied

[root@instructor ~]# echo 111 >> file200

bash: file200: Permission denied

[root@xiaochen ~]# rm -rf file200

rm: cannot remove `file200’: Operation not permitted

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值