Linux基础(day8)

时间:2022-04-27
本文章向大家介绍Linux基础(day8),主要内容包括2.14 文件和目录权限chmod、2.15 更改所有者和所属组chown、2.16 umask、2.17 隐藏权限lsattr/chattr、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

2.14 文件和目录权限chmod

文件属性

[root@localhost ~]# ls -l
总用量 12
-rw-r--r--. 1 root root    0 10月 25 16:06 1.txt
-rw-------. 1 root root 1422 10月 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-表示第一段,user所有者的权限
  • r--表示第二段,group所属组的权限
  • r--表示第三段,others其他用户权限
  • 【点.】 有的文件有点,有的没有,意味这个文件受制于SELinux,如果selinux开启,创建的文件或目录在这个位置就会有点
  • 数字1,则表示 相同inode的文件数,与目录下子目录数有关
  • root(第一个),表示文件所属主 ,文件所有者
  • root(第二个),表示文件所属组
  • 0(数字),表示文件大小
  • 25 16:06(时间),表示文件最后一次修改的时间
  • 1.txt,表示文件 (这里可以是目录或文件)

chmod

  • chmod等于change mode
  • chmod命令,用于改变用户对文件或目录的读写执权限
  • chmod -R 表示可以批量更改目录本身以及目录下的子目录和文件的权限
[root@hf-01 ~]# ls -l
总用量 8
-rw-r--r--. 1 root root 924 10月 25 06:49 2.txt
-rw-------. 1 root root 973 8月  21 05:05 anaconda-ks.cfg.1
[root@hf-01 ~]# chmod 700 2.txt     更改2.txt文件权限
[root@hf-01 ~]# ls -l 2.txt
-rwx------. 1 root root 924 10月 25 06:49 2.txt     这里会发现2.txt权限改变了
[root@hf-01 ~]# getenforce  查看防火墙是否关闭
[root@hf-01 ~]# setenforce 0       临时关闭防火墙

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

只有关闭了selinux,-rwx------. 最后的这个点才会消失

chmod例子

