复习(day8)

2.14 文件和目录权限chmod

文件属性

[root@hf ~]# ls -l
总用量 12
-rw-r--r--. 1 root root    0 3月 27 16:06 1.txt
-rw-------. 1 root root 1422 3月 21 00:17 anaconda-ks.cfg
  • -rw-r–r–. 1 root root 0 10月 25 16:06 1.txt
    • -表示文件的类型,rw-r–r–后面的九位,表示文件的权限
      • r (read)表示可读权限 –数字4表示,r=4
      • w (write)表示可写权限 –数字2表示,w=2
      • x (excute)表示可执行权限 –数字1表示,x=1
    • 总结:rwx=7 rw-=6 –x=1 rw-r–r–=644 rw-r-xr-x=655
  • -rw-r–r–. 1 root root 0 10月 25 16:06 1.txt

    • rw-表示第一段,user所有者的权限
    • r–表示第二段,group所属组的权限
    • r–表示第三段,others其他用户权限
    • 【点.】 有的文件有点,有的没有,意味这个文件受制于SELinux,如果selinux开启,创建的文件或目录在这个位置就会有点
    • 数字1,则表示 相同inode的文件数,与目录下子目录数有关
    • root(第一个),表示文件所属主 ,文件所有者
    • root(第二个),表示文件所属组
    • 0(数字),表示文件大小
    • 25 16:06(时间),表示文件最后一次修改的时间
    • 1.txt,表示文件 (这里可以是目录或文件)
  • chmod等于change mode

    • 用于改变用户对文件或目录的读写执权限
    • chmod -R 表示可以批量更改目录本身以及目录下的子目录和文件的权限
[root@hf ~]# ls -l
总用量 8
-rw-r--r--. 1 root root 924 1025 06:49 2.txt
-rw-------. 1 root root 973 821 05:05 anaconda-ks.cfg.1
[root@hf ~]# chmod 700 2.txt     
[root@hf ~]# ls -l 2.txt
-rwx------. 1 root root 924 1025 06:49 2.txt     
[root@hf ~]# getenforce  
[root@hf ~]# setenforce 0       

若想永久关闭防火墙,则需要更改配置文件
[root@hf-01 ~]# vi /etc/selinux/config     

