9月14日任务
2.14 文件和目录权限chmod
2.15 更改所有者和所属组chown
2.16 umask
2.17 隐藏权限lsattr/chattr
文件/目录权限 chmod
预备知识
-
所有者user:拥有该文件/目录的用户
-
所属组group:拥有该文件/目录的群组
ls命令查看文件、目录的详细信息时,其第一个字段例如"-rwxr--r--.",将除首尾外的9位每3位为一组,
分别是其所有者(u)、所属组(g)、其他用户(o)对该文件/目录的权限。
几种具体用法(例rwxr-xr-x)
注意,使用表示式方式修改权限,-不要写,如g=r-x是错误的写法!!需要写成g=rx!!
数字/表达式转换
-
r - 读权限 - 4
-
w - 写权限 - 2
-
x - 执行权限 - 1
重要参数: -R
-
chmod -R 权限 DIR
一次性修改目录及其下属文件、目录的权限
[root@centos7 test]# ll
总用量 0
----------. 1 root root 0 ... file1
----------. 1 root root 0 ... file2
-rwxrwxrwx. 1 root root ... file3
# 数字形式
[root@centos7 test]# chmod 755 file1
# 表达式形式
[root@centos7 test]# chmod u=rwx,g=rx,o=rx file2
# 删减形式
[root@centos7 test]# chmod g-w,o-w file3
[root@centos7 test]# ll
总用量 0
-rwxr-xr-x. 1 root root 0 ... file1
-rwxr-xr-x. 1 root root 0 ... file2
-rwxr-xr-x. 1 root root 0 ... file3
修改文件、目录的所有者/所属组 chown
chown命令可以只修改所有者,也可以只修改所属组,也可以同时修改其所属主和所属组。如下方的几个重要用法
几个具体用法
说明:
-
只修改所有者:chown castiel file1
-
只修改所属组:chown :castiel file2
-
同时都修改:chown castiel:castiel file3
重要参数:-R
一次性修改目录及其内的文件目录的所有者、所属组:chown -R DIR
[root@centos7 test]# ls -l
总用量 0
-rw-r--r--. 1 root root 0 ... file1
-rw-r--r--. 1 root root 0 ... file2
-rw-r--r--. 1 root root 0 ... file3
[root@centos7 test]# chown castiel file1
[root@centos7 test]# chown :castiel file2
[root@centos7 test]# chown castiel:castiel file3
[root@centos7 test]# ls -ll
总用量 0
-rw-r--r--. 1 castiel root 0 ... file1
-rw-r--r--. 1 root castiel 0 ... file2
-rw-r--r--. 1 castiel castiel 0 ... file3
[root@centos7 test]# ls -l
总用量 0
drwxr-xr-x. 2 root root 6 ... dir1
-rw-r--r--. 1 root root 0 ... file1
[root@centos7 test]# ls -ld /test/
drwxr-xr-x. 3 root root 31 ... /test/
[root@centos7 test]# chown -R castiel /test/
# /test目录以及内部的文件目录的所有者被一次性改变!
[root@centos7 test]# ls -l
总用量 0
drwxr-xr-x. 2 castiel root 6 ... dir1
-rw-r--r--. 1 castiel root 0 ... file1
[root@centos7 test]# ls -ld /test/
drwxr-xr-x. 3 castiel root 31 ... /test/
默认权限 umask
root用户的umask值为0022,普通用户的umask值为0002。 系统默认创建的权限是跟系统设置的umask值有关的!
对于root用户
-
默认的目录权限为:0777-0022=0755(rwxr-xr-x);
-
默认的文件权限为:0666-0022=0644(rw-r--r--)。
计算方法是先将数字转换为rwx再进行计算。
同理,对于普通用户
-
默认的目录权限为0775;
-
默认的文件权限为0664。
修改umask值,默认创建的文件/目录的权限也会随之变化!
隐藏权限 lsattr/chatter
查看隐藏权限:lsattr
默认查看一个目录下的文件或子目录的隐藏权限;如果要查看目录本身需要加上-d参数!
-
lsattr -R DIR
可以一次性查看目录及其下层文件、目录的隐藏权限
修改隐藏权限:chattr
-
chattr +i file 无法修改文件(也不能追加)
-
chattr +a file 无法修改文件,但是可以追加内容
无法修改的具体表现为:无法删除、修改内容、追加内容、重命名、修改时间。a参数可追加内容,i参数不可追加,其他方面所有都相同。
重要参数:-R
-
chattr -R DIR
一次性修改目录及其下属文件、目录的隐藏权限!
chattr权限对于目录有一点不同:对于目录中已经存在的文件,可以进行修改;目录中不存在的新建的文件将无法创建
(但是对于设置+a参数的目录,可以执行追加创建新文件,但无法删除)!
拓展:其他chattr可用参数
-
A 设置了该参数的文件或目录的atime不可修改
-
s 数据会自动同步写入磁盘
-
c 自动压缩该文件,读取时自动解压
补充知识点:目录必须需要x权限才能执行:要在目录下创建、修改文件,必须先进入该目录!!
[root@centos7 ~]# chattr +a 111
[root@centos7 ~]# echo "1" >> 111/1.txt
[root@centos7 ~]# echo "1" > 111/2.txt
[root@centos7 ~]# rm -f 111/1.txt
rm: 无法删除"111/1.txt": 不允许的操作
[root@centos7 ~]# chattr +i 111/
[root@centos7 ~]# touch 111/3.txt
touch: 无法创建"111/3.txt": 权限不够
[root@centos7 ~]# echo "111" >> 111/3.txt
-bash: 111/3.txt: 权限不够