(一)基本权限

文件系统权限
1、权限的作用:保证系统安全性,称为普通权限或文件系统权限,作用是保护文件,让有权限的用户可以访问,否则不能访问,linux文件系统权限,主要设置在文件上,限制对象是用户。
注意:root用户不受文件系统权限控制
[root@localhost ~]# ls -l aa.txt
-rw-r--r--. 1 root root 65 5月 16 14:18 aa.txt
权限位: rwx | rwx | rwx 拥有者 所属组 其他人
user group other u g o

id Lyon uid=1000(Lyon) gid=1000(Lyon) 组=1000(Lyon)
ls -l /tmp/tt.txt
-rw-r--r--. 1 root root 4 5月 17 14:06 tt.txt
uid=0
------>拥有者
---------->所属组(附加组)
---------->其他人
第一个三位
第二个三位
第三个三位

file:
r -------->read cat head tail more.....
w -------->write vim > >> gedit
x ---------> exec ./ 后者绝对路径执行
directory:
r ------->read ls r-x 详细信息
w ----->touch rm -wx 创建删除
rm -rf * rwx 目录/文件 or 目录 /root/abc.txt(创建时需要目录同时拥有x权限)
x ------->cd 文件权限修改
chmod change mode(字符修改方法)
u g o a , + - = ,
r w x
chmod u+x aa.txt
chmod u-rw aa.txt
chmod u+r,g+w,o+x aa.txt
chmod ug+rw aa.txt
chmod ugo+rwx aa.txt
chmod a-rwx aa.txt
chmod o=--- aa.txt
chmod u=r aa.txt
chmod u=rw aa.txt
chmod u=r,g=rw,o=x aa.txt
chmod ugo=rwx aa.txt
chmod a=r aa.txt

查看以下目录或者文件权限:
/etc/passwd rw-r--r-- 644
/etc/shadow --------- 000
/tmp rwxrwxrwt 1777
/root r-xr-x--- 550
/home/Lyon rwx------ 700
/ r-xr-xr-x 555
/etc rwxr-xr-x 755
root用户创建文件默认权限 rw-r--r-- 644
root用户创建目录默认权限 rwxr-xr-x 755
普通用户创建文件默认权限 rw-rw-r-- 664
普通用户创建目录默认权限 rwxrwxr-x 775

数字表示权限:
r -------- 4
w -------- 2
x -------- 1
rw 4+2 6
wx 2+1 3
rwx 4+2+1 =7
321 -wx-w---x
456 r--r-xrw-
chmod 数字表示法 文件名
[root@localhost opt]# chmod 644 /passwd
[root@localhost opt]# ll /passwd
-rw-r--r-- 1 root root 290 10月 28 09:20 /passwd
umask 权限反掩码
0
umask值可以规定linux默认目录及文件权限,计算方式,用目录或文件最大权限和umask值取反。
linux目录默认权限 最大777 文件最大权限666,umask值默认是0022
770
666-022=644
777-022=755
umask 查看
umask 0002 修改
666-002=664
777-002=7750
0
umask 0023
666-023=643
ll kk.txt
-rw-r--r--. 1 root root 0 5月 17 15:30 kk.txt

023
000 010 011
111 101 100 --取反
754
666
110 110 110
111 101 100
110 100 100
6 4 4

创建文件 642 umask=?
666
110 110 110
642
110 100 010
110 110 110
111 101 011 ----> 000 010 100 ---> 024
110 100 010
umask=0024
chmod -R 777 dirname/
改:
/tmp/test.txt rw-r--r-- robin root
robin Lyon ---> other
chown Lyon /tmp/test.txt
chgrp upup /tmp/test.txt