只有关闭了selinux,-rwx------. 最后的这个点才会消失
[root@hf ~]# mkdir hf/   新建目录hf/
[root@hf ~]# ls
2.txt  anaconda-ks.cfg.1  hf
[root@hf ~]# cd hf/
[root@hf hf]# touch 1.txt       新建文件1.txt
[root@hf hf]# ls 
1.txt
[root@hf hf]# ls -l 
总用量 0
-rw-r--r--. 1 root root 0 1026 06:56 1.txt
[root@hf hf]# cd
[root@hf ~]# chmod 770 1.txt
chmod: 无法访问"1.txt": 没有那个文件或目录      这是因为1.txt在目录hf/下面
[root@hf ~]# chmod 770 hf/      更改hf/文件夹的权限
[root@hf ~]# ls -l hf/          会发现里面的1.txt权限没有发生变化
总用量 0
-rw-r--r--. 1 root root 0 1026 06:56 1.txt
[root@hf ~]# ls -ld hf/         而文件夹的权限则发生了变化
drwxrwx---. 2 root root 18 1026 06:56 hf/
[root@hf ~]# chmod -R 661 hf/     在加上了-R选项,文件和目录和子目录批量的更改了权限
[root@hf ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 1026 06:56 1.txt
[root@hf ~]# ls -ld hf/
drw-rw---x. 2 root root 18 1026 06:56 hf/
  • u 表示user
  • g 表示group
  • o 表示others
  • a 表示all(全部)
    • 如:u+(-)rwx,g+(-)rwx,o+(-)rwx 如果更改多个属性,中间可用“,”隔开。
    • 又如:a+(-)rwx
[root@hf ~]# chmod u=rwx,g=w,o=r hf/       
[root@hf ~]# ls -ld hf/
drwx-w-r--. 2 root root 18 1026 06:56 hf/
[root@hf ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 1026 06:56 1.txt
[root@hf ~]# chmod a+x hf/      
[root@hf ~]# ls -ld hf/
drwx-wxr-x. 2 root root 18 1026 06:56 hf/
[root@hf ~]# chmod o+w hf/      
[root@hf ~]# ls -ld hf/
drwx-wxrwx. 2 root root 18 1026 06:56 hf/
[root@hf ~]# chmod a-w hf/      
[root@hf ~]# ls -ld hf/
dr-x--xr-x. 2 root root 18 1026 06:56 hf/


  • 总结

在Linux系统中,目录的默认权限为755,文件的默认权限为644

2.15 更改所有者和所属组chown

  • chown命令
    • chown等于change owner,更改文件的所有者和所属组
[root@hf ~]# ls /tmp
aminglinux  amning  mysql.sock  yum.log
[root@hf ~]# ls -l /tmp/yum.log        
-rw-r--r--. 1 root root 0 326 07:48 /tmp/yum.log
[root@hf ~]# chown aming /tmp/yum.log 
chown: 无效的用户: "aming"             
[root@hf ~]# chown hanfeng /tmp/yum.log    
[root@hf ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng root 0 326 07:48 /tmp/yum.log
  • chown -R username:group filename
  • chown 【-R】用户名 文件名 /更改所属主
  • chown 【-R】 用户名:组名 文件名 /更改所属主和所属组
[root@hf ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 1026 07:48 /tmp/yum.log
[root@hf ~]# chown user1:hanfeng /tmp/yum.log
[root@hf-01 ~]# !ls     
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 hanfeng 0 1026 07:48 /tmp/yum.log
[root@hf ~]# chown -R hanfeng:user1 /tmp/aminglinux/
[root@hf ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 1024 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 1025 06:55 aming2
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 1024 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 1025 06:55 aming2
[root@hf ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 hanfeng user1 27 1025 07:29 /tmp/aminglinux/
[root@hf ~]# touch /tmp/aminglinux/3.txt
[root@hf ~]# chown -R user1:hanfeng /tmp/aminglinux/
[root@hf ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 user1 hanfeng 18 1024 07:21 2
-rw-r--r--. 1 user1 hanfeng  0 1026 08:23 3.txt
drwxr-xr-x. 4 user1 hanfeng 31 1025 06:55 aming2
[root@hf ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 user1 hanfeng 39 1026 08:23 /tmp/aminglinux/
  • chown 【-R】 :组名 文件名 / 只更改所属组
[root@hf ~]# chown :root /tmp/yum.log
[root@hf ~]# !ls     
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 root 0 1026 07:48 /tmp/yum.log
  • chgrp命令
    • chgrp等于change group 更改文件所属的用户组
[root@hf ~]# chgrp user1 /tmp/yum.log
[root@hf ~]# !ls     
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 326 07:48 /tmp/yum.log


  • chgrp 【-R】 组名 文件名

-R 只用于目录,作用是级联更改子目录以及子文件。

2.16 umask

  • umask命令,通过这个值可以确定文件和目录的默认权限是什么。
    • 默认情况下,目录的权限值为755(rwxr-xr-x),普通文件的默认权限为644(-rw-r–r–),umask默认值为0022(—-w–w-)
[root@hf ~]# touch 11.txt
[root@hf ~]# ls -l 11.txt
-rw-r--r--. 1 root root 0 326 08:39 11.txt
[root@hf ~]# mkdir 123
[root@hf ~]# ls -ld 123
drwxr-xr-x. 2 root root 6 326 08:39 123
[root@hf ~]# umask  
0022
[root@hf ~]# umask 002   
[root@hf ~]# touch 33.txt
[root@hf ~]# ls -l 33.txt       
-rw-rw-r--. 1 root root 0 326 08:56 33.txt
[root@hf ~]# mkdir 234
[root@hf ~]# ls -ld 234         
drwxrwxr-x. 2 root root 6 326 08:57 234
  • 所以在创建目录或者文件的时候,文件或目录的权限是通过:

    • 在创建的目录或者文件的权限=默认值(文件为666 rw-rw-rw,目录为777 rwxrwxrwx)-umask的值
      得来的。
  • 若用户创建普通文件。则预设没有可执行权限,只有rw两个权限,最大值为666(-rw-rw-rw)

  • 若用户建立目录,则预设开放所有权限,最大值777(rwxrwxrwx)
  • umask算法
当umask=003
目录的权限:777(rwxrwxrwx)-003(-------wx)=774(rwxrrxr--)
普通文件的权限:666(rw-rw-rw--003(-------wx)=664(rw-rw-r--)
--x减去--w依然是什么都没有

2.17 隐藏权限lsattr/chattr

  • chattr命令
    • chattr等于change attribute 附加权限
[root@hf ~]# chattr +i 33.txt      
[root@hf ~]# vi 33.txt          
[root@hf ~]# head -n2 /etc/passwd > 33.txt
-bash: 33.txt: 权限不够     
[root@hf ~]# ls -l 33.txt       
-rw-rw-r--. 1 root root 0 1026 08:56 33.txt
[root@hf ~]# lsattr 33.txt      
----i----------- 33.txt
[root@hf ~]# touch ha.txt
[root@hf ~]# lsattr ha.txt       
---------------- ha.txt
Try 'mv --help' for more information.
[root@hf-01 ~]# mv 33.txt 56.txt        
[root@hf-01 ~]# rm 33.txt               
rm:是否删除普通空文件 "33.txt"?y
rm: 无法删除"33.txt": 不允许的操作
  • chattr命令,它是一个非常严谨的权限
    • chattr +i 则为文件增加了隐藏属性
      • 若是有一个文件使它无法追加更改,删除,编辑,则就可以使用chattr +i属性
  • 若想去除隐藏熟悉,则chattr -i
[root@hf ~]# chattr -i 1.txt   
[root@hf ~]# lsattr  1.txt       
---------------- 1.txt
[root@hf ~]# mv 1.txt 3.txt       
[root@hf ~]# touch 3.txt
[root@hf ~]# echo "gurui" > 3.txt
[root@hf ~]# chattr +a   3.txt      
[root@hf ~]# rm 3.txt
rm:是否删除普通空文件 "3.txt"?y
rm: 无法删除"3.txt": 不允许的操作
[root@hf ~]# vi 3.txt       
[root@hf ~]# echo "hanfeng shuaiguo" > 3.txt     
无法添加内容进去
-bash: 3.txt: 不允许的操作
[root@hf ~]# echo "hanfeng shuaiguo" >> 3.txt        
[root@hf ~]# cat 3.txt
gurui
hanfeng shuaiguo
[root@hf ~]# echo "hanfeng shuaiguo" >> 3.txt     
[root@hf ~]# cat 3.txt
gurui
hanfeng shuaiguo
hanfeng shuaiguo
[root@hf ~]# touch 3.txt     
[root@hf ~]# ls -l 3.txt
-rw-r--r--. 1 root root 40 326 15:41 3.txt
  • chattr +a 属性, 只能追加,可以touch更改时间信息,但不能删除,不能更改名字和内容
    • chattr -a 可以去除该属性
[root@hf ~]# lsattr 3.txt
-----a---------- 3.txt
[root@hf ~]# chattr -a 3.txt    
[root@hf ~]# lsattr 3.txt
 ---------------- 3.txt
  • chattr 【+-=】 【Asaci】 文件或目录名
    +、-、=分别表示增加、删除、设定
    给目录加特殊权限,目录下的文件的文件内容是可以更改的,这个权限只是作用于目录本身。

  • A:增加该属性后,表示文件或目录的atime将不可更改。
    (小)s:增加该属性后,会将数据同步写入磁盘中。

  • a:增加该属性后,表示只能追加不能删除,非root用户不能设定该属性。
  • c:增加该属性后,表示自动压缩该文件,读取时自动解压。
  • i:增加该属性后,表示文件不能删除、重命名、设定链接、写入以及新增数据等。

  • lsattr等于list attribute 用于查看文件或目录的特殊属性

    • lsattr -d 查看目录的属性
    • 给目录加上+i权限后,是和文件添加+i属性一样的效果,不能更改名字,不能写入内容,不能创建子目录,也不能删除
    • 给目录加了一个+a权限或+i权限,能更改目录里面文件的内容
  • lsattr -R 会显示目录及子目录下的文件(一层或多层目录文件),若是不加-R,则仅仅显示一层目录文件

    [root@hf ~]# mkdir 111
    [root@hf ~]# lsattr 111
    [root@hf ~]# mkdir 111/222/
    [root@hf ~]# lsattr 111/
    ---------------- 111/222
    [root@hf ~]# lsattr -d 111/
    ---------------- 111/
    [root@hf ~]# chattr +i 111/
    [root@hf ~]# lsattr -d 111/
    ----i----------- 111/
    [root@hf ~]# rm -r 111/
    rm:是否进入目录"111/"? y
    rm:是否删除目录 "111/222"?y
    rm: 无法删除"111/222": 权限不够
    [root@hf ~]# mv 111 1212
    mv: 无法将"111" 移动至"1212": 不允许的操作
    [root@hf ~]# touch 111/12.txt
    touch: 无法创建"111/12.txt": 权限不够
    [root@hf ~]# chattr -i 111/     
    [root@hf ~]# lsattr -d 111/
    ---------------- 111/
    [root@hf ~]# chattr +a 111/     
    [root@hf ~]# touch 111/23.txt  
     [root@hf ~]# head -n 2 /etc/passwd > 111/23.txt     
    [root@hf ~]# cat 111/23.txt  
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin
    [root@hf ~]# chattr -a 111
    [root@hf ~]# chattr +i 111/
    [root@hf ~]# head -n 2 /etc/passwd > 111/23.txt
    [root@hf ~]# 
    [root@hf ~]# lsattr -R 111/     
    ---------------- 111/222
    
    111/222:
    
    ---------------- 111/23.txt
    ---------------- 111/12.txt
    [root@hf ~]# lsattr 111/        
    ---------------- 111/222
    ---------------- 111/23.txt
    ---------------- 111/12.txt
    [root@hf ~]# tree 111/
    111/
    ├── 12.txt
    ├── 222
    └── 23.txt
    
    1 directory, 2 files
  • lsattr 【-aRd】 文件名或目录名
    • -a:类似于ls的-a选项,连同隐藏文件一同列出
    • -R:连通子目录子文件的数据一同列出
    • -d:查看目录本身的特殊权限
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值