我们之前已经很多次用到 ls命令了,如你所见,我们用它来列出并显示当前目录下的文件,当然这是在不带任何参数的情况下,它能做的当然不止这么多,现在我们就要用它来查看文件权限。
[root@VM_200_13_CentOS ~]# ls -ltotal 8drwxr-xr-x 2 root root 4096 Aug 14 11:37 auth-rw-r--r-- 1 root root7 Aug 14 11:34 auth.txt | 例子 | 说明 |
|---|---|
| -rwx------ | 文件所有者对文件具有读取、写入和执行的权限 |
| -rwxr--r-- | 文件所有者具有读、写与执行的权限 其他用户则具有读取的权限 |
| -rw-rw-r-x | 文件所有者与同组用户具有读写权限 其他用户具有读取和执行权限 |
| drwx--x--x | 目录所有者具有浏览目录修改(删除、移动)目录和进入目录的权限 同组用户和其他用户具有进入目录的权限 |
| drwx------ | 除了目录所有者具有完整的权限之外 其他用户对该目录完全没有任何权限 |
其实文件与目录设置不止这些,还有所谓的特殊权限。由于特殊权限会拥有一些“特权”。
因而用户若无特殊需求,不应该启用这些权限,避免安全方面出现严重漏洞,造成入侵,甚至摧毁系统!
[root@VM_200_13_centos ~]# su - testLast login: Sun Aug 14 19:05:04 CST 2016 on pts/0[test@VM_200_13_centos ~]$ which passwd/bin/passwd[test@VM_200_13_centos ~]$ ll /bin/passwd-rwsr-xr-x. 1 root root 27832 Jun 102014 /bin/passwd该文件的所属用户和所属用户组均为root,显然test用户不具有该执行文件的读写执行权限[test@VM_200_13_centos ~]$ passwdChanging password for user test.Changing password for test.(current) UNIX password:New password:Retype new password:passwd: all authentication tokens updated successfully.因为具有SUID权限所有普通用户执行该命令能修改密码因为SUID、SGID、Sticky占用x的位置来表示,所以在表示上会有大小写之分。
加入同时开启执行权限和SUID、SGID、Sticky,则权限表示字符是小写的
如果不具有x权限,会以大写显示
[root@VM_200_13_centos ~]# mkdir test[root@VM_200_13_centos ~]# lltotal 4drwxr-xr-x 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod u+s test/[root@VM_200_13_centos ~]# lltotal 4drwsr-xr-x 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod u-x test/[root@VM_200_13_centos ~]# lltotal 4drwSr-xr-x 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod g+s test/[root@VM_200_13_centos ~]# lltotal 4drwSr-sr-x 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod g-x test/[root@VM_200_13_centos ~]# lltotal 4drwSr-Sr-x 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod o+t test/[root@VM_200_13_centos ~]# lltotal 4drwSr-Sr-t 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod o-x test/[root@VM_200_13_centos ~]# lltotal 4drwSr-Sr-T 2 root root 4096 Aug 14 19:59 test | 选项 | 说明 |
|---|---|
| -c | 只输出成功修改权限的文件权限更改信息 |
| -f | 忽略大部分的错误信息 |
| -v | 输出详细信息 |
| -R | 递归更改目录和文件的权限 |
[root@VM_200_13_centos ~]# lltotal 4drwsr-Sr-T 2 root root 4096 Aug 14 19:59 test[root@VM_200_13_centos ~]# chmod u-x test/[root@VM_200_13_centos ~]# chmod g+w test/[root@VM_200_13_centos ~]# chmod o+w test/[root@VM_200_13_centos ~]# lltotal 4drwSrwSrwT 2 root root 4096 Aug 14 19:59 test如果要为文件设置多个用户权限(用户权限、用户组用户权限、其他用户权限)这种方式就很麻烦,需要多次设置| 权限 | 二进制 | 十进制 |
|---|---|---|
| r | 100 | 4 |
| w | 10 | 2 |
| x | 1 | 1 |
| s | SUID:100 SGID:10 | SUID:4 SGID:2 |
| t | 1 | 1 |
| - | 0 | 0 |
[root@VM_200_13_centos ~]# lltotal 0-rw-r--r-- 1 root root 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# chmod 000 a[root@VM_200_13_centos ~]# lltotal 0---------- 1 root root 0 Aug 14 20:41 a| 例子 | 说明 |
|---|---|
| -rwx------ | 二进制:111000000 十进制:700 权限:所属用户具有读写执行,所属用户组用户和其他用户没有任何权限 |
| -rwx---rwx | 二进制:111000111 十进制:707 权限:所属用户和其他用户具有读写执行,所属用户组用户没有任何权限 |
| -rwx--xrwx | 二进制:111001111 十进制:717 权限:所属用户和其他用户具有读写执行,所属用户组用户只有执行权限 |
[root@VM_200_13_centos ~]# chmod 711 a[root@VM_200_13_centos ~]# lltotal 0-rwx--x--x 1 root root 0 Aug 14 20:41 a如果要授予特殊权限SUID(4)、SGIU(2)、SBIT(1)则要使用4位1进制数表示[root@VM_200_13_centos ~]# lltotal 0---------- 1 root root 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# chmod 7000 a[root@VM_200_13_centos ~]# lltotal 0---S--S--T 1 root root 0 Aug 14 20:41 a| 选项 | 说明 |
|---|---|
| -c | 只输出成功修改权限的文件权限更改信息 |
| -f | 忽略大部分的错误信息 |
| -v | 输出详细信息 |
| -h | 修复符号链接 |
| -R | 递归更改目录和文件的权限 |
| --deference | 作用于符号链接的指向,而不是链接文件本身 |
[root@VM_200_13_centos ~]# lltotal 0---S--S--T 1 root root 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# chown test:test a[root@VM_200_13_centos ~]# lltotal 0------S--T 1 test test 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# chown root a[root@VM_200_13_centos ~]# lltotal 0------S--T 1 root test 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# chown :test1 a[root@VM_200_13_centos ~]# lltotal 0------S--T 1 root test1 0 Aug 14 20:41 a[root@VM_200_13_centos ~]# mkdir -p ~/aa/b/c[root@VM_200_13_centos ~]# touch aa/a[root@VM_200_13_centos ~]# chown -R -v:test1 aa/ownership of "aa/a" retained as root:test1ownership of "aa/b/c" retained as root:test1ownership of "aa/b" retained as root:test1ownership of "aa/" retained as root:test1 | 选项 | 说明 |
|---|---|
| -R | 递归处理,将指定目录下的所有文件及子目录一并处理 |
| -v | <版本编号> 设置文件或目录版本 |
| -V | 显示指令执行过程 |
| + | 开启文件或目录的指定属性 |
| - | 关闭文件或目录的指定属性 |
| = | 指定文件或目录的指定属性 |
| 属性 | 说明 |
|---|---|
| A | 即Atime,告诉系统不要修改对这个文件的最后访问时间 |
| S | 即Sync,一旦应用程序对这个文件执行了写操作 使系统立刻把修改的结果写到磁盘 |
| a | 即Append Only,系统只允许在这个文件之后追加数据 不允许任何进程覆盖或截断这个文件 如果目录具有这个属性,系统将只允许在这个目录下建立和修改文件 而不允许删除任何文件 |
| b | 不更新文件或目录的最后存取时间 |
| c | 将文件或目录压缩后存放 |
| d | 当dump程序执行时,该文件或目录不会被dump备份 |
| D | 检查压缩文件中的错误 |
| i | 即Immutable,系统不允许对这个文件进行任何的修改 如果目录具有这个属性,那么任何的进程只能修改目录之下的文件 不允许建立和删除文件 |
| s | 彻底删除文件,不可恢复,因为是从磁盘上删除 然后用0填充文件所在区域 |
| u | 当一个应用程序请求删除这个文件 系统会保留其数据块以便以后能够恢复删除这个文件 用来防止意外删除文件或目录 |
| t | 文件系统支持尾部合并(tail-merging) |
| X | 可以直接访问压缩文件的内容 |
[root@VM_200_13_centos ~]# chattr +i /etc/passwd[root@VM_200_13_centos ~]# lsattr /etc/passwd----i----------- /etc/passwd要想修改此文件就要把i属性去掉[root@VM_200_13_centos ~]# chattr -i /etc/passwd[root@VM_200_13_centos ~]# lsattr /etc/passwd---------------- /etc/passwd让某个文件只能往里面追加数据但不能删除[root@VM_200_13_centos ~]# chattr +a /etc/passwd[root@VM_200_13_centos ~]# lsattr /etc/passwd-----a---------- /etc/passwd[root@VM_200_13_centos ~]# chattr -a /etc/passwd[root@VM_200_13_centos ~]# lsattr /etc/passwd---------------- /etc/passwd本文永久更新链接地址