/文件和目录权限chmod /更改所有者和所属组chown/umask/隐藏权限lsattr/chattr

2.14 文件和目录权限chmod 
2.15 更改所有者和所属组chown
2.16 umask
2.17 隐藏权限lsattr/chattr

文件和目录权限chmod 

文件权限:

r     4       可读

w    2      可写

x     1      可执行

r--         4

rw-       6

rwx      7

 

passwd的权限是644

[root@centos_1 ~]# ls -l /etc/passwd

-rw-r--r--. 1 root root 889 11月 16 22:53 /etc/passwd

 

9位权限分三段:

rw- 所有者的权限

r--   所属主的权限

r--  其它用户的权限

 

更改权限:

chmod  (change mode)

chmod  700  文件名称

[root@centos_1 ~]# chmod 700 1.txt 

[root@centos_1 ~]# ls -l 1.txt 

-rwx------. 1 root root 0 11月 17 09:10 1.txt

 

权限中的点"." 表示该文件受制于selinux

selinux限制用户的行为,如果做认证,selinux开启就不会让你登录。

如果selinux开启了,创建文件或者目录的权限后面会有一个点"."

查看selinux是否开启:getenforce

临时关闭selinux   enforce 0

永久关闭selinux:修改配置文件

vi /etc/selinux/config

SELINUX=disabled

 

这样关闭selinux后创建文件或者目录权限最后就没有点"."

[root@centos_1 ~]# touch xiao.txt

[root@centos_1 ~]# ls -l xiao.txt 

-rw-r--r-- 1 root root 0 11月 20 20:25 xiao.txt

 

chmod -R 批量更改目录下的文件权限:

chmod  -R   770 xiaobo

 

 

设置权限的写法:

chmod  u=rwx,g=r,o=r   xiaobo

给所有者所属主其它用户的权限加上x权限:

chmod  a+x xiaobo (a表示all所有)

chmod  a-x  xiaobo 都去掉x权限

也可以单独添加或者减去权限:

chmod  u-x  xiaobo

chmod  u+x xiaobo

chmod  g+x xiaobo

chmod  g-x xiaobo

chmod  o+x xiaobo

chmod  o-x xiaobo

[root@centos_1 ~]# chmod u+x,g+x 1.tx

[root@centos_1 ~]# ls -l 1.tx

-rwxr-xr--. 1 root root 0 11月 17 09:18 1.tx

 

更改所有者和所属组chown

chown  (change owner) 更改文件所有者

[root@centos_1 ~]# chown xiaobo anaconda-ks.cfg.1 

[root@centos_1 ~]# ls -l anaconda-ks.cfg.1 

-rw-------. 1 xiaobo root 3360 11月 17 08:43 anaconda-ks.cfg.1

 

更改所属组: chgrp (change group)

chgrp xiaobo

[root@centos_1 ~]# chgrp xiaobo 1.tx

[root@centos_1 ~]# ls -l 1.tx

-rwxr-xr--. 1 root xiaobo 0 11月 17 09:18 1.tx

 

 

一次性更改所有者和所属主:

chown  xiaoming:xiaobo  1.txt

[root@centos_1 ~]# chown xiaoming:xiaobo 11.t 

[root@centos_1 ~]# ls -l 11.t 

-rw-r--r--. 1 xiaoming xiaobo 0 11月 17 09:19 11.t

 

只单独改所属主:

chown    :xiaobo  12.txt

[root@centos_1 ~]# chown :xiaobo 12.txt 

[root@centos_1 ~]# ls -l 12.txt 

-rw-r--r-- 1 root xiaobo 0 11月 20 20:53 12.txt

 

 

chown root:  2.txt 一次更改所有者和所属主:

[root@centos_1 ~]# chown root: 2.txt 

[root@centos_1 ~]# ls -l 2.txt 

-rw-r--r--. 1 root root 0 11月 17 09:17 2.txt

连续批量更改所有者和所属主

chown -R   xiaoming:xiaobo /tmp/

[root@centos_1 /]# chown -R xiaoming:xiaobo /tmp/*

[root@centos_1 /]# ls -l /tmp/

drwx------. 3 xiaoming xiaobo     97 11月 17 05:38 xiaobo

drwx------. 3 xiaoming xiaobo     86 11月 17 05:32 xiaobo1

drwxr-xr-x. 2 xiaoming xiaobo     19 11月 17 05:45 xiaobo2

 

umask

目录 777 -umask = 目录权限

文件666 -umask = 文件权限

umask  决定默认文件和目录的权限

用9位的权限位来减 umask 就是文件权限

 

系统umask的值是0022

[root@centos_1 ~]# umask

0022

 

通过这个值可以确定目录和文件的权限:

目录 777 (rwxrwxrwx)- 022(----w--w-) =755(rwxr-xr-x)

文件 666(rw-rw-rw)- 022(----w--w-)= 644(rw-r-r--)

 

更改权限 umask  002(权限值)

更改之后创建的文件权限就会改变。

隐藏权限lsattr/chattr

chattr  (change file attributes) 更改文件隐藏权限

 chattr - change file attributes

[root@centos_1 ~]# chattr +i 1.txt 

会隐藏文件的权限: 查看隐藏权限lsattr 1.txt

[root@centos_1 ~]# lsattr 1.txt 

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

 

看到的权限是:

[root@centos_1 ~]# ls -l 1.txt 

-rw-r--r-- 1 root root 0 11月 20 21:50 1.txt

 

实际上只有一个i权限

加了i权限后文件不能 touch更改文件时间,不能写,不能移动,不能删除,不能追加内容

chattr 1.txt

[root@centos_1 ~]# mv 1.txt /

mv: 无法将"1.txt" 移动至"/1.txt": 不允许的操作

 

[root@centos_1 ~]# touch 1.txt 

touch: 无法创建"1.txt": 权限不够

[root@centos_1 ~]# rm -f 1.txt 

rm: 无法删除"1.txt": 不允许的操作

去掉i权限:

chattr -i  1.txt

[root@centos_1 ~]# chattr -i 1.txt 

 

 

chattr +a 1.txt

文件可以追加,可以touch更改文件时间信息,不能移动 删除 重命名;

 

去掉权限

chattr -a 1.txt

文件恢复权限

 

给目录设置隐藏权限: chattr + i 11/

给目录设置i权限跟设置文件权限是一样的.

[root@centos_1 ~]# chattr +i 11/

查看隐藏权限 lsattr -d 11/

[root@centos_1 ~]# lsattr -d 11/

----i----------- 11/

 

 

给目录加入i 权限,但是可以往改目录下的文件写入内容:

先在目录下创建文件,之后给目录设置i权限,可以往目录下的文件写入内容

chattr +i 11/

head -n2 /etc/passwd  > 11/1.txt

[root@centos_1 ~]# touch 11/1.txt

[root@centos_1 ~]# chattr +i 11/

[root@centos_1 ~]# head -n2 /etc/passwd > 11/1.txt 

 

 

lsattr -R  显示所有文件(包括子目录下的文件)

[root@centos_1 ~]# lsattr -R

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

----i----------- ./11

./11:

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

 

lsattr -a 查看隐藏的

lsattr  -d  只查看目录

原文地址:https://www.cnblogs.com/zhaocundang/p/8076176.html