一、 文件或目录权限 chmod

chmod命令用来变更文件或目录的权限。只有文件主和超级用户才可以便用该命令

打开终端切换到/tmp,运行ls -l

[root@centos701 tmp]# ls -l

总用量 55076

drwxr-xr-x. 3 root root       15 10月 23 21:24 1

drwxr-xr-x. 2 root root        6 10月 23 21:24 123

-rw-r--r--. 1 root root        4 10月 24 21:11 1.txt

drwxr-xr-x. 3 root root       17 10月 24 20:49 aminglinux

以上是目录或文件的详细属性截图,我们默认将 ls 出来的信息划分为9列,其中第1列又分为11个字段

除开第1字段表示类型,11字段表示selinux状态外,2-10字段则表示该文件或者目录权限

3affaabc0b0d0a3bfc5a2aa91a73758e.png-wh_

r=读取属性 值=4

w=写入属性 值=2

x=执行属性  值=1

如/tmp/1.txt该文件的权限的解读是:属主-可读可写(rw-);属组-可读(r--);其他-可读(r--),数字表示为:644。很多时候我们根据需要会对某个文件或目录进行权限的修改,这时候就用到chmod


语法

chmod (选项) (参数)

选项

-c或——changes:效果类似“-v”参数,但仅回报更改的部分;

 -f或--quiet或——silent:不显示错误信息; 

-R或——recursive:递归处理,将指令目录下的所有文件及子目录一并处理;

-v或——verbose:显示指令执行过程;

参数

权限模式:指定文件的权限模式;

文件:要改变权限的文件。

实例

使/tmp/1.txt具有自己可读可写可执行,组用户可读可写,其他用户可读

#chmod 764 /tmp/1.txt


二、更改所有者和所属组chown chgrp

     chown命令改变某个文件或目录的所有者和所属的组,该命令可以向某个用户授权,使该用户变成指定文件的所有者或者改变文件所属的组。用户可以是用户或者是用户D,用户组可以是组名或组id。文件名可以使由空格分开的文件列表,在文件名中可以包含通配符。

    

实例

更改1.txt文件的所属主为aming

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 root root 4 10月 24 21:11 1.txt

[root@centos701 tmp]# chown aming 1.txt

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 aming root 4 10月 24 21:11 1.txt


更改1.txt文件的所属主为user1,所属组为aming

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 aming root 4 10月 24 21:11 1.txt

[root@centos701 tmp]# chown user1:aming 1.txt

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt


更改1.txt文件的属组为root

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt

[root@centos701 tmp]# chown :root 1.txt

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 user1 root 4 10月 24 21:11 1.txt

第2个办法

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 user1 aming 4 10月 24 21:11 1.txt

[root@centos701 tmp]# chgrp root 1.txt

[root@centos701 tmp]# ls -l 1.txt

-rw-r--r--. 1 user1 root 4 10月 24 21:11 1.txt


更改aminglinux目录及其子目录和文件的属主改成user1 属组改成aming

[root@centos701 tmp]# chown -R user1:aming /tmp/aminglinux/

[root@centos701 tmp]# ls -l /tmp/aminglinux/

总用量 0

drwxr-xr-x. 2 user1 aming 19 10月 24 20:59 ls2

[root@centos701 tmp]# ls -ld /tmp/aminglinux/

drwxr-xr-x. 3 user1 aming 17 10月 24 20:49 /tmp/aminglinux/


三、umask

umask是限制新建文件权限的掩码,也就是说它可以用来决定新建文件和目录的权限。

使用方法:umask xxx  如系统默认的是 0002 在设置的时候可以暂时忽略第1个0 即: umask 003

根据目录如果没有x权限就无法浏览,文件没有x权限就无法执行,可得目录最大权限为777,文件最大权限为666

目录权限=777-umask(换算成相应的rwx 逐位去减)

文件权限=666-umask