chown 命令可以修改所有者和所属组,作用于超级用户,普通用户不可用 chgrp 命令只可以修改所属组,作用于普通用户
chown .upup /tmp/test.txt
chown :upup /tmp/test.txt
chown Lyon.upup /tmp/test.txt
chown Lyon.upup /tmp/dir/
chown -R Lyon.upup /tmp/dir
chmod -R 777 /tmp/dir
-R 递归
时间设置
[root@localhost tmp]# date +%R
10:24
[root@localhost tmp]# date +%Y
2020
[root@localhost tmp]# date +%Y-%M
2020-25
[root@localhost tmp]# date +%Y-%m
2020-10
[root@localhost tmp]# date +%Y-%m-%d
2020-10-19
[root@localhost tmp]# date +"%Y-%m-%d %h"
2020-10-19 10月
[root@localhost tmp]#
[root@localhost tmp]# date +"%Y-%m-%d %h"
2020-10-19 10月
[root@localhost tmp]# date +"%Y-%m-%d %H"
2020-10-19 10
[root@localhost tmp]# date +"%Y-%m-%d %H:%M"
2020-10-19 10:25
[root@localhost tmp]# date +"%Y-%m-%d %H:%M:%s"
2020-10-19 10:25:1603074351
[root@localhost tmp]# date +"%Y-%m-%d %H:%M:%S"
2020-10-19 10:25:53
[root@localhost tmp]# date +%s
1603074361

技巧:
touch date +%Y-%m-%d
touch $(date +%Y-%m-%d).txt
timedatectl 查看时间信息
timedatectl set-timezone Asia/Shanghai 修改时区

时间:
[root@localhost tmp]# hwclock -s 从硬件时间写到系统
[root@localhost tmp]# date
2017年 05月 17日 星期三 23:55:07 CST
[root@localhost tmp]# date -s '20170517 16:02:40'
2017年 05月 17日 星期三 16:02:40 CST
[root@localhost tmp]# date
2017年 05月 17日 星期三 16:02:41 CST
[root@localhost tmp]# hwclock -w 从系统写到硬件时间
[root@localhost tmp]# hwclock
2017年05月17日 星期三 16时02分52秒 -0.211094 秒

(二)特殊权限
1.suid ( set user id )
作用:任何用户在运行拥有suid权限的命令(二进制可执行文件)时,都以该命令拥有者的身份执行
作用位置:user
作用对象:文件(二进制可执行文件)
chmod u+s 命令
2.sgid (set group id)
作用:任何用户在拥有sgid权限的目录下,创建的文件都要继承父目录的组
作用位置:group
作用对象:目录
chmod g+s directory
d g+s director

高级权限数字表示
suid -----------> 4
sgid -----------> 2
stickey---------> 1

[root@host1 opt]# chmod a=rwx,o+t dir1
[root@host1 opt]# ll -d dir1
drwxrwxrwt 2 root robin 32 2月 24 22:47 dir1
[root@host1 opt]# chmod 0755 dir1
[root@host1 opt]# ll -d dir1
drwxr-xr-x 2 root robin 32 2月 24 22:47 dir1
[root@host1 opt]# chmod 1777 dir1
[root@host1 opt]# ll -d dir1
drwxrwxrwt 2 root robin 32 2月 24 22:47 dir1

4777
2777
1777
3777

高级权限大小写区分是否具有x权限
[root@localhost tmp]# chmod u+s aa.txt
[root@localhost tmp]# ll aa.txt
-rwSr--r-- 1 root root 0 5月 18 11:51 aa.txt
[root@localhost tmp]# chmod +x aa.txt
[root@localhost tmp]# ll aa.txt
-rwsr-xr-x 1 root root 0 5月 18 11:51 aa.txt

[root@localhost tmp]# ll -d test/
drwxrwsrwt 2 root root 6 5月 18 11:52 test/
[root@localhost tmp]# chmod g-x,o-x test/
[root@localhost tmp]# ll -d test/
drwxrwSrwT 2 root root 6 5月 18 11:52 test/

(三)facl

linux中权限
1、文件系统权限 rwx
2、特殊权限 suid sgid sbit
3、facl 文件访问控制列表
4、隐藏权限 facl 文件访问控制列表Linux的权限非常重要,我们之前所有的集中权限,但是并不能只针对一个用户或者一个组进行单独设置,而ACL权限可以帮助我们实现这个功能,比如说有一个文件的所有者和所有者组都是a,这个文件权限是660,我可以让b这个用户可以对文件进行读写的操作,而b这个用户并不属于a组的成员。下面我们来看下如何使用ACL权限的设置和查看要使用acl,首先要确认文件系统是否支持了ACL功能,可以通过Default mount options的acl字样来判定是否支持,RHEL8或Centos默认都支持acl。
[root@localhost ~]# tune2fs -l /dev/sdc2 #/dev/sdc2已经被格式化成了ext4文件系统
tune2fs 1.44.6 (5-Mar-2019)
Filesystem volume name:
Last mounted on:
Filesystem UUID: 0151bbd7-de30-4066-93cf-a66f36f72113
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags: signed_directory_hash
Default mount options: user_xattr acl 此处发现有acl字样,表示支持acl功能

查看文件是否设置了acl权限
[root@localhost /]# touch /opt/test.txt
[root@localhost /]# ll /opt/test.txt
-rw-r--r--. 1 root root 0 3月 10 04:52 /opt/test.txt
权限后面的.点表示未设置acl权限
[root@localhost /]# man setfacl

里面搜索examples 找到第一个facl例子复制并修改
[root@localhost /]# useradd Lyon
[root@localhost /]# setfacl -m u:Lyon:r /opt/test.txt
[root@localhost /]# ll /opt/test.txt -rw-r--r--+ 1 root root 0 3月 10 04:52 /opt/test.txt

权限后面的.点表示已设置acl权限
acl命令管理
管理acl命令有两个setfacl用于设置管理,getfacl用于查看
setfacl -m u: Lyon: r file
命令 添加一个ACL 针对对象是单一用户 用户名称 权限 文件名

setfacl:
命令选项:
-m: 设置acl
-x:删除指定的acl
-b:删除所有acl

getfacl:
ACL权限设置的对象
u user 所有者
g group 所属组
m mask值

示例:设置一个标准的acl权限,并查看希望tom 对/opt/test.txt有读写权限
[root@localhost /]# useradd tom
[root@localhost /]# ll /opt/test.txt -rw-r--r--+ 1 root root 0 3月 10 04:52 /opt/test.txt
[root@localhost /]# setfacl -m u:tom:rw- /opt/test.txt

给tom对test.txt设置一个所有者权限是rw
[root@localhost /]# getfacl /opt/test.txt
getfacl: Removing leading '/' from absolute path names

file: opt/test.txt 文件名
owner: root 所有者
group: root 所属组
user::rw- 文件所有者原本的权限
user:lisa:r-- lisa的acl权限
user:tom:rw- tom的acl权限
group::r-- 文件组原本权限
mask::rw- 掩码
other::r-- 文件原本权限

验证:
[root@localhost opt]# su - tom
[tom@localhost ~]$ vim /opt/test.txt 发现可以进行编辑
[tom@localhost ~]$ exit 注销
[root@localhost opt]# useradd harry
[root@localhost opt]# su - harry
[harry@localhost ~]$ vim /opt/test.txt 但是harry是不可以编辑的

示例2:
针对不同的用户、组、其他人设置acl权限
1、给用户harry设置所有者权限为读写执行
2、给用户组admin设置读写权限
3、给其他人设置读写权限

给用户harry设置所有者权限为读写执行
[root@localhost opt]# useradd harry
[root@localhost opt]# setfacl -m u:harry:rwx test.txt

rwx位置也可以用数字代替 给用户组admin设置读写权限
[root@localhost opt]# setfacl -m g:admin:rw test.txt
[root@localhost opt]# getfacl test.txt
file: test.txt
owner: root
group: root
user::rw-
user:tom:rw-
user:harry:rwx
group::r--
group:admin:rw-

