美文网首页
文件和目录管理(下)

文件和目录管理(下)

作者: 一个菜鸟JAVA | 来源:发表于2019-08-01 22:03 被阅读0次

    查看文件或目录的权限

    ls命令可以列出当前目录中的所有文件和目录.加上-l会列出详细信息,-a会列出所有的文件或者目录(包括隐藏的).例如下面的操作:

    [root@localhost test_dir]# ls -la
    total 4
    drwxrwxr-x.  4 root root   52 Jul 30 23:07 .
    drwx------. 16 root root 4096 Jul 30 23:05 ..
    -rw-rw-r--.  1 root root    0 Jul 30 23:06 1.txt
    -rw-rw-r--.  1 root root    0 Jul 30 23:06 2.txt
    drwxrwxr-x.  3 root root   35 Jul 31 22:39 d1
    drwxrwxr-x.  2 root root    6 Jul 30 23:07 d2
    [root@localhost test_dir]# tree d1
    d1
    ├── d1_1
    └── f_1_1.txt
    
    1 directory, 1 file
    

    下面解释了上面的结果:

    文件权限信息解读.png
    • 为什么d1目录显示有3个子目录?
      因为d1目录还包含两个隐含的子目录...,分别代表当前目录和上级目录.

    文件的隐藏属性

    通过lsattr可以查看文件隐藏属性.其中有13个小横线,每个横线代表一个属性.还可以通过chattr设置文件的隐藏属性.下面例子,可以设置文件a属性,使文件只能追加,不能删除,即使使用root用户也不行,但是可以追加.常用的还有i属性,使文件无法写入,改名,删除操作.

    [zengchao@localhost ~]$ touch 1.txt
    [zengchao@localhost ~]$ lsattr 1.txt 
    ---------------- 1.txt
    [zengchao@localhost ~]$ chattr +a 1.txt 
    chattr: Operation not permitted while setting flags on 1.txt
    [zengchao@localhost ~]$ sudo chattr +a 1.txt 
    [zengchao@localhost ~]$ lsattr 1.txt 
    -----a---------- 1.txt
    [zengchao@localhost ~]$ sudo echo 'hello world' >> 1.txt 
    [zengchao@localhost ~]$ cat 1.txt 
    hello world
    [zengchao@localhost ~]$ sudo rm -rf 1.txt 
    [sudo] password for zengchao: 
    rm: cannot remove ‘1.txt’: Operation not permitted
    

    改变文件权限:chmod

    在linux中任何文件都定义了文件拥有这(user),拥有组(group),其他人(oters)权限,分别用u,g,o来代表.而对应的具体权限则使用rwx来定义,增加权限使用+,删除权限使用-,详细权限使用=.例如下面表格的操作:

    操作 作用效果
    chmod u+x 1.txt 添加执行的权限给该文件的拥有者
    chmod u-x 1.txt 去掉执行的权限给该文件的拥有者
    chmod g+rwx 1.txt 添加读/写/执行的权限给该文件所属的组
    chmod o=rwx 1.txt 设置读/写/执行的权限给该文件其他用户

    在linux中,还用数字表示文件的读/写/执行,例如r(4),w(2),x(1).例如下面的命令:

    [zengchao@localhost ~]$ ls -l 1.txt 
    -rw-rw-r--. 1 zengchao zengchao 12 Aug  1 20:46 1.txt
    [zengchao@localhost ~]$ chmod 700 1.txt 
    [zengchao@localhost ~]$ ls -l 1.txt 
    -rwx------. 1 zengchao zengchao 12 Aug  1 20:46 1.txt
    

    原本该文件的拥有者的权限为:读/写,所属组的权限为:读/写/执行,其他人的权限为:读,通过chmod 700 1.txt命令,使文件的拥有者有读/写/执行的权限,所属组合其他用户没有读/写/执行的权限.

    改变文件的拥有者(chgown)和所属组(chgrp)

    文件被当前用户创建,默认拥有者就是当前用户,所属的组就是当前用户所在的组.通过chownchgrp可以修改文件所属的用户和组.例如下面的操作:

    [zengchao@localhost ~]$ ls -l 1.txt
    -rw-rw-r--. 1 zengchao zengchao 0 Aug  1 21:18 1.txt
    [zengchao@localhost ~]$ sudo chown tom 1.txt
    [zengchao@localhost ~]$ ls -l 1.txt
    -rw-rw-r--. 1 tom zengchao 0 Aug  1 21:18 1.txt
    [zengchao@localhost ~]$ sudo chgrp tom 1.txt
    [zengchao@localhost ~]$ ls -l 1.txt
    -rw-rw-r--. 1 tom      tom       0 Aug  1 21:18 1.txt
    

    特殊的文件权限s/t

    [zengchao@localhost ~]$ ls -l /etc/passwd /etc/shadow /usr/bin/passwd 
    -rw-r--r--. 1 root root  2308 Jul 28 15:51 /etc/passwd
    ----------. 1 root root  1369 Jul 28 15:52 /etc/shadow
    -rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd
    

    上面3个文件,在用户修改密码时会用到.当用户修改自己的密码时,会使用/usr/bin/passwd命令修改/etc/passwd/etc/shadow中的文件,但是这两个文件只有root用户才有权限修改,为什么不是root用户也能修改呢?
    可以发现,/usr/bin/passwd这个文件有个s权限,这个权限表示其他用户执行该文件时将具有该文件所有者的权限.但是该文件仅限制为二进制文件,在平常使用中比较少.

    [zengchao@localhost ~]$ ls -ld /tmp/
    drwxrwxrwt. 18 root root 4096 Aug  1 21:50 /tmp/
    [zengchao@localhost ~]$ touch /tmp/1.txt
    [zengchao@localhost ~]$ su - tom
    Password: 
    Last login: Thu Aug  1 21:44:01 CST 2019 on pts/0
    [tom@localhost ~]$ rm -rf /tmp/1.txt 
    rm: cannot remove ‘/tmp/1.txt’: Operation not permitted
    [tom@localhost ~]$ echo 'hello world' >> /tmp/1.txt 
    -bash: /tmp/1.txt: Permission denied
    

    上面操作,可以看到文件夹tmp对于其他用户有一个t权限,该权限允许其他用户在该目录下操作自己的文件,但是别人的文件无法操作.

    相关文章

      网友评论

          本文标题:文件和目录管理(下)

          本文链接:https://www.haomeiwen.com/subject/cgdhdctx.html