查看文件信息,使用ls -l命令,执行后其结果以下:bash
[root@test-01 ~]# ls -l 总用量 4 drwxr-xr-x. 2 root root 6 12月 19 08:17 4 -rw-------. 1 root root 973 12月 12 17:09 anaconda-ks.cfg
如上面显示,文件类型由第一位表示,文件或目录的权限由后面9个权限位组成,三位一组,2-4位表示文件或目录的全部者的权限,5-7位表示文件所属组对这个文件的权限,8-10位表示全部者和所属组外的其余用户对该文件的权限。权限表示顺序是是否可读、是否可写、是否可执行=rwx ,好比说没有写的权限,就表示为r-x 。 其中r=4 w=2 x=1 rwx=7 ,rw-=6 , --x=1 , rw-r--r--=644 。code
[root@test-01 tmp]# ls -la 1 总用量 4 drwxr-xr-x. 2 root root 6 12月 19 21:13 . drwxrwxrwt. 9 root root 4096 12月 21 15:38 .. [root@test-01 tmp]# chmod 700 1 [root@test-01 tmp]# !ls ls -la 1 总用量 4 drwx------. 2 root root 6 12月 19 21:13 . drwxrwxrwt. 9 root root 4096 12月 21 15:38 ..
这样就更改了目录tmp/1的权限。drwxr-xr-x.这个字符串最后一个“.”表示这个目录或文件受制于SELinux。字符串
chmod -R 命令更改目录的权限时会将该目录下全部的子目录,子文件都更改权限。级联更改。同步
chmod u=rwx,g=r,o=r filname。这条命令当中,u 表示全部者,g 表示所属组,o表示其余用户。 chmod a-x filname,其中a表示全部,这条命令的意思是,对于这个文件,全部者、所属组、其余用户都没有执行权限;chmod a+x _filname_这条命令表示对于这个文件,全部者,所属组,其余用户都有执行权限。it
使用格式为 :chown _username _filnametest
[root@test-01 ~]# useradd lc1 [root@test-01 ~]# useradd lc2 [root@test-01 ~]# ls -l /tmp/yum.log -rw-------. 1 root root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]# chown lc1 /tmp/yum.log [root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]#
命令格式为:chgrp_ username__ filename_date
[root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 root 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]# chgrp lc1 /tmp/yum.log [root@test-01 ~]# !ls ls -l /tmp/yum.log -rw-------. 1 lc1 lc1 0 12月 12 17:07 /tmp/yum.log [root@test-01 ~]#
** 使用chown 也能够更改文件的所属组,其使用格式为:chown username:group filename,这条命令中若是username省略,表示只更改所属组。*file
chattr的格式为chattr [+-=] 参数 [文件或目录] 参数以下:权限
[root@test-01 ~]# chattr +a /tmp/1 [root@test-01 ~]# lsattr /tmp/1 [root@test-01 ~]# lsattr /tmp ---------------- /tmp/yum.log -----a-A-------- /tmp/1 [root@test-01 ~]# chattr -A /tmp/1 [root@test-01 ~]# lsattr /tmp/1 [root@test-01 ~]# lsattr /tmp ---------------- /tmp/yum.log -----a---------- /tmp/1 [root@test-01 ~]# mkdir /tmp/1/2 [root@test-01 ~]# rmdir /tmp/1/2 rmdir: 删除 "/tmp/1/2" 失败: 不容许的操做 [root@test-01 ~]#
一个文件被赋予这个a的属性后,只能追加内容,不能删除文件里面的内容command
[root@test-01 ~]# vi /tmp/1/1.txt [root@test-01 ~]# chattr +a /tmp/1/1.txt [root@test-01 ~]# !head head -n 10 /etc/passwd >> /tmp/1/1.txt [root@test-01 ~]# vi /tmp/1/1.txt E325: ATTENTION Found a swap file by the name "/tmp/1/1_txt.swp" owned by: root dated: Sat Dec 23 03:30:18 2017 file name: /tmp/1/1.txt modified: no /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown ~ ~ ~ "/tmp/1/1.txt" "/tmp/1/1.txt" E212: Can't open file for writing Press ENTER or type command to continue [root@test-01 ~]# rm -f /tmp/1/1.txt rm: 没法删除"/tmp/1/1.txt": 不容许的操做 [root@test-01 ~]#
当一个文件被赋予i属性时,不能删除,不能重命名不能写入,不能新增数据
[root@test-01 ~]# head -n 10 /etc/passwd >> /tmp/1/1.txt -bash: /tmp/1/1.txt: 权限不够 [root@test-01 ~]# vi /tmp/1/1.txt [root@test-01 ~]# mv /tmp/1/1.txt /tmp/1/2.txt mv: 没法将"/tmp/1/1.txt" 移动至"/tmp/1/2.txt": 不容许的操做 [root@test-01 ~]#
该命令用于读取文件或目录的特殊权限,格式为:lsattr 参数 文件或目录
[root@localhost ~]# lsattr -R /tmp ---------------- /tmp/test_mv /tmp/test_mv: ---------------- /tmp/test_mv/3 /tmp/test_mv/3: ---------------- /tmp/test_mv/3/2 /tmp/test_mv/3/2: ---------------- /tmp/test_mv/3/2/2.txt ---------------- /tmp/test_mv/3/4 /tmp/test_mv/3/4: ---------------- /tmp/test_mv/3/4/2.txt ---------------- /tmp/test_mv/3/4/2 /tmp/test_mv/3/4/2: ---------------- /tmp/test_mv/3/4/2/2.txt
默认状况下,目录的权限值为755,文件的权限值为644,这两个值是由umask干涉的,umask默认值为0022 ,第一个0不用看,当建立目录时,预设值为777,由于有umask的干涉,它的默认值会变为755,就是预设值减去umask值,当建立普通文件时,预设是没有可执行权限为666,减去umask值变为644 。 umask值能够更改,命令为umask xxx ,
[root@localhost ~]# touch /tmp/1.txt [root@localhost ~]# ls -ld /tmp/1.txt -rw-r--r--. 1 root root 0 6月 8 00:55 /tmp/1.txt [root@localhost ~]# mkdir /tmp/1 [root@localhost ~]# ls -ld /tmp/1 drwxr-xr-x. 2 root root 6 6月 8 00:56 /tmp/1 [root@localhost ~]# umask 002 [root@localhost ~]# touch /tmp/2.txt [root@localhost ~]# mkdir /tmp/2 [root@localhost ~]# ls -ld /tmp drwxrwxrwt. 9 root root 4096 6月 8 00:56 /tmp [root@localhost ~]# ls -ld /tmp/2 drwxrwxr-x. 2 root root 6 6月 8 00:56 /tmp/2 [root@localhost ~]# ls -ld /tmp/2.txt -rw-rw-r--. 1 root root 0 6月 8 00:56 /tmp/2.txt