root拥有最高权限
-rw-r--r-- 1 root root 1548 May 7 00:18 /etc/passwd
- | rw- | r-- | r-- | root | root | time | filename |
---|---|---|---|---|---|---|---|
文件类型 | 拥有者权限 | 所属组权限 | 其他人的权限 | 拥有者 | 属组 | 最后修改时间 | 对象 |
其中文件类型
|文件类型|解释|
|-|-|
|p|管道文件|
|d|目录|
|l|链接文件|
|-|普通文件|
|s|socket套接口文件.比如我们启用mysql时,会产生一个mysql.sock文件|
|c|表示字符设备文件|
|b|表示块设备文件|
权限
| 权限 | 对文件来说 | 对目录来说 |
| - | - | - |
|r|读|读(看目录里面有什么)|
|w|写|在目录里面操作文件|
|x|执行|进入目录|
常用的几种文件权限组成
|权限|说明|
|-|-|
| -rwx --- --- | 文件所有者对文件具有读取,写入和执行的权限|
| -rw- r-- r-- | 文件所有者具有读,写的权限,用户组里用户和其他用户具有读取的权限|
| -rw- rw- r-x | 文件所有者和同组里用户对文件具有读写的权限,其他用户仅具有读取和执行的权限|
| drwx --x --x | 目录所有者具有读写和进入目录的权限,其他用户仅能进入该目录,却无法读取任何数据|
| drwx --- --- | 除了目录所有者具有完整权限外,其他用户对该目录完全没有任何权限|
更改属主
chown user filename
更改属主和属组
chown user:group filename
更改属组
chown :group filename
例
[root@meditation sunlizhao]# ll
total 0
-rw-rw-r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh
更改属主
[root@meditation sunlizhao]# chown root test.sh
[root@meditation sunlizhao]# ll
total 0
-rw-rw-r-- 1 root sunlizhao 0 Jun 24 21:49 test.sh
更改属主和属组
[root@meditation sunlizhao]# chown sunlizhao:root test.sh
[root@meditation sunlizhao]# ll test.sh
-rw-rw-r-- 1 sunlizhao root 0 Jun 24 21:49 test.sh
更改属组
[root@meditation sunlizhao]# chown :sunlizhao test.sh
[root@meditation sunlizhao]# ll test.sh
-rw-rw-r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh
文件所有者一定可以写文件
1切换到属主,查看权限
[root@meditation sunlizhao]# su sunlizhao
[sunlizhao@meditation ~]$ ll
total 0
-r--r--r-- 1 sunlizhao sunlizhao 0 Jun 24 21:49 test.sh
2发现没有权限,尝试使用vim进行写如内容:qqq.并强制保存退出:wq!
[sunlizhao@meditation ~]$ cat test.sh
[sunlizhao@meditation ~]$ vim test.sh
[sunlizhao@meditation ~]$ cat test.sh
qqq
语法:chmod [对谁操作] [操作符] [赋予什么权限] 文件名
对谁操作 | 解释 |
---|---|
u | 用户user,表示文件或目录的所有者 |
g | 用户组group,表示文件或目录的所属用户组 |
o | 其他用户others |
a | 所有用户 |
作符号 | 解释 |
---|---|
+ | 添加权限 |
- | 减少权限 |
= | 直接给定一个权限 |
例
[root@meditation sunlizhao]# ll
total 4
-r--r--r-- 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh
赋予文件的所有者和所属组只有读和写权限
[root@meditation sunlizhao]# chmod ug=rw test.sh
[root@meditation sunlizhao]# ll
total 4
-rw-rw-r-- 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh
赋予文件其他用户执行权限
[root@meditation sunlizhao]# chmod o+x test.sh
[root@meditation sunlizhao]# ll
total 4
-rw-rw-r-x 1 sunlizhao sunlizhao 4 Jun 24 22:15 test.sh
权限 | 二进制 | 八进制 | 描述 |
---|---|---|---|
--- | 000 | 0 | 没有任何权限 |
--x | 001 | 1 | 只有执行权限 |
-w- | 010 | 2 | 只有写入权限 |
-wx | 011 | 3 | 有执行和写入权限 |
r-- | 100 | 4 | 只有读取权限 |
r-x | 101 | 5 | 有读取和执行权限 |
rw- | 110 | 6 | 有读取和写入权限 |
rwx | 111 | 7 | 有全部权限 |
rwx的权限分别是4,2,1 .使用计算后的数值. 比如rwxr-x--x用751来表示
为什么我们创建的文件的权限默认是644呢?
我们创建文件的默认权限是怎么来的呢
umask命令允许设定文件创建时的缺省模式
修改配置文件中的umask值
[root@meditation ~]# vim /etc/profile
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
UID大于199且用户的组名和用户名一样,那么umask值为002,否则为022
普通用户一般umask值为022
777-022=755
这是一个好的记忆方法,但是不严谨.设置为033就会出错
正确的计算方法是
1, 将默认权限(目录777,文件666)和umask值都转换为2进制
2, 对umask取反
3, 将默认权限和umask取反后的值 做与运算
4,将得到的二进制再转换为8进制,即为权限
例子: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
[root@meditation ~]# umask 044
[root@meditation ~]# touch text.sh
[root@meditation ~]# mkdir text
[root@meditation ~]# 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
666-044=622
777-044=533
文件的特殊权限
|suid | sgid | sticky |
|-|-|-|
|u+s|g+s|o+t|
只能设置在二进制可执行程序上面.对目录设置无效
功能:程序运行时的权限从执行者变更为 程序所有者的权限
例子
[root@meditation ~]# which passwd
/usr/bin/passwd
[root@meditation ~]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
SUID属性一般用在可执行文件上,当用户执行该文件时,会临时拥有该执行文件的所有者权限
当我们使用passwd时,会获得所有者root的权限
比如普通用户用passwd修改密码.其实根本没有权限去修改/etc/shadow文件.也是在使用passwd修改时,获得的权限
例:为less增加权限,可以在普通用户下执行获得所有者root的权限
[sunlizhao@meditation ~]$ less /etc/shadow
/etc/shadow: Permission denied
[sunlizhao@meditation ~]$ su root
Password:
[root@meditation sunlizhao]# chmod u+s /usr/bin/less
[root@meditation sunlizhao]# su - sunlizhao
Last login: Tue Jun 25 00:34:59 CST 2019 on pts/0
[sunlizhao@meditation ~]$ less /etc/shadow
sunlizhao:$1$ovE1KmR/$VjboKPTHp83/K5q8UqdgF.:18022:0:99999:7:::
即可以给二进制可执行程序设置,也可以给目录设置
功能,在设置了SGID权限的目录下简历文件时,新创建的文件的所属组会继承上级目录的权限
例:为目录设置SGID权限后,更改GID.在目录下创建文件,发现GID继承上级目录
[root@meditation sunlizhao]# mkdir test10
[root@meditation sunlizhao]# chmod g+s test10/
[root@meditation sunlizhao]# chown :bin test10/
[root@meditation sunlizhao]# ll -d test10/
drwxr-sr-x 2 root bin 4096 Jun 25 00:45 test10/
[root@meditation sunlizhao]# touch ./test10/test.sh
[root@meditation sunlizhao]# ll ./test10/test.sh
-rw-r--r-- 1 root bin 0 Jun 25 00:48 ./test10/test.sh
只作用于目录
功能: 目录下创建的文件只有root,文件创建者,目录所有者才能删除
例:设置用户sunlizhao对文件test.sht拥有rwx权限,sunlizhao用户不属于test.sh的所属主和组,又不想给other提权,该怎么做
[root@meditation ~]# setfacl -m u:sunlizhao:rwx /tmp/test.sh
[root@meditation ~]#
例2给目录加扩展权限(默认可以,不加d也可以)
[root@meditation ~]# mkdir /tmp/test
[root@meditation ~]# setfacl -m d:u:sunlizhao:rwx /tmp/test
[root@meditation ~]#
例3给目录下所有文件增加扩展权限
[root@meditation ~]# setfacl -R -m u:sunlizhao:rwx /tmp/test
[root@meditation ~]#
去掉所有ACL权限
[root@meditation 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
[root@meditation tmp]# setfacl -b /tmp/test.sh
[root@meditation tmp]# setfacl -b /tmp/test
[root@meditation 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
centos6开始有的
chattr [选项] 文件
选项
+a 只能追加内容
+i 系统不允许对这个文件进行任何的修改,如果是目录,那么只能修改目录下的文件,不允许建立和删除文件
-a 取消a
-i 取消i
lsattr 查看
原文:https://www.cnblogs.com/inmeditation/p/11572349.html