====文件权限====
rw-r--r-- root root install.log
权限对象:
属主:u
属组:g
其他人: o
所有人:a
权限类型:
读:r 4
写:w 2
执行: x 1
===设置权限
1. 更改文件的属主、属组
chown:
[root@station230 ~]# chown alice.hr file1 //改属主、属组
[root@station230 ~]# chown alice file1 //只改属主
[root@station230 ~]# chown .hr file1 //只改属组
chgrp:
[root@station230 ~]# chgrp it file1 //改文件属组
[root@station230 ~]# chgrp -R it dir1 //改文件夹属组
2. 更改权限
a. 使用字符
对象 赋值符 权限类型
u + r
chmod g - w
o = x
a
[root@station230 ~]# chmod u+x file1 //属主加执行
[root@station230 ~]# chmod a=rwx file1 //所有人等于读写执行
[root@station230 ~]# chmod a=- file1 //所有人没有权限
[root@station230 ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@station230 ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果
b. 数字
[root@station230 ~]# chmod 644 file1
[root@station230 ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1
=================================================================
读、写、执行 测试
[root@station230 ~]# mkdir /home/test
[root@station230 ~]# touch /home/test/file1
[root@station230 ~]# touch /home/test/file2
[root@station230 ~]# ll -d /home/test/
drwxr-xr-x 2 root root 4096 10-26 10:44 /home/test/
[root@station230 ~]# chmod o=- /home/test/
[root@station230 ~]# ll -d /home/test/
drwxr-x--- 2 root root 4096 10-26 10:44 /home/test/
=================================================================
[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01
[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01
[root@station230 ~]#
[root@station230 ~]# mkdir /home/up01
[root@station230 ~]# chgrp up01 /home/up01/
[root@station230 ~]# chmod 770 /home/up01/
[root@station230 ~]# ll -d /home/up01/
drwxrwx--- 2 root up01 4096 10-26 11:41 /home/up01/
特殊权限:
suid 4
sgid 2
sticky 1
umask:文件、目录创建后的默认权限是由umask决定的
root: 文件 644 umask: 0022
目录 755
普通用户:文件 664 umask: 0002
目录 775
[root@station230 ~]# umask -S //缺省文件夹的实际权限
u=rwx,g=rx,o=rx
[root@station230 ~]# umask //查看当前用户的umask权限
0022
[root@station230 ~]# umask 000 //设置umask权限
[root@station230 ~]# umask
0000
[root@station230 ~]# touch file8 //创建file8文件
[root@station230 ~]# mkdir dir8 //创建目录
[root@station230 ~]# ll -d dir8 file8 //查看文件目录权限
drwxrwxrwx 2 root root 4096 10-26 14:10 dir8
-rw-rw-rw- 1 root root 0 10-26 14:10 file8
[root@station230 ~]# umask 222
[root@station230 ~]# umask
0222
==============================================================
小知识:
[root@station230 ~]# umask 077; touch file60 //当前shell生效
[root@station230 ~]# (umask 077; touch file70) //()表示在子shell生效
[root@station230 ~]# ll file70
-rw------- 1 root root 0 10-26 14:31 file70
[root@station230 ~]#
[root@station230 ~]# umask
0022
==============================================================
ACL: UGO权限的扩展
UGO传统权限:只能一个用户,一个组和其他人
[root@station230 ~]# touch /home/test.txt
[root@station230 ~]# ll /home/test.txt
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
[root@station230 ~]# getfacl /home/test.txt
# file: test
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@station230 ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[root@station230 ~]# setfacl -m u:jack:- /home/test.txt //增加用户jack权限
查看:
[root@station230 ~]# ll /home/test.txt
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[root@station230 ~]# getfacl /home/test.txt
[root@station230 ~]# setfacl -m g:hr:r /home/test.txt
[root@station230 ~]# setfacl -x g:hr /home/test.txt //删除组hr的权限
[root@station230 ~]# setfacl -b /home/test.txt //删除所有acl权限
man setfacl
/EXAMPLES
ACL: 继承(默认)
[root@station230 ~]# mkdir -p /home/dir100/aaa
[root@station230 ~]# setfacl -m d:u:jack:rwx /home/dir100
==========
设置文件扩展属性(权限),针对所有用户,包括root
[root@station230 ~]# lsattr fil100.txt file200.txt
------------- fil100.txt
------------- file200.txt
[root@station230 ~]# chattr +a fil100.txt 仅能被增加,而不能能被删除.直到 chattr -a 取消这个 a 的参数之后,才能被删除或者移动
[root@station230 ~]# chattr +i file200.txt 连 root 都不能杀掉!而且也不能新增数据
[root@station230 ~]# lsattr fil100.txt file200.txt
-----a------- fil100.txt
----i-------- file200.txt
[root@station230 ~]# cat fil100.txt file200.txt
1111
222
[root@station230 ~]# rm -rf fil100.txt file200.txt
rm: 无法删除 “fil100.txt”: 不允许的操作
rm: 无法删除 “file200.txt”: 不允许的操作
[root@station230 ~]# echo 111 >> fil100.txt
[root@station230 ~]# cat fil100.txt
1111
111
[root@station230 ~]# echo 111 >> file200.txt
bash: file200.txt: 权限不够
[root@station230 ~]# chattr -i file200.txt
[root@station230 ~]# lsattr file200.txt
------------- file200.txt
rw-r--r-- root root install.log
权限对象:
属主:u
属组:g
其他人: o
所有人:a
权限类型:
读:r 4
写:w 2
执行: x 1
===设置权限
1. 更改文件的属主、属组
chown:
[root@station230 ~]# chown alice.hr file1 //改属主、属组
[root@station230 ~]# chown alice file1 //只改属主
[root@station230 ~]# chown .hr file1 //只改属组
chgrp:
[root@station230 ~]# chgrp it file1 //改文件属组
[root@station230 ~]# chgrp -R it dir1 //改文件夹属组
2. 更改权限
a. 使用字符
对象 赋值符 权限类型
u + r
chmod g - w
o = x
a
[root@station230 ~]# chmod u+x file1 //属主加执行
[root@station230 ~]# chmod a=rwx file1 //所有人等于读写执行
[root@station230 ~]# chmod a=- file1 //所有人没有权限
[root@station230 ~]# chmod ug=rw,o=r file1 //属主属组等于读写,其他人只读
[root@station230 ~]# ll file1 //以长模式方式查看文件权限
-rw-rw-r-- 1 alice it 17 10-25 16:45 file1 //显示的结果
b. 数字
[root@station230 ~]# chmod 644 file1
[root@station230 ~]# ll file1
-rw-r--r-- 1 alice it 17 10-25 16:45 file1
=================================================================
读、写、执行 测试
[root@station230 ~]# mkdir /home/test
[root@station230 ~]# touch /home/test/file1
[root@station230 ~]# touch /home/test/file2
[root@station230 ~]# ll -d /home/test/
drwxr-xr-x 2 root root 4096 10-26 10:44 /home/test/
[root@station230 ~]# chmod o=- /home/test/
[root@station230 ~]# ll -d /home/test/
drwxr-x--- 2 root root 4096 10-26 10:44 /home/test/
=================================================================
[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01
[root@station230 ~]# groupadd up01
[root@station230 ~]# useradd user1000 -G up01
[root@station230 ~]# useradd user2000 -G up01
[root@station230 ~]#
[root@station230 ~]# mkdir /home/up01
[root@station230 ~]# chgrp up01 /home/up01/
[root@station230 ~]# chmod 770 /home/up01/
[root@station230 ~]# ll -d /home/up01/
drwxrwx--- 2 root up01 4096 10-26 11:41 /home/up01/
特殊权限:
suid 4
sgid 2
sticky 1
umask:文件、目录创建后的默认权限是由umask决定的
root: 文件 644 umask: 0022
目录 755
普通用户:文件 664 umask: 0002
目录 775
[root@station230 ~]# umask -S //缺省文件夹的实际权限
u=rwx,g=rx,o=rx
[root@station230 ~]# umask //查看当前用户的umask权限
0022
[root@station230 ~]# umask 000 //设置umask权限
[root@station230 ~]# umask
0000
[root@station230 ~]# touch file8 //创建file8文件
[root@station230 ~]# mkdir dir8 //创建目录
[root@station230 ~]# ll -d dir8 file8 //查看文件目录权限
drwxrwxrwx 2 root root 4096 10-26 14:10 dir8
-rw-rw-rw- 1 root root 0 10-26 14:10 file8
[root@station230 ~]# umask 222
[root@station230 ~]# umask
0222
==============================================================
小知识:
[root@station230 ~]# umask 077; touch file60 //当前shell生效
[root@station230 ~]# (umask 077; touch file70) //()表示在子shell生效
[root@station230 ~]# ll file70
-rw------- 1 root root 0 10-26 14:31 file70
[root@station230 ~]#
[root@station230 ~]# umask
0022
==============================================================
ACL: UGO权限的扩展
UGO传统权限:只能一个用户,一个组和其他人
[root@station230 ~]# touch /home/test.txt
[root@station230 ~]# ll /home/test.txt
-rw-r--r-- 1 root root 0 10-26 13:59 /home/test.txt
[root@station230 ~]# getfacl /home/test.txt
# file: test
# owner: root
# group: root
user::rw-
group::r--
other::r--
[root@station230 ~]# setfacl -m u:alice:rw /home/test.txt //增加用户alice权限
[root@station230 ~]# setfacl -m u:jack:- /home/test.txt //增加用户jack权限
查看:
[root@station230 ~]# ll /home/test.txt
-rw-rw-r--+ 1 root root 0 10-26 13:59 /home/test.txt
[root@station230 ~]# getfacl /home/test.txt
[root@station230 ~]# setfacl -m g:hr:r /home/test.txt
[root@station230 ~]# setfacl -x g:hr /home/test.txt //删除组hr的权限
[root@station230 ~]# setfacl -b /home/test.txt //删除所有acl权限
man setfacl
/EXAMPLES
ACL: 继承(默认)
[root@station230 ~]# mkdir -p /home/dir100/aaa
[root@station230 ~]# setfacl -m d:u:jack:rwx /home/dir100
==========
设置文件扩展属性(权限),针对所有用户,包括root
[root@station230 ~]# lsattr fil100.txt file200.txt
------------- fil100.txt
------------- file200.txt
[root@station230 ~]# chattr +a fil100.txt 仅能被增加,而不能能被删除.直到 chattr -a 取消这个 a 的参数之后,才能被删除或者移动
[root@station230 ~]# chattr +i file200.txt 连 root 都不能杀掉!而且也不能新增数据
[root@station230 ~]# lsattr fil100.txt file200.txt
-----a------- fil100.txt
----i-------- file200.txt
[root@station230 ~]# cat fil100.txt file200.txt
1111
222
[root@station230 ~]# rm -rf fil100.txt file200.txt
rm: 无法删除 “fil100.txt”: 不允许的操作
rm: 无法删除 “file200.txt”: 不允许的操作
[root@station230 ~]# echo 111 >> fil100.txt
[root@station230 ~]# cat fil100.txt
1111
111
[root@station230 ~]# echo 111 >> file200.txt
bash: file200.txt: 权限不够
[root@station230 ~]# chattr -i file200.txt
[root@station230 ~]# lsattr file200.txt
------------- file200.txt