chown 修改文件的属主、属组(对文件、目录都有效,需要用root帐户操作)
chown [OPTION]... [OWNER][:[GROUP]] FILE...
chown [OPTION]... --reference=RFILE FILE...参考某文件的权限修改当前文件的属主、属组
-R:递归修改子目录及文件的属主、属组
OWNER:属主
GROUP:属组
属主、属组之间用:或.都行
chgrp 修改文件的属组
chgrp [OPTION]... GROUP FILE...
chgrp [OPTION]... --reference=RFILE FILE...参考某文件的权限修改当前文件的属组
-R:递归修改子目录及文件的属组
文件的权限主要针对三类对象进行定义:
owner: 属主, u
group: 属组, g
other: 其他, o
每个文件针对每类访问者都定义了三种权限:
r: Readable
w: Writable
x: eXcutable
对文件来说:
r:可读,可使用类似cat等命令查看文件内容
w:可写,可以编辑或删除此文件
x:可执行,exacutable,可以在命令提示符下当作命令提交给内核运行
对目录来说:
r:可对此目录执行ls以列出内部的所有文件
w:可以在此目录创建文件
x:可以使用cd切换进此目录,也可以使用ls -l查看内部文件的详细属性
rwx权限组合:
---:无权限
r--:只读
r-x:读和执行
rw-:读和写
rwx:读写执行
权限组合对应的八进制和二进制:
0 000 --- 无权限
1 001 --x 可执行
2 010 -w- 可写
3 011 -wx 写和执行
4 100 r-- 只读
5 101 r-x 读和执行
6 110 rw- 读和写
7 111 rwx 读写执行
例如:
640: rw-r-----
755:rwxr-xr-x
修改文件权限
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...使用八进制模式的权限修改文件的权限
chmod [OPTION]... --reference=RFILE FILE...参考某文件的权限修改当前文件的权限
-R:递归修改子目录及文件的权限
chmod 用户类别(u/g/o/a)=权限(rwx) FILE
如:chmod a=r /test/test.txt 修改test.txt文件的权限为所有用户只读
chmod ug=rwx /test/test/ 修改test目录的权限为属主和属组读写执行权限
chmod 用户类别(u/g/o/a)+/-权限(rwx) FILE
如:chmod a-x /test/test.sh 取消test.sh文件所有用户的执行权限
chmod ug+w /test/test/ 给test目录的属主和属组加上写权限
新建用户的过程解析:
1,编辑passwd文件,添加newuser用户一行
nano /etc/passwd
newuser:x:2000:2000:NEWUSER:/home/newuser:/bin/bash
2,编辑group文件,添加newuser组一行
nano /etc/group
newuser:x:2000
test:x:1000:newuser
3,编辑shadow文件,添加newuser密码一行
nano /etc/shadow
newuser:!!:1970年元旦到今天天数:0:99999:7:::
4,为newuser用户创建一个家目录,复制/etc/skel到/home/newuser目录
cp -r /etc/skel /home/newuser
5,修改/home/newuser及其内部文件属主、属组均为newuser
chmown -R newuser:newuser /home/newuser
6,修改/home/newuser及其内部文件的属组和其它用户没有任何访问权限
chmod -R 700 /home/newuser
umask:查看或设置文件权限创建掩码
从最大权限中屏蔽掉相应的权限位,从而获得默认权限
umask:查看当前用户设定的umask
umask ###:临时设定当前用户的umask
umask -p:输出当前umask
umask -S:以模式的方式显示umask
长期生效:umask ###写入~/.bashrc
文件的默认最大权限:666
目录的默认最大权限:777
管理员的默认umask值:022
普通用用的默认umask值:002
默认权限=默认最大权限-默认umask
管理员:
文件的默认权限:666-022=644 rw-r--r--
目录的默认权限:777-022=755 rwxr-xr-x
普通用户:
文件的默认权限:666-002=664 rw-rw-r--
目录的默认权限:777-002=775 rwxrwxr-x
linux系统默认特性: 文件默认不允许具有执行权限,如果用户更改umask,导致计算结果文件具有执行权限时,自动将其权限位+1,取消文件执行权限
如果不想让别人对自己建立的目录及文件有任何访问权限,可以把自己的umask设定为027
安全上下文
前提:进程有属主和属组;文件有属主和属组
(1) 任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限
(2) 启动为进程之后,其进程的属主为发起者;进程的属组为发起者所属的组
(3) 进程访问文件时的权限,取决于进程的发起者
(a) 进程的发起者,同文件的属主:则应用文件属主权限
(b) 进程的发起者,属于文件属组;则应用文件属组权限
(c) 应用文件“其它”权限
文件特殊权限
SUID:只能作用于二进制的可执行程序
任何用户都可以以程序属主的权限运行
程序添加SGID命令:chmod u+s FILE...
程序删除SGID命令:chmod u-s FILE...
权限位映射显示特点:
SUID: user,占据属主的执行权限位
显示为s: 属主拥有x权限
显示为S:属主没有x权限
SGID:可作用在二进制的可执行程序上,也可作用于目录上
SGID:作用在二进制的可执行程序上时,任何用户都可以以程序属组的权限运行
程序添加SGID命令:chmod g+s FILE...
程序删除SGID命令:chmod g-s FILE...
SGID:作用在目录上时,任何用户在该目录下新建的文件和目录的属组将自动继承该目录的属组权限
目录添加SGID命令:chmod g+s DIR...
目录删除SGID命令:chmod g-s DIR...
权限位映射显示特点:
SGID: group,占据属组的执行权限位
显示为s: group拥有x权限
显示为S:group没有x权限
Sticky位:只能作用于目录上
在目录设置Sticky位,只有文件的属主或root可
以删除该文件,其他具有写权限的用户,只能编辑别人的文件
目录添加Sticky位命令:chmod o+t DIR...
目录删除Sticky位命令:chmod o-t DIR...
权限位映射显示特点:
Sticky: other,占据other的执行权限位
显示为t: other拥有x权限
显示为T:other没有x权限
文件权限显示四位时,最前面的那一位就是特殊权限代码
umask显示的是四位也正是源于此。
特殊权限组合对应的八进制和二进制:
1 001 Sticky
2 010 SGID
3 011 SGID+Sticky
4 100 SUID
5 101 SUID+Sticky
6 110 SUID+SGID
7 111 SUID+SGID+Sticky
设定文件特定属性:
chattr +a file|dir:只能增加内容,不能移动、重命名、删除
chattr +i file|dir:冻结文件或目录,只能查看,不能做任何更改
lsattr 显示特定属性
chattr +a 作用在目录上时如图所示:
[root@centos7 test]# mkdir share [root@centos7 test]# chattr +a share 给share目录添加特殊权限 [root@centos7 test]# mkdir share/dir 在share目录下新新目录及文件,都是允许的 [root@centos7 test]# touch share/file.txt [root@centos7 test]# ll share total 4 drwxr-xr-x. 2 root root 4096 Aug 6 16:13 dir -rw-r--r--. 1 root root 0 Aug 6 16:13 file.txt [root@centos7 test]# rm -rf share/dir 接着删除刚才在share目录下新新目录及文件 rm: cannot remove ‘share/dir’: Operation not permitted 都是不允许的 [root@centos7 test]# rm -f share/file.txt rm: cannot remove ‘share/file.txt’: Operation not permitted [root@centos7 test]# mv share/dir /root 接着移动刚才在share目录下新新目录及文件 mv: cannot remove ‘share/dir’: Operation not permitted 都是不允许的 [root@centos7 test]# mv share/file.txt /root mv: cannot remove ‘share/file.txt’: Operation not permitted [root@centos7 test]# ll share total 4 drwxr-xr-x. 2 root root 4096 Aug 6 16:13 dir -rw-r--r--. 1 root root 0 Aug 6 16:13 file.txt [root@centos7 test]# cd share [root@centos7 share]# rename dir test dir 接着刚才在share目录下新新目录及文件 rename: dir: rename to test failed: Operation not permitted 都是不允许的 [root@centos7 share]# rename file.txt test.txt file.txt rename: file.txt: rename to test.txt failed: Operation not permitted [root@centos7 share]# echo "hello world" > file.txt [root@centos7 share]# cat file.txt hello world
chattr +a 作用在文件上时如图所示:
[root@centos7 share]# cd .. [root@centos7 test]# touch test.txt [root@centos7 test]# chattr +a test.txt 给test.txt目录添加特殊权限 [root@centos7 test]# rm -f test.txt 删除test.txt,不允许 rm: cannot remove ‘test.txt’: Operation not permitted [root@centos7 test]# mv test.txt /root 移动test.txt,不允许 mv: cannot remove ‘test.txt’: Operation not permitted [root@centos7 test]# rename test.txt share.txt test.txt 重命名test.txt,不允许 rename: test.txt: rename to share.txt failed: Operation not permitted [root@centos7 test]# echo "good luck for you" > test.txt 覆盖重定向,不允许 -bash: test.txt: Operation not permitted [root@centos7 test]# echo "good luck for you">>test.txt 追加重定向,允许(这才是增加内容) [root@centos7 test]# cat test.txt good luck for you [root@centos7 test]# lsattr 查看chattr +a设置后的特殊属性 -----a-------e-- ./test.txt -----a-------e-- ./share
chattr +i(设置冻结属性)应用实例:
[root@centos7 test]# chattr -a share 取消特殊属性 [root@centos7 test]# chattr -a test.txt [root@centos7 test]# lsattr -------------e-- ./test.txt -------------e-- ./share [root@centos7 test]# ll share total 8 drwxr-xr-x. 2 root root 4096 Aug 6 16:13 dir -rw-r--r--. 1 root root 12 Aug 6 16:22 file.txt [root@centos7 test]# rm -rf share 删除之前创建的目录 [root@centos7 test]# rm -f file.txt 删除之前创建的文件 [root@centos7 test]# ll total 4 -rw-r--r--. 1 root root 10 Aug 6 16:31 test.txt [root@centos7 test]# rm -f test.txt [root@centos7 test]# ll total 0 [root@centos7 test]# mkdir share 重新创建测试目录 [root@centos7 test]# touch share/share.txt 在目录下创建一个文件方便后期验证效果 [root@centos7 test]# touch test.txt 重新创建测试目文件 [root@centos7 test]# echo "your test is very good">>test.txt [root@centos7 test]# cat test.txt 添加了一行内容方便后期验证效果 your test is very good [root@centos7 test]# ll total 4 drwxr-xr-x. 2 root root 4096 Aug 6 17:39 share -rw-r--r--. 1 root root 0 Aug 6 17:39 test.txt [root@centos7 test]# chattr +i share 设置冻结属性 [root@centos7 test]# chattr +i test.txt 设置冻结属性 [root@centos7 test]# mkdir share/dir 在目录下新建目录及文件,都不允许 mkdir: cannot create directory ‘share/dir’: Permission denied [root@centos7 test]# touch share/file.txt touch: cannot touch ‘share/file.txt’: Permission denied [root@centos7 test]# echo "test add">>test.txt -bash: test.txt: Permission denied 向添加过特殊权限的文件中追加内容,不允许 [root@centos7 test]# rm -rf share 删除添加过特殊权限的目及文件,都不允许 rm: cannot remove ‘share’: Operation not permitted [root@centos7 test]# rm -f test.txt rm: cannot remove ‘test.txt’: Operation not permitted [root@centos7 test]# rename share sharedir share 重命名添加过特殊权限的目及文件,都不允许 rename: share: rename to sharedir failed: Operation not permitted [root@centos7 test]# rename test.txt file.txt test.txt rename: test.txt: rename to file.txt failed: Operation not permitted [root@centos7 test]# mv share /roott 移动添加过特殊权限的目及文件,都不允许 mv: cannot remove ‘share’: Operation not permitted [root@centos7 test]# mv test.txt /root mv: cannot remove ‘test.txt’: Operation not permitted [root@centos7 test]# ll share 唯一能做的就是查看在设置冻结属性之前已有文件和目录内容 total 0 -rw-r--r--. 1 root root 0 Aug 6 19:32 share.txt 原有的目录内容是可以读取的 [root@centos7 test]# cat test.txt 原有的文件内容是可以读取的 your test is very good [root@centos7 test]# lsattr 查看chattr +i设置后的特殊属性 ----i--------e-- ./test.txt ----i--------e-- ./share
FACL:(Filesystem access control list)文件访问控制列表
利用文件保存额外的访问控制权限,实现灵活的权限管理,除了对文件的属组、属组和其它人,可以对更多的用户和组设置权限
CentOS7.0默认创建的xfs和ext4文件系统有ACL功能。
CentOS7.X之前版本,默认手工创建的ext4文件系统无ACL功能。需手动增加:
tune2fs –o acl /dev/sdb1
mount –o acl /dev/sdb1 /mnt
ACL生效顺序:所有者,自定义用户,自定义组,其他人
设置FACL
setfacl -M acl.file dest 使用文件挂载acl
setfacl -X acl.file dest 清空acl权限
setfacl -b dest 卸载facl
getfacl file |directory 查看文件或目录的acl
setfacl -R...递归设置(和其它选项合并使用)
setfacl -m u:user:rwx file|directory 单独为某用户添加acl
setfacl -m g:group:rw file| directory单独为某个组添加acl
setfacl -Rm g:group:rwX directory 单独为某个组添加acl并递归生效
setfacl -m d:u:wang:rx directory 单独为某用户添加默认acl
setfacl -m d:g:wang:rx directory 单独为某组添加默认acl
setfacl -k directory 删除默认acl
setfacl -x u:wang file |directory 清空某个用户的acl权限
复制FACL
getfacl file1 | setfacl --set-file=- file2 复制file1的acl权限给file2
备份还原FACL
getfacl -R /tmp/dir1 > acl.txt 递归备份acl权限信息
setfacl -R --set-file=acl.txt /tmp/dir1 递归还原acl权限信息
转载于:https://blog.51cto.com/183530300/1833995