6.6 2.14-2.17

2.14 文件或目录权限chmod

[root@hyc-01-01 ~]# ls -l

总用量 8

-rw-r--r--. 1 root root    0 6   5 21:14 2.txt

-rw-------. 1 root root 6716 6   5 22:49 anaconda-ks.cfg.1

-rw-r--r--. 1 root root    0 6   5 21:17 ls2

从左到右第一位为文件类型,后面9位为文件权限位;

r代表可读,w表示可写,x为可执行;

第一个3位组表示所有者权限,第二个3位组表示所属组,第三个3位组表示其他用户;

 

扫描二维码关注公众号,回复: 1509376 查看本文章

用数字表示权限:

r=4,w=2,x=1;用户权限即为这三个值之和;

r--=4,rw-=6,rwx=7

[root@hyc-01-01 ~]# ls -l

总用量 8

-rw-r--r--. 1 root root    0 6   5 21:14 2.txt

-rw-------. 1 root root 6716 6   5 22:49 anaconda-ks.cfg.1

-rw-r--r--. 1 root root    0 6   5 21:17 ls2

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

-rwx------. 1 root root 0 6   5 21:14 2.txt

当开启selinux时,创建的每个文件第一组的最后一位会出现.

 

Selinux

三种状态:

Enforce:对于不符合策略的访问行为进行彻底的限制

Permissive:当访问行为不符合策略时会生成信息,但不会真正限制访问

Disable:此时selinux处于关闭状态

[root@hyc-01-01 ~]# getenforce 获得selinux状态信息

Enforcing

[root@hyc-01-01 ~]# setenforce 0 不彻底的关闭,仍会出现告警,系统重启后失效

[root@hyc-01-01 ~]# getenforce

Permissive

彻底关闭selinux

[root@hyc-01-01 ~]# vi /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

#     enforcing - SELinux security policy is enforced.

#     permissive - SELinux prints warnings instead of enforcing.

#     disabled - No SELinux policy is loaded.

SELINUX=disable 修改为disable会彻底关闭selinux

# SELINUXTYPE= can take one of three two values:

#     targeted - Targeted processes are protected,

#     minimum - Modification of targeted policy. Only selected processes are protected.

#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

-R 一次性统一修改目录及目录下文件及子目录的权限

[root@hyc-01-01 tmp]# ls -l hyc2

总用量 0

drwxr-xr-x. 3 root root 19 6   3 17:36 1

drwx------. 4 root root 82 6   5 08:04 hyc1

[root@hyc-01-01 tmp]# chmod -R 770 hyc2

[root@hyc-01-01 tmp]# ls -l hyc2 目录及其子目录拥有相同的权限

总用量 0

drwxrwx---. 3 root root 19 6   3 17:36 1

drwxrwx---. 4 root root 82 6   5 08:04 hyc1

[root@hyc-01-01 tmp]# ls -ld hyc2

drwxrwx---. 4 root root 27 6   5 21:26 hyc2

 

用字母表示修改的权限:

[root@hyc-01-01 tmp]# ls -ld hyc1 修改前的权限

drwxr-xr-x. 2 root root 6 6   5 21:31 hyc1

[root@hyc-01-01 tmp]# chmod u=rwx,g=,o= hyc1 修改权限,等号后面不能加-

[root@hyc-01-01 tmp]# ls -ld hyc1

drwx------. 2 root root 6 6   5 21:31 hyc1 修改后

[root@hyc-01-01 tmp]#

u表示所有者,g表示所有组,o表示其他用户,a表示所有用户;

 

仅针对部分权限进行增删:

[root@hyc-01-01 tmp]# chmod a-x hyc1

[root@hyc-01-01 tmp]# ls -ld hyc1

drw-------. 2 root root 6 6   5 21:31 hyc1

 

2.15 更改所有者和所属组chown

修改文件的所有者

[root@hyc-01-01 tmp]# chown hyc /tmp/1.txt

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

-rw-r--r--. 1 hyc root 883 6   5 07:44 1.txt

修改文件的所属组

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

-rw-r--r--. 1 hyc root 883 6   5 07:44 1.txt

[root@hyc-01-01 tmp]# chgrp hyc 1.txt

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

-rw-r--r--. 1 hyc hyc 883 6   5 07:44 1.txt

修改所有者及所属组

[root@hyc-01-01 tmp]# chown root:root 1.txt

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

-rw-r--r--. 1 root root 883 6   5 07:44 1.txt

chown仅修改属组

[root@hyc-01-01 tmp]# chown :hyc 1.txt

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

-rw-r--r--. 1 root hyc 883 6   5 07:44 1.txt

更改当前目录及其子目录和文件的所有者和所属组

[root@hyc-01-01 tmp]# chown -R hyc:hyc hyc2

[root@hyc-01-01 tmp]# ls -l hyc2

总用量 0

drwxrwx---. 3 hyc hyc 19 6   3 17:36 1

drwxrwx---. 4 hyc hyc 82 6   5 08:04 hyc1

[root@hyc-01-01 tmp]# ls -ld hyc2

drwxrwx---. 4 hyc hyc 27 6   5 21:26 hyc2

2.16 umask

所有文件或目录产生的初始权限均与umask有关

[root@hyc-01-01 tmp]# touch 1.txt

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

-rw-r--r--. 1 root hyc 883 6   6 23:24 1.txt 文件默认权限为644

[root@hyc-01-01 tmp]# mkdir 1

[root@hyc-01-01 tmp]# ls -ld 1

drwxr-xr-x. 2 root root 6 6   6 23:24 1 目录默认权限为755

 

[root@hyc-01-01 tmp]# umask 查看umask

0022

[root@hyc-01-01 tmp]# umask 0002 修改umask

[root@hyc-01-01 tmp]# umask

0002

 

[root@hyc-01-01 tmp]# touch 2.txt

[root@hyc-01-01 tmp]# ls -l 2.txt

-rw-rw-r--. 1 root root 0 6   6 23:25 2.txt 修改umask值后的默认文件权限664

[root@hyc-01-01 tmp]# mkdir 2

[root@hyc-01-01 tmp]# ls -ld 2

drwxrwxr-x. 2 root root 6 6   6 23:25 2 变更后的目录权限775

文件的初始权限为666-umask的后3位;

目录的初始权限为777-umask3位;

x权限对于目录表示是否可以进入目录,对于文件x权限表示是否可以执行;

 

基于umask获得的初始权限计算方法:

umask=0003

666-003=(rw-rw-rw-)-(-------wx)=rw-rw-r--=664

777-003=(rwxrwxrwx)-(-------wx)=rwxrwxr--=774

 

2.17 隐藏权限lsattr_chattr

ls –l无法查看隐藏权限

 

i权限

[root@hyc-01-01 tmp]# chattr +i 1.txt 为文件1.txt增加隐藏权限i

增加该权限后文件无法被执行写操作、删除、改名、修改文件时间

[root@hyc-01-01 tmp]# lsattr 1.txt 查看隐藏权限

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

出现2.txt~的原因

[root@hyc-01-01 ~]# ls

2.txt  anaconda-ks.cfg.1  ls2

[root@hyc-01-01 ~]# chattr +i 2.txt

[root@hyc-01-01 ~]# lsattr 2.txt

----i----------- 2.txt

[root@hyc-01-01 ~]# vi 2.txt 无法写入

[root@hyc-01-01 ~]# ls

2.txt  2.txt~  anaconda-ks.cfg.1  ls2

[root@hyc-01-01 ~]# touch 2.txt 已经存在2.txt时再touch会修改文件的创建时间,i权限不允许

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

[root@hyc-01-01 ~]#

通常用户写入数据到文件时会创建一个与该文件一样的缓存文件,将要修改和新增的内容写入缓存文件,再在保存退出时用缓存文件将原文件覆盖以向原文件写数据,然后会将缓存文件删除;

当文件有隐藏权限i时,由于缓存文件无法向文件写数据,导致保存不成功,所以系统也不会删除缓存文件,ls命令看到的带~2.txt文件即2.txt产生的缓存文件;

[root@hyc-01-01 ~]# chattr -i 2.txt

[root@hyc-01-01 ~]# rm -f 2.txt 去掉i权限后,可以正常删除

 