[root@hf-01 ~]# mkdir hf/   新建目录hf/
[root@hf-01 ~]# ls
2.txt  anaconda-ks.cfg.1  hf
[root@hf-01 ~]# cd hf/
[root@hf-01 hf]# touch 1.txt       新建文件1.txt
[root@hf-01 hf]# ls 
1.txt
[root@hf-01 hf]# ls -l 
总用量 0
-rw-r--r--. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 hf]# cd
[root@hf-01 ~]# chmod 770 1.txt
chmod: 无法访问"1.txt": 没有那个文件或目录      这是因为1.txt在目录hf/下面
[root@hf-01 ~]# chmod 770 hf/      更改hf/文件夹的权限
[root@hf-01 ~]# ls -l hf/          会发现里面的1.txt权限没有发生变化
总用量 0
-rw-r--r--. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# ls -ld hf/         而文件夹的权限则发生了变化
drwxrwx---. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod -R 661 hf/     在加上了-R选项,文件和目录和子目录批量的更改了权限
[root@hf-01 ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# ls -ld hf/
drw-rw---x. 2 root root 18 10月 26 06:56 hf/

首字母缩写更改权限

  • u 表示user
  • g 表示group
  • o 表示others
  • a 表示all(全部)
    • 如:u+(-)rwx,g+(-)rwx,o+(-)rwx 如果更改多个属性,中间可用“,”隔开。
    • 又如:a+(-)rwx
[root@hf-01 ~]# chmod u=rwx,g=w,o=r hf/       字母缩写代替更改权限
[root@hf-01 ~]# ls -ld hf/
drwx-w-r--. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# ls -l hf/
总用量 0
-rw-rw---x. 1 root root 0 10月 26 06:56 1.txt
[root@hf-01 ~]# chmod a+x hf/      所有文件权限都加上x执行权限
[root@hf-01 ~]# ls -ld hf/
drwx-wxr-x. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod o+w hf/      其他用户组加上w可写的权限
[root@hf-01 ~]# ls -ld hf/
drwx-wxrwx. 2 root root 18 10月 26 06:56 hf/
[root@hf-01 ~]# chmod a-w hf/      所有文件权限减去w可写的权限
[root@hf-01 ~]# ls -ld hf/
dr-x--xr-x. 2 root root 18 10月 26 06:56 hf/

总结

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

2.15 更改所有者和所属组chown

chown命令

chown介绍和例子

  • chown等于change owner 更改文件的所有者和所属组
[root@hf-01 ~]# ls /tmp
aminglinux  amning  mysql.sock  yum.log
[root@hf-01 ~]# ls -l /tmp/yum.log        会看到yum.log的所有者是root
-rw-r--r--. 1 root root 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown aming /tmp/yum.log 
chown: 无效的用户: "aming"             这是因为在/etc/passwd中没有aming这个用户,需要useradd aming即可
[root@hf-01 ~]# chown hanfeng /tmp/yum.log    这时会看到所有者发生了变化,yum.log文件的所有者变化成hanfeng了
[root@hf-01 ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng root 0 10月 26 07:48 /tmp/yum.log

chown的用法

  • chown -R username:group filename
  • chown 【-R】用户名 文件名 /更改所属主
  • chown 【-R】 用户名:组名 文件名 /更改所属主和所属组
[root@hf-01 ~]# !ls
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown user1:hanfeng /tmp/yum.log
[root@hf-01 ~]# !ls     这里会看到所属主和所属组发生了改变,用户和组中间用:隔开
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 hanfeng 0 10月 26 07:48 /tmp/yum.log
[root@hf-01 ~]# chown -R hanfeng:user1 /tmp/aminglinux/
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 hanfeng user1 18 10月 24 07:21 2
drwxr-xr-x. 4 hanfeng user1 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 hanfeng user1 27 10月 25 07:29 /tmp/aminglinux/
[root@hf-01 ~]# touch /tmp/aminglinux/3.txt
[root@hf-01 ~]# chown -R user1:hanfeng /tmp/aminglinux/
[root@hf-01 ~]# ls -l /tmp/aminglinux/
总用量 0
drwxr-xr-x. 2 user1 hanfeng 18 10月 24 07:21 2
-rw-r--r--. 1 user1 hanfeng  0 10月 26 08:23 3.txt
drwxr-xr-x. 4 user1 hanfeng 31 10月 25 06:55 aming2
[root@hf-01 ~]# ls -ld /tmp/aminglinux/
drwxr-xr-x. 4 user1 hanfeng 39 10月 26 08:23 /tmp/aminglinux/
  • chown 【-R】 :组名 文件名 / 只更改所属组
[root@hf-01 ~]# chown :root /tmp/yum.log
[root@hf-01 ~]# !ls     这里只更改了它的所属组
ls -l /tmp/yum.log
-rw-r--r--. 1 user1 root 0 10月 26 07:48 /tmp/yum.log

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

chgrp

  • chgrp等于change group 更改文件所属的用户组
[root@hf-01 ~]# chgrp user1 /tmp/yum.log
[root@hf-01 ~]# !ls     在之前的所属组是root,现在所属组是user1
ls -l /tmp/yum.log
-rw-r--r--. 1 hanfeng user1 0 10月 26 07:48 /tmp/yum.log
  • chgrp 【-R】 组名 文件名

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

2.16 umask

umask命令介绍

  • umask命令,通过这个值可以确定文件和目录的默认权限是什么。
  • 默认情况下,目录的权限值为755(rwxr-xr-x),普通文件的默认权限为644(-rw-r--r--),umask默认值为0022(----w--w-)

例子对比

[root@hf-01 ~]# touch 11.txt
[root@hf-01 ~]# ls -l 11.txt
-rw-r--r--. 1 root root 0 10月 26 08:39 11.txt
[root@hf-01 ~]# mkdir 123
[root@hf-01 ~]# ls -ld 123
drwxr-xr-x. 2 root root 6 10月 26 08:39 123
[root@hf-01 ~]# umask   这是系统root用户的umask值0022,通过这个值就可以确认文件的默认权限,也可以确认目录的默认权限是什么
0022
[root@hf-01 ~]# umask 002   这里更改默认权限,写全了是0002,但一般会省去开头的0,写成002
[root@hf-01 ~]# touch 33.txt
[root@hf-01 ~]# ls -l 33.txt       这里和上面对比,会发现创建的文本权限发生了变化
-rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt
[root@hf-01 ~]# mkdir 234
[root@hf-01 ~]# ls -ld 234         这里的文件夹权限也发生了变化
drwxrwxr-x. 2 root root 6 10月 26 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-01 ~]# chattr +i 33.txt      给33.txt(空文件)增加了隐藏属性
[root@hf-01 ~]# vi 33.txt          是无法进去编辑文件,增加内容的,强制保存都不可以,它会提示说只有可读权限
[root@hf-01 ~]# head -n2 /etc/passwd > 33.txt
-bash: 33.txt: 权限不够     将文件写入到33.txt文件中,也会提示权限不够
[root@hf-01 ~]# ls -l 33.txt       但查看的时候会看到有可读可写的权限,这时就要想要它是否添加了隐藏属性
-rw-rw-r--. 1 root root 0 10月 26 08:56 33.txt
[root@hf-01 ~]# lsattr 33.txt      可查看33.txt添加了i隐藏属性
----i----------- 33.txt
[root@hf-01 ~]# touch ha.txt
[root@hf-01 ~]# lsattr ha.txt       查看ha.txt文件的隐藏属性
---------------- ha.txt
Try 'mv --help' for more information.
[root@hf-01 ~]# mv 33.txt 56.txt        这时修改将文件改名,会发现无法实现
mv: 无法将"33.txt" 移动至"56.txt": 不允许的操作
[root@hf-01 ~]# rm 33.txt               也无法去删除该文件,就算加-f强制去删除,再去查看的时候,会看到文件依旧存在
rm:是否删除普通空文件 "33.txt"?y
rm: 无法删除"33.txt": 不允许的操作
[root@localhost ~]# touch 1.txt     会发现也无法现更改
touch: 无法创建"1.txt": 权限不够

chattr用法

  • chattr命令,它是一个非常严谨的权限
  • chattr +i 则为文件增加了隐藏属性
    • 若是有一个文件使它无法追加更改,删除,编辑,则就可以使用chattr +i属性
  • 若想去除隐藏熟悉,则chattr -i
[root@localhost ~]# chattr -i 1.txt   删除隐藏属性
[root@localhost ~]# lsattr  1.txt       再来查看,会发现隐藏属性没了
---------------- 1.txt
[root@localhost ~]# mv 1.txt 3.txt       这时就可以更改名称了(编辑、删除都可以)
[root@localhost ~]# touch 3.txt
[root@localhost ~]# echo "gurui" > 3.txt
[root@localhost ~]# chattr +a   3.txt      给文件增加a属性,会发现无法删除、编辑,添加内容,只能追加内容
[root@localhost ~]# rm 3.txt
rm:是否删除普通空文件 "3.txt"?y
rm: 无法删除"3.txt": 不允许的操作
[root@localhost ~]# vi 3.txt       无法编辑3.txt文件
[root@localhost ~]# echo "hanfeng shuaiguo" > 3.txt     
无法添加内容进去
-bash: 3.txt: 不允许的操作
[root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt        只能追加内容进3.txt文件中
[root@localhost ~]# cat 3.txt
gurui
hanfeng shuaiguo
[root@localhost ~]# echo "hanfeng shuaiguo" >> 3.txt     继续追加内容
[root@localhost ~]# cat 3.txt
gurui
hanfeng shuaiguo
hanfeng shuaiguo
[root@localhost ~]# touch 3.txt     可以更改时间信息
[root@localhost ~]# ls -l 3.txt
-rw-r--r--. 1 root root 40 10月 26 15:41 3.txt
  • chattr +a 属性, 只能追加,可以touch更改时间信息,但不能删除,不能更改名字和内容
    • chattr -a 可以去除该属性

    [root@localhost ~]# lsattr 3.txt -----a---------- 3.txt [root@localhost ~]# chattr -a 3.txt 会看到a属性去除了 [root@localhost ~]# lsattr 3.txt ---------------- 3.txt

chattr总结

  • chattr 【+-=】 【Asaci】 文件或目录名

+、-、=分别表示增加、删除、设定 给目录加特殊权限,目录下的文件的文件内容是可以更改的,这个权限只是作用于目录本身。

  • A:增加该属性后,表示文件或目录的atime将不可更改。 (小)s:增加该属性后,会将数据同步写入磁盘中。
  • a:增加该属性后,表示只能追加不能删除,非root用户不能设定该属性。
  • c:增加该属性后,表示自动压缩该文件,读取时自动解压。
  • i:增加该属性后,表示文件不能删除、重命名、设定链接、写入以及新增数据等。

latter介绍

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

latter用法

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

[root@localhost ~]# mkdir 111
[root@localhost ~]# lsattr 111
[root@localhost ~]# mkdir 111/222/
[root@localhost ~]# lsattr 111/
---------------- 111/222
[root@localhost ~]# lsattr -d 111/
---------------- 111/
[root@localhost ~]# chattr +i 111/
[root@localhost ~]# lsattr -d 111/
 ----i----------- 111/
[root@localhost ~]# rm -r 111/
rm:是否进入目录"111/"? y
rm:是否删除目录 "111/222"?y
rm: 无法删除"111/222": 权限不够 
[root@localhost ~]# mv 111 1212 
mv: 无法将"111" 移动至"1212": 不允许的操作 
[root@localhost ~]# touch 111/12.txt 
touch: 无法创建"111/12.txt": 权限不够 
[root@localhost ~]# chattr -i 111/     给目录去除-i属性 
[root@localhost ~]# lsattr -d 111/ 
---------------- 111/ 
[root@localhost ~]# chattr +a 111/     给目录加上+a权限 
[root@localhost ~]# touch 111/23.txt  只能追加一个文件(创建一个文件,也算是追加)  
[root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt     给目录加了 
[root@localhost ~]# cat 111/23.txt   
root:x:0:0:root:/root:/bin/bash 
bin:x:1:1:bin:/bin:/sbin/nologin 
[root@localhost ~]# chattr -a 111 
[root@localhost ~]# chattr +i 111/ 
[root@localhost ~]# head -n 2 /etc/passwd > 111/23.txt 
[root@localhost ~]#  
[root@localhost ~]# lsattr -R 111/     
在加上-R会查看111/目录下的文件和111/子目录下的文件 
---------------- 111/222  
111/222:  
---------------- 111/23.txt 
---------------- 111/12.txt 
[root@localhost ~]# lsattr 111/        若是不加,就仅仅显示一层的目录文件 
---------------- 111/222 
---------------- 111/23.txt 
---------------- 111/12.txt 
[root@localhost ~]# tree 111/ 
111/ 
├── 12.txt 
├── 222 
└── 23.txt  
1 directory, 2 files
 

lsattr总结

  • lsattr 【-aRd】 文件名或目录名
    • -a:类似于ls的-a选项,连同隐藏文件一同列出
    • -R:连通子目录子文件的数据一同列出
    • -d:查看目录本身的特殊权限