给组设置的acl权限为
rw
mask::rwx
other::r--
给其他人设置读写权限

[root@localhost opt]# setfacl -m o:rw test.txt
[root@localhost opt]# getfacl test.txt
file: test.txt
owner: root
group: root
user::rw-
u
other::rw-

给其他人设置了rw权限
删除指定的acl权限setfacl -x g:staff file命令 删除 组:组名 文件名
删除一个acl组权限
[root@localhost opt]# setfacl -x g:admin /opt/test.txt
[root@localhost opt]# getfacl /opt/test.txt
getfacl: Removing leading '/' from absolute path names

file: opt/test.txt
owner: root
group: root
user::rw-

user:tom:rw-
user:harry:rwx
group::r--

发现原组权限下面的acl设置的组权限已经被删除
mask::rwx other::rw-
删除一个acl所有者权限
[root@localhost opt]# setfacl -x u:tom /opt/test.txt
[root@localhost opt]# getfacl /opt/test.txt
getfacl: Removing leading '/' from absolute path names

file: opt/test.txt
owner: root
group: root user::rw-

发现原所有者权限下面的tom的acl权限已经被删除
mask::rwx
user:harry:rwx
group::r--
mask::rwx
other::rw-
其他人也是同上的操作

删除文件所有acl权限setfacl -b
[root@localhost opt]# setfacl -b /opt/test.txt
清除test.txt文件所有acl
[root@localhost opt]# setfacl -m o:r /opt/test.txt
另外设置其他人权限为r权限,恢复至初始状态
[root@localhost opt]# getfacl /opt/test.txt
经查看发现没有任何acl信息
getfacl: Removing leading '/' from absolute path names
file: opt/test.txt
owner: root
group: root
user::rw-
group::r--
other::r--
[root@localhost opt]# ll /opt/test.txt -rw-r--r--. 1 root root 7 3月 10 05:27 /opt/test.txt

ll查看后发现"+"号变回了"."
修改mask值
setfacl -m m
我们可以通过调整mask值来限制acl权限的大小范围(有效权限范围)。
比如给a用户设置了rwx权限,但是此时mask值权限为rw,最终权限取,a用户权限会和mask权限重叠部分。
a rwx
m rw
有效权限 rw

1、设置acl时会将会自动将权限添加至mask值部分,而且还会将mask值权限体现在文件所属组位置,通过ls -l可查询
[root@localhost opt]# touch test2.txt
[root@localhost opt]# ll test2.txt -rw-r--r--. 1 root root 0 3月 10 06:34 test2.txt
[root@localhost opt]# setfacl -m g:admin:rwx test2.txt

设置acl 此时权限设置为rwx
[root@localhost opt]# getfacl test2.txt
file: test2.txt
owner: root
group: root
user::rw-
group::r--
group:admin:rwx mask::rwx
更新至mask位置 other::r--
[root@localhost opt]# ll test2.txt -rw-rwxr--+ 1 root root 0 3月 10 06:34 test2.txt

同时应用在了文件所属组位置

2、设置mask值得方法,及有效权限判定规则
[root@localhost opt]# setfacl -m m:rw test2.txt
设置mask值的方法
[root@localhost opt]# getfacl test2.txt
file: test2.txt
owner: root
group: root user::rw- group::r-- group:admin:rwx
effective:rw- #最终权限取,用户权限会和mask权限重叠部分。
mask::rw- other::r--

修改文件原用户组权限
setfacl -m g::perms file
注意:如果使用chmod命令修改 已添加过acl权限的文件时,所属组位置会显示mask值,所以需要用setfacl -m g::perms命令修改原所属组权限
[root@localhost opt]# setfacl -m g::rw test.txt
给test.txt所属组设置了rw权限
[root@localhost opt]# ll test.txt -rw-rwxr--+ 1 root root 7 3月 10 05:27 test.txt
[root@localhost opt]# getfacl test.txt
file: test.txt
owner: root
group: root
user::rw- user:tom:rwx group::rw-

