目录权限
用ls -l命令查看文件详情
[root@localhost ~]# ls -l
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r--r--. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# -rw-r--r--. 第一位用来表示文件类型 此处 - 表示anaconda-ks.cfg是文件文件
[root@localhost ~]# rw-r--r--表示三个权限位,最后一位 . 表示受制于SELinux(Security-Enhanced Linux) 的安全限制,后面的1 表示目录层数
定义 r=4 w=2 x=1 则 rwx=7 rw-=6 --x=1 -wr=3 数字表示权值
命令chmod 更改目录或文件的权限
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r--r--. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# chmod 700 anaconda-ks.cfg.1
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rwx------. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
chmod - R 用来联级更改目录包括目录里所有文件的权限
chmod的其他用法 a±x a±r a±w u g o 三个权限位同理,=号后的参数不能有 --
更改文件和目录的所有者和所属组
chown命令 (change owner)
例:
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# chown elon anaconda-ks.cfg.1
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 elon root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]#
命令chgrp 更改文件或目录的所属组,无法改成不存在的所属组
例:
[root@localhost ~]# chgrp elon anaconda-ks.cfg.1
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 elon elon 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# chgrp use1 anaconda-ks.cfg.1
chgrp: 无效的组:"use1"
[root@localhost ~]#
用chown同时更改所有者和所属组 chown ownername:groupname filename或foldername
例:
[root@localhost ~]# chown root:root anaconda-ks.cfg.1
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]#
用chown :所属组 更改文件或目录的所属组
例:
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 root root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# chown :elon anaconda-ks.cfg.1
[root@localhost ~]# ls -l
总用量 20
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 root elon 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]#
chown -R的用法
chown -R usename:group foldername 级联更改目录及下层文件的所有者和所属组
umask作用是决定默认文件和目录的权限
例:
[root@localhost ~]# umask
0022
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l
总用量 20
-rw-r--r--. 1 root root 0 3月 29 08:33 1.txt
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 elon root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]# rm 1.txt
rm:是否删除普通空文件 "1.txt"?y
[root@localhost ~]# umask 002
[root@localhost ~]# umask
0002
[root@localhost ~]# touch 1.txt
[root@localhost ~]# ls -l
总用量 20
-rw-rw-r--. 1 root root 0 3月 29 08:35 1.txt
-rw-------. 1 root root 1587 3月 28 07:23 anaconda-ks.cfg
-rw-r-xr--. 1 elon root 14408 3月 28 15:03 anaconda-ks.cfg.1
[root@localhost ~]#
查看umask值,直接使用umask命令
例:
[root@localhost ~]# umask
0022
要更改umask值,umask 4位的数字,第一位为0时,可简写成3位
[root@localhost ~]# umask 002
[root@localhost ~]# umask
0002
[root@localhost ~]#
新建目录或文件的权限算法
目录权值=777-umask值 (目录没有x权限无法浏览)
文件权值=666-umask值
****此算法非数学运算,是根据文件或目录对应的9位权限相减的默认权值****
隐藏权限
文件的隐藏权限查看命令lschattr
目录的隐藏权限查看命令lschattr -d
[root@bogon ~]# lsattr anaconda-ks.cfg
---------------- anaconda-ks.cfg
[root@bogon ~]# lsattr -d /root
---------------- /root
添加隐藏权限命令 chattr +i filename或foldername
chattr +i 命令使 文件或目录的内容可读不可改,允许复制但不允许移动删除操作,不可更改时间
取消隐藏权限命令 chattr -i filename或foldername
[root@bogon ~]# touch 1.txt
[root@bogon ~]# chattr +i 1.txt
[root@bogon ~]# lsattr 1.txt
----i----------- 1.txt
[root@bogon ~]# chattr -i 1.txt
[root@bogon ~]# lsattr 1.txt
---------------- 1.txt
[root@bogon ~]#
chattr +a 隐藏修改命令 允许修改隐藏目录或文件内容,更改时间,不允许删除目录或文件 取消命令 chattr -a
lsattr -R命令 :级联查看文件或目录及下层所有目录及文件的隐藏权限