当 umask=003   时 ,计算目录和文件的权限(注意 rwx - -wx时,其中--x=-,即没有的位数减去有值的位数,依然等于-)

目录权限=777-003=(rwx rwx rwx)-(--- --- --- -wx)=rwx rwx r--=774

文件权限=666-003=(rw- rw- rw-)-(--- --- -wx)=rw- rw- r--=664

操作

[root@centos701 ~]# ls -l      --修改前默认文件和目录的权限分别为 644与755 

总用量 4

-rw-r--r--  1 root root    0 10月 25 22:47 11.txt

drwxr-xr-x  2 root root    6 10月 25 22:48 123

-rw-rw-r--  1 root root    0 10月 25 22:49 12.txt

-rw-------. 1 root root 1431 10月 24 02:55 anaconda-ks.cfg

[root@centos701 ~]# umask

0022

[root@centos701 ~]# umask 003

[root@centos701 ~]# touch 13.txt

[root@centos701 ~]# mkdir 456

[root@centos701 ~]# ls -l      --修改后新建了文件13.txt和目录456的权限分别为 664与774,改之前的文件权限保持不变

总用量 4

-rw-r--r--  1 root root    0 10月 25 22:47 11.txt

drwxr-xr-x  2 root root    6 10月 25 22:48 123

-rw-rw-r--  1 root root    0 10月 25 22:49 12.txt

-rw-rw-r--  1 root root    0 10月 25 23:33 13.txt

drwxrwxr--  2 root root    6 10月 25 23:33 456

-rw-------. 1 root root 1431 10月 24 02:55 anaconda-ks.cfg


四、隐藏权限lsattr_chattr

chattr命令用来改变文件属性;lsattr命令用来查看文件属性。文件和目录属性共有以下8种模式

a:让文件或目录仅供附加用途; (log日志的重定向,防止日志被篡改)

b:不更新文件或目录的最后存取时间; 

c:将文件或目录压缩后存放; 

d:将文件或目录排除在倾倒操作之外; 

i:不得任意更动文件或目录;     (重要文件可以使用防止修改删除)

s:保密性删除文件或目录; 

S:即时更新文件或目录; 

u:预防意外删除。


4.1.chattr

语法

chattr (选项)

-R:递归处理,将指令目录下的所有文件及子目录一并处理;

 -v<版本编号>:设置文件或目录版本; 

-V:显示指令执行过程; 

+<属性>:开启文件或目录的该项属性; 

-<属性>:关闭文件或目录的该项属性; 

=<属性>:指定文件或目录的该项属性。


4.2.lsattr

语法

lsattr (选项)

-E:可显示设备属性的当前值,但这个当前值是从用户设备数据库中获得的,而不是从设备直接获得的。

-D:显示属性的名称,属性的默认值,描述和用户是否可以修改属性值的标志。

-R:递归的操作方式;

-V:显示指令的版本信息; 

-a:列出目录中的所有文件,包括隐藏文件。

-D,-E,-R这三个选项不可以一起使用,互相排斥


实例

1 chattr

[root@centos701 ~]# chattr +i 1.txt    --如果需要取消则可以输入chattr -i  1.txt

[root@centos701 ~]# vi 1.txt

编辑1.txt时会提示,你正在更改一个只读文件,无论使用任何办法都无法保存

c6f12d58f1233f80d84fe3987a73d08a.png-wh_

2.lsattr 

[root@centos701 ~]# lsattr -R     --加上-R可以列出所有目录及子目录和文件

[root@centos701 ~]# lsattr -R

---------------- ./anaconda-ks.cfg

---------------- ./11.txt

---------------- ./123

./123:

---------------- ./12.txt

---------------- ./1234

./1234:

---------------- ./13.txt

---------------- ./456

./456:

----i----------- ./1.txt

---------------- ./111

./111:

---------------- ./111/12.txt

发现1.txt这里有多了一个"i",其他都是-