linux权限管理
权限管理1文件的基本权限1.1基础权限
-rw-r--r-- 1 root root 1548 May 7 00:18 /etc/passwd
其中文件类型 权限 常用的几种文件权限组成 更改文件的属主和属组
更改属主 例 [[email?protected] sunlizhao]# ll total 0 -rw-rw-r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh 更改属主 [[email?protected] sunlizhao]# chown root test.sh [[email?protected] sunlizhao]# ll total 0 -rw-rw-r-- 1 root sunlizhao 0 Jun 24 21:49 test.sh 更改属主和属组 [[email?protected] sunlizhao]# chown sunlizhao:root test.sh [[email?protected] sunlizhao]# ll test.sh -rw-rw-r-- 1 sunlizhao root 0 Jun 24 21:49 test.sh 更改属组 [[email?protected] sunlizhao]# chown :sunlizhao test.sh [[email?protected] sunlizhao]# ll test.sh -rw-rw-r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh 一个文件只有读的权限,拥有者可以修改这个文件吗?文件所有者一定可以写文件 [[email?protected] sunlizhao]# su sunlizhao [[email?protected] ~]$ ll total 0 -r--r--r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh 2发现没有权限,尝试使用vim进行写如内容:qqq.并强制保存退出:wq! [[email?protected] ~]$ cat test.sh [[email?protected] ~]$ vim test.sh [[email?protected] ~]$ cat test.sh qqq 修改文件,目录的权限语法:chmod [对谁操作] [操作符] [赋予什么权限] 文件名
例 [[email?protected] sunlizhao]# ll total 4 -r--r--r-- 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh 赋予文件的所有者和所属组只有读和写权限 [[email?protected] sunlizhao]# chmod ug=rw test.sh [[email?protected] sunlizhao]# ll total 4 -rw-rw-r-- 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh 赋予文件其他用户执行权限 [[email?protected] sunlizhao]# chmod o+x test.sh [[email?protected] sunlizhao]# ll total 4 -rw-rw-r-x 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh 八进制修改权限
1.2补码
umask命令允许设定文件创建时的缺省模式 永久更改:修改配置文件中的umask值
[[email?protected] ~]# vim /etc/profile if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then umask 002 else umask 022 fi
计算方法
正确的计算方法是 例子:umask为033 文件默认权限666的二进制为110 110 110 #1转换2进制 umask值为033的二进制为000 011 011 umask值得二进制取反为111 100 100 #2umask二进制后取反 110 110 110 和 111 100 100做与运算得到110 101 101 #3 与运算 110 101 101 #4转成8进制,得到权限 6 4 4 临时配置[[email?protected] ~]# umask 044 [[email?protected] ~]# touch text.sh [[email?protected] ~]# mkdir text [[email?protected] ~]# ll text.sh -d text drwx-wx-wx 2 root root 4096 Jun 24 23:42 text -rw--w--w- 1 root root 0 Jun 24 23:41 text.sh
2特殊权限文件的特殊权限 SUID只能设置在二进制可执行程序上面.对目录设置无效 [[email?protected] ~]# which passwd /usr/bin/passwd [[email?protected] ~]# ll /usr/bin/passwd -rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
例:为less增加权限,可以在普通用户下执行获得所有者root的权限 [[email?protected] ~]$ less /etc/shadow /etc/shadow: Permission denied [[email?protected] ~]$ su root Password: [[email?protected] sunlizhao]# chmod u+s /usr/bin/less [[email?protected] sunlizhao]# su - sunlizhao Last login: Tue Jun 25 00:34:59 CST 2019 on pts/0 [[email?protected] ~]$ less /etc/shadow sunlizhao:$1$ovE1KmR/$VjboKPTHp83/K5q8UqdgF.:18022:0:99999:7::: SGID即可以给二进制可执行程序设置,也可以给目录设置 例:为目录设置SGID权限后,更改GID.在目录下创建文件,发现GID继承上级目录 [[email?protected] sunlizhao]# mkdir test10 [[email?protected] sunlizhao]# chmod g+s test10/ [[email?protected] sunlizhao]# chown :bin test10/ [[email?protected] sunlizhao]# ll -d test10/ drwxr-sr-x 2 root bin 4096 Jun 25 00:45 test10/ [[email?protected] sunlizhao]# touch ./test10/test.sh [[email?protected] sunlizhao]# ll ./test10/test.sh -rw-r--r-- 1 root bin 0 Jun 25 00:48 ./test10/test.sh sticky只作用于目录 文件扩展权限ACL例:设置用户sunlizhao对文件test.sht拥有rwx权限,sunlizhao用户不属于test.sh的所属主和组,又不想给other提权,该怎么做 [[email?protected] ~]# setfacl -m u:sunlizhao:rwx /tmp/test.sh [[email?protected] ~]# 例2给目录加扩展权限(默认可以,不加d也可以) [[email?protected] ~]# mkdir /tmp/test [[email?protected] ~]# setfacl -m d:u:sunlizhao:rwx /tmp/test [[email?protected] ~]# 例3给目录下所有文件增加扩展权限 去掉所有ACL权限 [[email?protected] tmp]# ll drwxrwxr-x+ 2 root root 4096 Jun 25 01:21 test -rw-rwxr--+ 1 root root 5 Jun 25 01:18 test.sh [[email?protected] tmp]# setfacl -b /tmp/test.sh [[email?protected] tmp]# setfacl -b /tmp/test [[email?protected] tmp]# ll drwxr-xr-x 2 root root 4096 Jun 25 01:21 test -rw-r--r-- 1 root root 5 Jun 25 01:18 test.sh 3创建一个root无法删除的文件
chattr [选项] 文件 选项 +a 只能追加内容 +i 系统不允许对这个文件进行任何的修改,如果是目录,那么只能修改目录下的文件,不允许建立和删除文件 -a 取消a -i 取消i lsattr 查看 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |