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

一、文件或目录权限chmod

1.   一个文件有三个权限位,"r"表示可读,"w"表示可写,"x"表示可执行,"-"表示不可以

[root@localhost ~]# ls -al
dr-xr-x---.  4 root root  157 12月 19 22:46 .
drwxr-xr-x. 17 root root  224 12月 14 10:03 ..



2. 权限是可以用数字代替的,r=4 ,w=2 ,x=1,rwx就是这三个数字相加:r+w+x=4+2+1=7 ,“-”表示0

[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 1418 11月  9 14:16 anaconda-ks.cfg
drwxr-xr-x. 3 root root   29 12月 19 22:46 qw                #可以看到qw这个目录的权限是:rwxr-xr-x,那么用数字表示就是:766
 
3. chmod ,change mode的意思,用来更改文件的权限

[root@localhost tmp]# ls -l
总用量 8
-rw-r--r--. 1 root root    0 12月 20 10:05 11.txt   # 可以看到11.txt的权限是rw-r--r--,数字表示为644,
drwxr-xr-x. 3 root root   30 12月 20 10:07 33

要更改11.txt只有文件的所有者可读可写可执行,所属用户组和其他人不可以读写执行,用字母表示为rwx------,数字表示为700,执行以下操作:

[root@localhost tmp]# chmod 700 11.txt                     #执行chmod ,更改11.txt的权限
[root@localhost tmp]# ls -l
总用量 8
-rwx------. 1 root root    0 12月 20 10:05 11.txt            #文件的权限变为rwx------,数字表示700
drwxr-xr-x. 3 root root   30 12月 20 10:07 33

4. 文件的权限位最后一位还有一个"."  ,表示文件受制于SElinux,SElinux开启的时候创建文件就会出现这个".",只有在SElinux完全关

闭的时候才不会有



使用 setenforce 0关闭的SElinux状态为:Permissive,实际上在开启,但不会限制,只是打印一个警告,要彻底关闭需要更改配置文件:/etc/selinux/config
[root@localhost tmp]# setenforce 0
[root@localhost tmp]# getenforce
Permissive
5. chmod -R 批量更改文件的权限,目录和目录下的文件都会被更改
[root@localhost tmp]# ls -ld 33                               #  查看33的权限为rwxr-xr-x
drwxr-xr-x. 3 root root 30 12月 20 10:07 33
[root@localhost tmp]# ls -l 33                                #33目录下的文件权限11.txt为rw-r--r--,目录33为rwxr-xr-x
总用量 0
-rw-r--r--. 1 root root 0 12月 20 10:07 11.txt
drwxr-xr-x. 2 root root 6 12月 20 10:06 33

[root@localhost tmp]# chmod -R 777 33            #使用-R更改权限,777
 
[root@localhost tmp]# ls -l 33                               #查看33这个目录和目录下的目录文件都变成了777
总用量 0
-rwxrwxrwx. 1 root root 0 12月 20 10:07 11.txt
drwxrwxrwx. 2 root root 6 12月 20 10:06 33

6. chmod也可以使用字母来更改权限, u表示文件所有者,g表示用户组、o表示其他人

[root@localhost tmp]# chmod u=rwx,g=rx,o=x ss.txt    #u表示user,拥有者、g表示用户组、o表示其他人
[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 root root 35 12月 20 10:18 ss.txt

7. chmod a+-也可以用来改变权限,a表示all,所有的,文件所有者(u)、文件所属组(g)、其他人(o),当然也可以使用u+-,g+-。

[root@localhost tmp]# chmod a-x test/                      #a-x表示所有人的权限(u,g,x)都减去x,把test目录所有人的权限减去x
[root@localhost tmp]# ls -ld test/
drw-r--r--. 4 root root 50 12月 20 10:02 test/
[root@localhost tmp]# chmod a+x test/                     #a+x表示所有人的权限(u,g,x)都加上x,把test目录所有人的权限加上x
[root@localhost tmp]# ls -ld test/
drwxr-xr-x. 4 root root 50 12月 20 10:02 test/

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

1.  chown  ==  change owenr 更改所有者  

[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 root root 35 12月 20 10:18 ss.txt            #ss.txt文件的所有者是root
[root@localhost tmp]# chown lx01 ss.txt                     #将ss.txt文件的所有者更改为lx01
[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 lx01 root 35 12月 20 10:18 ss.txt

2.   chgrp  == change group 更改所属组

[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 lx01 root 35 12月 20 10:18 ss.txt      #ss.txt文件的所属组是root
[root@localhost tmp]# chgrp lx02 ss.txt                #将ss.txt文件的所属组更改为lx02
[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 lx01 lx02 35 12月 20 10:18 ss.txt

3. chown [所有者]:[所属组]       所有者和所属组之间用":"分隔,不仅可以改用户,也可以更改用户组

[root@localhost tmp]# chown root:root ss.txt               #ss.txt文件的所属组和所有者都更改为root
[root@localhost tmp]# ls -l ss.txt
-rwxr-x--x. 1 root root 35 12月 20 10:18 ss.txt
chown也是可以只更改所属组的,将前面的所有者省略即可, chown :[所属组]
[root@localhost tmp]# chown :lx02 ss.txt                 #省略掉所有者
[root@localhost tmp]# ls -l ss.txt                               
-rwxr-x--x. 1 root lx02 35 12月 20 10:18 ss.txt     #可以看到只更改了所属组
4.chown -R ,可以批量更改目录的所有者和所属组,目录以及目录下的文件都会被更改

三、umask

1.当我们创建一个目录或者文件的时候都会有一个默认的权限,那么这个默认权限是怎么来的呢?
[root@localhost tmp]# touch as.txt
[root@localhost tmp]# ls -l as.txt
-rw-r--r--. 1 root root 0 12月 20 15:06 as.txt              创建一个as.txt文件,默认权限为644
[root@localhost tmp]# mkdir lx
[root@localhost tmp]# ls -ld lx
drwxr-xr-x. 2 root root 6 12月 20 15:07 lx                   创建一个lx目录,默认权限为755
2. umask ,umask就是指定目前用户在新建文件或者目录时候的默认权限值。
[root@localhost tmp]# umask        
0022                                        umask为022,最前面的一位为特殊权限,只需要看后面三位就可以
[root@localhost tmp]# umask 002           #可以通过这种方式更改umask
[root@localhost tmp]# umask
0002                                                            #umask已经更改为002
3. 当我们进入目录的时候,实际上就是在执行这个目录,所以目录必须要有x权限才能打开

4. 文件和目录权限的计算方式,用文件的权限(666)或者目录的权限(777)换算成字母减去umask

    如果umask为022(----w--w-),那么新建文件和目录的默认权限计算方式如下:

    文件(666)-(022)=(rw-rw-rw-)-(----w--w- ) =rw-r--r--(644)
 
    目录(777)-(022)=(rwxrwxrwx)-(----w--w-) = rwxr-xr-x(755) 

四、隐藏权限lsattr_chattr

1.   chattr +i         给文件加上 i 隐藏权限,不能写,移动,touch,追加,删除,改名

      chattr -i         给文件取消i权限
[root@localhost tmp]# chattr +i 22.txt                     #给22.txt加上i权限
[root@localhost tmp]# cat /etc/passwd > 22.txt             
-bash: 22.txt: 权限不够                                                   #对22.txt进行写入的时候,提示权限不够
[root@localhost tmp]# chattr -i 22.txt                       #把22.txt的i权限取消
[root@localhost tmp]# cat /etc/passwd > 22.txt      #可以正常写入
 2.   lsattr    查看文件的隐藏权限
[root@localhost tmp]# lsattr 22.txt
----i----------- 22.txt              #可以看到22.txt有一个i隐藏权限
3.  chattr +a      给文件加上a权限,a权限只能只能追加、更改时间信息(touch),其它的不能操作

     chattr -a       把文件取消i权限

​4. lsattr -d    lsattr查看的是目录下面的文件,lsattr -d可以查看目录本身
[root@localhost tmp]# lsattr -d
---------------- .
5. 给目录加上i和a权限是和文件一样的,但是目录下已经存在的文件是可以进行写的

6.lsattr -R 可以查看目录下的所有子目录和文件(不包括隐藏)的隐藏权限

    lsattr -a  可以查看隐藏文件的隐藏权限




友情链接:阿铭Linux

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值