文件原组权限已被修改 mask::rwx other::r-- 使用setfacl修改组权限的原因:
[root@localhost opt]# ll test2.txt -rw-rwxr--+ 1 root root 0 4月 11 22:23 test2.txt
[root@localhost opt]# chmod 644 test2.txt

如果文件设置acl权限,使用chmod修改test2.txt权限时更改了所属组位置权限,实际修改的是acl的mask值
[root@localhost opt]# ll test2.txt -rw-r--r--+ 1 root root 0 4月 11 22:23 test2.txt
[root@localhost opt]# getfacl test2.txt
file: test2.txt
owner: root

group: root user::rw- group::rw-
effective:r-- group:admin:rwx
effective:r--
mask::r--
other::r--

递归设置和默认acl权限
递归-R: 查看、设置、删除均可生效
default:使目录中的子文件和目录都继承父目录的权限
递归 -R,查看、设置、删除
[root@localhost opt]# mkdir dir1
[root@localhost opt]# setfacl -m u:tom:rwx dir1
[root@localhost opt]# getfacl dir1 # file: dir1
owner: root
group: root user::rwx user:tom:rwx

设置acl group::r-x mask::rwx other::r-x
[root@localhost opt]# touch dir1/file{1,2}
[root@localhost opt]# getfacl dir1/file1
file: dir1/file1
owner: root
group: root user::rw- 未设置
group::r--
other::r--

[root@localhost opt]# getfacl dir1/file2
file: dir1/file2
owner: root
group: root user::rw- 未设置
group::r--
other::r--
递归设置和查看
[root@localhost opt]# setfacl -R -m u:tom:rwx /opt/dir1/
-R属于递归设置
[root@localhost opt]# getfacl -R /opt/dir1/ #递归查看
getfacl: Removing leading '/' from absolute path names

file: opt/dir1/
owner: root
group: root
user::rwx
user:tom:rwx
group::r-x
mask::rwx
other::r-x

file: opt/dir1//file1
owner: root
group: root
user::rw-
user:tom:rwx
group::r--
mask::rwx
other::r--

file: opt/dir1//file2
owner: root
group: root
user::rw-
user:tom:rwx
group::r--
mask::rwx
other::r--

递归删除
[root@localhost opt]# setfacl -Rb /opt/dir1/
[root@localhost opt]# getfacl -R /opt/dir1
getfacl: Removing leading '/' from absolute path names

file: opt/dir1
owner: root
group: root
user::rwx
group::r-x
other::r-x

file: opt/dir1/file1
owner: root
group: root
user::rw-
group::r--
other::r--

file: opt/dir1/file2
owner: root

注意:
这个两个权限都只能针对目录。
递归ACL权限是针对目录下的子文件,文件是不可能有子文件的,会报错。
默认ACL权限是指这个目录新出现的文件或者目录遵守ACL权限。如果给文件赋予ACL权限,虽然不报错,但是没有作用。因为不能在文件中创建子文件
ACL备份及还原
ACL备份及恢复
setfacl -Rm u:harry:rwx acldir/
getfacl -R acldir/
getfacl -R acldir/ > /acl.bak 将权限备份到/acl.bak文件中
setfacl -Rb acldir/ 清除,模拟丢失
getfacl -R acldir/ 此时没有权限
setfacl --restore=/acl.bak 恢复
getfacl -R acldir/ 再查看权限已经恢复

恢复方法二: 将getfacl输出作为setfac的输入
getfacl -R /folder/ > file1
setfacl --set-file=file1 /folder/

posted on 2022-07-04 10:16  Lyon-w  阅读(357)  评论(0编辑  收藏  举报