chmod、chown、umask、lsattr和chattr命令

9月14日任务centos

2.14 文件和目录权限chmodbash

2.15 更改全部者和所属组chowncentos7

2.16 umaskspa

2.17 隐藏权限lsattr/chattr3d

 

文件/目录权限 chmod

预备知识

  • 全部者user:拥有该文件/目录的用户code

  • 所属组group:拥有该文件/目录的群组同步

ls命令查看文件、目录的详细信息时,其第一个字段例如"-rwxr--r--.",将除首尾外的9位每3位为一组,ast

分别是其全部者(u)、所属组(g)、其余用户(o)对该文件/目录的权限。class

几种具体用法(例rwxr-xr-x)

注意,使用表示式方式修改权限,-不要写,如g=r-x是错误的写法!!须要写成g=rx!!test

 

数字/表达式转换

  • 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: 权限不够
相关文章
相关标签/搜索