2.14 文件和目录权限chmod
命令chmod(change mode简写)用于改变用户对文件/目录的读写执行权限;
格式:chmod xyz 文件名 (xyz是数字)
[root@linux-151 tmp]# chmod 750 111
[root@linux-151 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 3月 28 23:11 111
查看目录信息
[root@linux-151 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 3月 28 23:11 111
文件或目录的详细信息分9段,第1段drwxr-xr-x包含文件的类型和所有者,所属组,其他用户对文件的权限;
r(read)可读
w(write)可写
x(executable)可执行
r=4 w=2 x=1
注:目录必须有x权限,文件没有x权限
1. chmod //修改文件和目录的权限
[root@linux-151 tmp]# ls -ld 111
drwxr-xr-x 2 root root 6 3月 28 23:11 111
[root@linux-151 tmp]# chmod 750 111
[root@linux-151 tmp]# ls -ld 111
drwxr-x--- 2 root root 6 3月 28 23:11 111
2. chmod –R //联级更改,修改目录本身及目录下所有的子目录文件权限
drwxr-xr-x 2 root root 6 3月 28 23:42 222/123
-rw-r--r-- 1 root root 0 3月 28 23:42 222/1.txt
[root@linux-151 tmp]# chmod 700 -R 222
[root@linux-151 tmp]# ls -ld 222 -l 222/1.txt -ld 222/123
drwx------ 3 root root 30 3月 28 23:42 222
drwx------ 2 root root 6 3月 28 23:42 222/123
-rwx------ 1 root root 0 3月 28 23:42 222/1.txt
3. chmod a=rwx,g=rx,o=rx 文件名(不常用)
u(user) 所有者
g(group)所属组
o(others)其他用户
[root@linux-151 tmp]# chmod u=rwx,g=rx,o=rx -R 222
[root@linux-151 tmp]# ls -ld 222 -l 222/1.txt -ld 222/123
drwxr-xr-x 3 root root 30 3月 28 23:42 222
drwxr-xr-x 2 root root 6 3月 28 23:42 222/123
-rwxr-xr-x 1 root root 0 3月 28 23:42 222/1.txt
2.15 更改所有者和所属组chown
chown(change ownar)命令用来修改文件的所属主(所有者)和所属组
chown -R 作用于目录联级修改,目录本身和子目录文件。
[root@linux-151 ~]# useradd user1 \\创建用户user1
[root@linux-151 ~]# groupadd group1 \\创建组 group1
[root@linux-151 ~]# chown user1:group1 5.txt \\修改文件5.txt所属主为user1,所属组为group1
[root@linux-151 ~]# ls -l 5.txt
-rw-r--r--. 1 user1 group1 78 3月 28 23:01 5.txt
chown user1 1.txt \\修改属主
chown user1:group1 1.txt \\修改属主,属组
chown user1.group1 1.txt \\修改属主,属组
chown :group1 1.txt \\修改属组
chown .group1 1.txt \\修改属组
[root@linux-151 ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 3月 28 23:44 1.txt
[root@linux-151 ~]# chown lem 1.txt
[root@linux-151 ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 lem root 0 3月 28 23:44 1.txt
[root@linux-151 ~]# chown :lem 1.txt
[root@linux-151 ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 lem lem 0 3月 28 23:44 1.txt
[root@linux-151 ~]# chown root:root 1.txt
[root@linux-151 ~]# ls -l 1.txt
-rw-r--r--. 1 root root 0 3月 28 23:44 1.txt
[root@linux-151 ~]# chown lem.lem 1.txt \\chown也可以用点来隔开
[root@linux-151 ~]# !ls
ls -l 1.txt
-rw-r--r--. 1 lem lem 0 3月 28 23:44 1.txt
chgrp 修改文件属组 chgrp group1 1.txt
chgrp -R group1 111
!ls 表示命令历史中以ls开头距我访问最近的一条命令
history 历史命令
[root@linux-151 ~]# history
16 midir test
17 mkdir test
18 ls
!18 !后面加上历史命令前面的序号也可以执行。
[root@linux-151 ~]# !18
ls
111.bak 222 4.txx 5.txt 6.txt anaconda-ks.cfg install.log.syslog test
1.txt 444 4.txy 666 777 install.log prem
2.16 umask
umask命令用于改变文件以及目录的默认权限
umask XXX(3个x表示数字,原本umask值为4位,第一位默认省略)
umask 022
[root@linux-151 ~]# umask
0022
注:通常只用到后面三位022 --- -w- -w-
系统默认情况下目录权限值为755,文件权限为644,这个是有umask规定的
当umask值为022时,系统文件和目录的默认权限为
目录 文件
777 rwx rwx rwx 666 rw- rw- rw-
-022 --- -w- -w- -022 --- -w- -w-
= rwx r-x r-x rw- r-- r--
= 7 5 5 6 4 4
当umask值为003时,系统文件和目录的默认权限为
目录 文件
777 rwx rwx rwx 666 rw- rw- rw-
-003 --- --- -wx -003 --- --- -wx
= rwx rwx r-- rw- rw- r--
= 7 7 4 6 6 4
2.17 隐藏权限lsattr/chattr
1、lsattr 查看文件的特殊属性
lsattr -d 查看目录本身特殊属性
[root@linux-151 tmp]# lsattr 222
---------------- 222/1.txt
---------------- 222/123
[root@linux-151 tmp]# chattr +i 222/1.txt
[root@linux-151 tmp]# chattr +a 222/
123/ 1.txt 2.txt
[root@linux-151 tmp]# chattr +a 222/123/
[root@linux-151 tmp]# lsattr -d 222
-----a---------- 222
lsattr -R 联级,查看目录下子目录子文件的特殊属性
[root@linux-151 tmp]# lsattr -R 222
----i----------- 222/1.txt
-----a---------- 222/123
222/123:
---------------- 222/2.txt
lsattr -a 查看目标目录所有文件(包含隐藏文件)的特殊属性
2、chattr 增加删除设定特殊属性
chattr +a 增加后只能追加,不能删除或者直接更改它的内容,非root用户不能设置该属性
chattr +i 增加后不能做任何操作,不能添加,删除,重命名,设定链接,修改属主,属组。
chattr +A 增加后文件或者目录的atime将不可被修改
chattr +S 增加后会将数据同步写入磁盘中
例1:给文件1.txt加上i权限,然后进行删除,修改、追加内容,修改文件属性,所有者,所属组。
[root@linux-151 tmp]# chattr +i 222/1.txt
[root@linux-151 tmp]# lsattr 222/1.txt
----i----------- 222/1.txt
[root@linux-151 tmp]# rm 222/1.txt
rm:是否删除普通空文件 "222/1.txt"?y
rm: 无法删除"222/1.txt": 不允许的操作
[root@linux-151 tmp]# echo '11111'>222/1.txt
-bash: 222/1.txt: 权限不够
[root@linux-151 tmp]# echo '11111'>>222/1.txt
-bash: 222/1.txt: 权限不够
[root@linux-151 tmp]# chmod 755 222/1.txt
chmod: 更改"222/1.txt" 的权限: 不允许的操作
[root@linux-151 tmp]# chown lem:lem 222/1.txt
chown: 正在更改"222/1.txt" 的所有者: 不允许的操作
删除i权限后就能正常修改了。
[root@linux-151 tmp]# chattr -i 222/1.txt
[root@linux-151 tmp]# chown lem:lem 222/1.txt
[root@linux-151 tmp]# ls -l 222/1.txt
-rw-r--r-- 1 lem lem 0 3月 28 23:42 222/1.txt
例2:给文件1.txt加上a权限,然后进行删除,修改、追加内容,修改文件属性,所有者,所属组。
[root@linux-151 tmp]# chattr +a 222/1.txt
[root@linux-151 tmp]# lsattr 222/1.txt
-----a---------- 222/1.txt
[root@linux-151 tmp]# rm 222/1.txt
rm:是否删除普通空文件 "222/1.txt"?y
rm: 无法删除"222/1.txt": 不允许的操作
[root@linux-151 tmp]# echo '1111'>222/1.txt
-bash: 222/1.txt: 不允许的操作
[root@linux-151 tmp]# echo '1111'>>222/1.txt
[root@linux-151 tmp]# cat 222/1.txt
1111
[root@linux-151 tmp]# chmod 755 222/1.txt
chmod: 更改"222/1.txt" 的权限: 不允许的操作
[root@linux-151 tmp]# mv 222/1.txt 222/2.txt
mv: 无法将"222/1.txt" 移动至"222/2.txt": 不允许的操作
[root@linux-151 tmp]# chown root:root 222/2.txt
chown: 无法访问"222/2.txt": 没有那个文件或目录
注:1.txt加上a权限后,只能进行追加内容
给目录赋予i权限后,依旧可以对该目录下已存在的文件进行编辑