a权限 只能追加,不能删除和更改,类似log日志

[root@hyc-01-01 ~]# chattr +a 3.txt

[root@hyc-01-01 ~]# lsattr 3.txt

-----a---------- 3.txt

[root@hyc-01-01 ~]# rm -f 3.txt

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

[root@hyc-01-01 ~]# vi 3.txt 无法写入

[root@hyc-01-01 ~]# mv 3.txt 4.txt 无法重命名

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

[root@hyc-01-01 ~]# head -3 /etc/passwd >> 3.txt 可以追加

[root@hyc-01-01 ~]# touch 3.txt 可以更改时间信息

 

查看目录的隐藏权限

[root@hyc-01-01 ~]# mkdir 111

[root@hyc-01-01 ~]# mkdir 111/222

[root@hyc-01-01 ~]# lsattr 111 此时查看的是111目录下的子目录的隐藏权限

---------------- 111/222

[root@hyc-01-01 ~]# lsattr -d 111 查看111本身的权限

---------------- 111

给目录添加i权限

[root@hyc-01-01 ~]# chattr +i 111

[root@hyc-01-01 ~]# rm -rf 111 无法删除目录本身及其中的子目录和文件

rm: 无法删除"111/222": 权限不够

[root@hyc-01-01 ~]# mv 111 1222 无法改名

mv: 无法将"111" 移动至"1222": 不允许的操作

[root@hyc-01-01 ~]# touch 111/222.txt 无法在目录中添加新文件和目录

touch: 无法创建"111/222.txt": 权限不够

给目录添加a权限

[root@hyc-01-01 ~]# lsattr -d 111

-----a---------- 111

[root@hyc-01-01 ~]# vi 111/12.txt

[root@hyc-01-01 ~]# cat 111/12.txt

Dddddddddddddddddddddd 给目录加a权限,目录下的文件依然可以写入

[root@hyc-01-01 ~]# cd 111

[root@hyc-01-01 111]# lsattr -d .

-----a---------- .

[root@hyc-01-01 111]# mkdir 2

[root@hyc-01-01 111]# ls

12.txt  12_txt.swp  2  22  222

[root@hyc-01-01 111]# rm -rf 2

rm: 无法删除"2": 不允许的操作

[root@hyc-01-01 111]# mv 2 2a

mv: 无法将"2" 移动至"2a": 不允许的操作

[root@hyc-01-01 111]# touch 12.txt 可以修改目录下子文件的时间

[root@hyc-01-01 ~]# chattr +i 111

[root@hyc-01-01 ~]# lsattr -d 111

----i----------- 111

[root@hyc-01-01 ~]# ls

111  222  3.txt  anaconda-ks.cfg.1  ls2

[root@hyc-01-01 ~]# vi 111/12.txt

[root@hyc-01-01 ~]# cat !$

cat 111/12.txt

dddddddddddddddddddddd 给目录加ia权限不会影响目录下文件内容的改写

dsfafdg

lsattr –R

[root@hyc-01-01 ~]# lsattr 111 默认只会查看第一层目录的子目录和文件的隐藏权限

---------------- 111/222

---------------- 111/22

---------------- 111/12_txt.swp

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

[root@hyc-01-01 ~]# lsattr -R 111 使用-R参数后会查看该目录下所有目录及文件

---------------- 111/222

111/222:

---------------- 111/222/aaa

---------------- 111/22

---------------- 111/12_txt.swp

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

-a

[root@hyc-01-01 ~]# lsattr -a 111 查看包括隐藏文件在内的文件的隐藏权限

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

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

---------------- 111/222

---------------- 111/22

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

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

---------------- 111/12_txt.swp

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

i权限:

文件:无法被执行写操作、删除、改名、修改时间信息

目录:无法增删目录中的文件和子目录,无法改名,可以修改子文件的时间信息

a权限:

文件:无法写入、删除、重命名,可以追加内容及更改时间信息

目录:可以创建新的子目录或文件,不能修改原文件或子目录的名称,无法删除原来的文件或目录,可以修改子文件时间信息


猜你喜欢

转载自blog.51cto.com/12216458/2125799
6.6