开发者学堂课程【Linux企业运维实战 - 入门及常用命令:Linux文件和目录权限高级用法】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/550/detail/7613
Linux文件和目录权限高级用法
目录:
一、不是所有系统都能设置读写执行权限
[root@centos7~]#cd/run/media/root/DA50-BA50/
[root@centos7DA50-BA50]#
l
s
backup-reset_f1_system volume Information
[root@centos7DA50-BA50]#
ll
f1
-rw-r--r--. 1 root root
0
Mar 29
01:33
f1
[root@centos7DA50-BA50]#chmod a=rwx
f1
[root@centos7DA50-BA50]#
ll
f1
rw-r--r--.
1
root root
0
Mar 29 01:33
f1
[root@centos7 DA50-BA50]#chown wang
f1
chown: changing ownership of 'f1': operation not
permitted
[root@centos7DA50-BA50]#
df -T
Fat格式的文件系统不支持读写执行以及所有者所属组特性的,只是为了兼容了Linux进行显示。
不是任何系统都支持的。数据是放在文件上的,而文件是由文件系统管理的,而文件系统有一些是互相混用的,当然传统的Linux是支持这些文件的修改的。
二、修改文件权限方法之参考一个文件的权限去修改另一个权限
例如现在把f1的权限做一下修改,把f2的权限和f1设置一样(参考f1设置f2)
total
0
-rw-r--r--. 1 root root
0
Apr4 18:45
-rw-r--r--. 1
root
root 0 Apr418:43 f2
[root@centos7 data]#chmod -rf1
[root@centoscataroot'o Apr418:43 f1
[root@centos7 data]#chmod --help
[root@centos7 data]#touch f3
[root@centos7 data]#
ll
Total
0
--w-------.
1
root root 0 Apr4 18:43
f1
-rw-r--r--. 1 root root o Apr 4 18:43
f2
-rw-r--r--. i root root o Apr 4 18:44
f3
[root@centos7 data]#chmod --reference=f1 f2 f3
[root@centos7 dataj#11
total
0
--w-------
.1
root root
0
Apr
418:43
f1
--w-------. 1 root root 0 Apr4 18:43 f2
--w-------.1root root
0
Apr418:44
f3
三、数字法授权(八进制)
有权限就转换成二进制1,没有权限就写成0。
Rwx:111
Rw-:110
R--:110
转换成二进制后,三个一组三个一组转换成八进制,r:1,w:2,x:1
实践:[root@centos7 data] #chmod 764 f1
[rootacentos7 data ] #11 f1
-rwxrw-r--.1 root root Apr 4 18:43 f1
八种情况:
--- 000 0
--x 001 1
-w- 010 2
-wx 011 3
R-- 100 4
R-x 101 5
Rw- 110 6
Rwx 111 7
对于文件来讲,常用的权限是6rw,4r,0,1x
只要有执行权限,数字加起来必定是奇数。
对于文件夹来讲常用的组合7rwx,5r-x,0
数字法授权特性:
rwxrw-r-- .
1
root root 0 Apr
4 18:43 f1
[root@centos7 data] #mkdir di r1
[
root@centos7 datal #touch dir1/f{2,3,4}
[
root@centos7 data
]
#mkdir dir1/dir2/
[root@centos7 data]#tree dir1
bash: tree: command not found
...
[root@centos7 data] #df
1 directory, 3 files
[root@centos7 data]#
11
total 0
drwxr-xr-x.3 root root 48 Apr4 18:54
d
irL
-
rwxrw-r--
.
1 root root
0 Apr
4 18:43f1
--w--------.
1
root
root
0 Apr
4 18:43 f2
--w---------. 1
root root 0 Apr
4 18:44 f3
[root@centos7 data]#111 dir1/
bash: 1 11 : command not found.
..
[ root@centos7 data]#
Dir1下的文件没有执行权限,现在希望dir1下面的dir2建立一个文件,对所有文件都加上执行权限。
[
root@centos7 data] #touch dir1/dir2/f5
[
root@centos 7
data]#11 dir1/dir2/f5
-rw-r--r--,1
root root 0 Ap
r
4
18:55 dir1/dir2/f5
[
r
oot@centos7 data] #chmod -R a+
x dir1
[root@centos7 data]#11 dir1/dir2/f5
-rwxr-xr-x. 1 root root 0 Apr 4 18:55 dir1/dir2/f5
[root@centos7 data]#11 dir1/
Total 0
对于目录的执行权限可以cd进去,可以访问文件内容。文件带有执行权限是比较危险的,一旦注入病毒,可能会造成文件被感染。
[root@centos7 data] #mkdir dir2/dir3 ,
mkdir: cannot create directory dir2/dir3': No such file or directory
[root@centos7 data ] #mkdir dir2/dir3 -p
[root@centos7 data] #touch dir2/ f6
[root@centos7 data]#touch dir2/dir3/f7
[root@centos7 data] #chmod -R -X dir2
[root@centos7 data]#11 -d dir2
drw-r--r--.3 root root 28 Apr 4 18:58 dir2
[root@centos7 data]#1t -d dir2/dir3
drw-r--r--.2 root_ root 16 Apr 4 18:58 dir2/dir3
[root@centos7 data]#11 -d dir2/dir3/f7
-rw-r--r--. 1 root root 0 Apr 4 18:58 dir2/dir3/f7
[root@centos7 data] #chmod -R +X dir2(X对目录加执行权限)
[root@centos7 data]#1T dir2/dir3/f7
rw-r--r--. 1 root root 0 Apr
4 18:58 dir2/dir3/f7
[root@centos7 data] #chmod U+x dir2/dir3/f7
[root@centos7 data]#11 dir2/dir3/f7 '
rwxr--r--. 1 root root 0 Apr 4 18:58 dir2/dir3/f7
total 0
-rwxr--r--,1
root
root 0 Apr
4 18:58
f7
-rw-r--r--,1
root root 0 Apr
4 19:01
f8
[ root@centos7 data] #chmod -R +X dir2
区别就是它对文件也加执行权限例如,前提是文件本身有执行权限了。
针对目录加执行权限,如果有执行权限的文件可以加执行权限。
◆文件属性操作
➢chown设置文件的所有者
➢chgrp设置文件的属组信息
如果出现一些破坏性命令,会无法恢复,
例如:
-R,敲命令时一定要慎重。基于安全考虑,尽量做好充足的测试。
四、修改文件的属主和属组
1.修改文件的属主:chown
chown [OPTION]... [OWNER][:[GROUP]] FILE..
用法:
OWNER
OWNER:GROUP
:GROUP
命令中的冒号可用.替换
-R:递归
chown [OPTION]... --reference=RFILE FILE...
2.修改文件的属组:chgrp
chgrp [OPTION].. GROUP FILE..
chgrp [OPTION].. --reference=RFILE FILE...
-R递归
3.文件的权限主要针对三类对象进行定义
owner:属主, u
group:属组, g .
other:其他,o
4.每个文件针对每类访问者都定义了三种权限
r: Readable
w: Writable
x: eXcutable
5.文件
r:可使用文件查看类工具获取其内容
w:可修改其内容
x:可以把此文件提请内核启动为一个进程
6.目录:
r:可以使用Is查看此目录中文件列表
w:可在此目录中创建文件,也可删除此目录中的文件
x:可以使用Is -l查看此目录中文件列表,可以cd进入此目录
X:只给目录x权限,不给文件x权限
五、修改文件权限
◆chmod [OPINN.. OCTAL-MODE FILE..
-R:递归修改权限
◆chmod [OPTION].. MODE[,MODE1... FILE...
MODE :
修改一类用户的所有权限:
u= g= 0= ug= a= u=,g=
修改一-类用户某位或某些位权限
U+U-g+g-0+0-a+a-+-
◆chmod [OPTION... --reference=RFILE FLE..
参考RFILE文件的权限,将FILE的修改为同RFILE
六、权限设置示例
◆chgrp sales testfile
◆chown root:admins testfile(同时修改所有者和所属组)
◆chmod u+wx,g-r,o=rx file
◆chmod -R g+ rwX /testdir
◆chmod 600 file
◆chown mage testfile
每个命令都应该尽可能的一目了然,慎重执行。
七、新建文件和目录的默认权限
◆umask值可以用来保留在创建文件权限
◆新建FILE权限: 666- umask
如果所得结果某位存在执行(奇数)权限,则将其权限+ 1
◆新建DIR权限: 777-umask
非特权用户umask是002
◆root的umask是022
◆umask:查看
◆umask #:设定
umask 002
◆umask -S模式方式显示
umask -p输出可被调用
◆全局设置: /etc/bashrc 用户设置: ~/.bashrc
新建一个文件或者文件夹
[root@centos7 data]#rm -rf
[
root@centos7 data] #touch f1
[r
oot@centos7 data] #mkdir dir1
[root@centos7 data]#1 1
total 0
drwxr-xr-x.2 root root 6 Apr
4 19:19 dirl1
root root 0
4 19:19 f1
[r oot@centos7 datal #touch
[r oot@centos7 data]#] 1
total 0
drwxr-xr-x.2 root root 6 Apr4 19:19 dirl
-rw-r--r--. 1 root root 0
Apr
4 19:19 f1
root root 0 Apn 4
19:19 f2
[root@centos7 data] #type umask
umask is a she11 bui ltin
[ root@centos7 data] #type chmod
Chmod is hashed (/usr /bin/ chmod)
[root@centos7 data] #umask +defau
l
t=
file
666
dir
/777
初步认为umask与默认权限对位相加。如果umask发生变化就会影响默认权限。
chmod is hashed (/usr /bin/ chmod)
[root@centos7 data] #umask022+defau1t644 |755=fi 1e66Acdir777
[root@centos7 dataj #umask
0022
[root@centos7 data] #umask
026
[root@centos7 data ] #umask
0026
[r oot@centos7 data] #touhc f3
bash: touhc: command not found. . .
Simi lar command is:' touch'
[root@centos7 data]#> f3
[r oot@centos7 data]#1l f3
rw-r---1 root root 0 Apr
4 19:24 f3
[r oot@centos7 data] #mkdir dir2
[r oot@centos7 data]#11 -d dir2
drwxr-x--X.2 root root 6 Apr
4 19:24 dir2
为什么文件用666文件夹用777,如果文件也用777,就有可能设一个umask000,777是有执行权限的,系统考虑到安全因素,所以默认不希望新建的文件具有执行权限,所以文件用了666来计算。666不具有执行权限,文件夹的执行权限就不意味着危险了。
八、umask的真正含义
Mask:掩码
Umask本质含义(功能):取消对应的权限
最大权限666,对应把125权限取消,先把666和123转换成对应的二进制,666对应的二进制为110 110 110 。125对应的二进制为001 010 101。0表示不取消,1表示取消。最终权限为110100010,最后的读写权限为642。
微调计算公式:umask022+default644|755=file666|dir777
观察一下结果,默认权限里面如果带奇数,就加1,如果是偶数,则不变。对文件来说需要这种公式,但是目录并不需要。
公式总结:
1.对于目录:default权限=777-umask
2.对于文件:default权限=666-umask:对结果观察,如果有奇数+1,偶数不变。
例子:如果umask 251,那么新建的文件夹的权限是什么?文件的权限是什么?
[root@centos7 data]#umask 251
[root@centos7 dataj #dir:777-251=526
file:666-251-415 ,426
bash: dir:777-251-526: command not found. . .
[root@centos7 data ] #umask
0251
[root@centos7 data] #touch f8
[r oot@centos7 data]#11 f8
-r---W-rW-. 1 root root 0 Apr
4 19:37 f8
[root@centos7 data] #mkdir d
i
r8
[root@centos7 data]#11 -d dir8
dr-x-w-rW-.2 root root 6 Apr
4 19:38 dir8
如果存得住必须写文件。
为什么默认情况下作为root和普通用户来讲的umask已经不一样了?
[root@centos7 ~] #umask 0022
[ root@centos7 ~] #su-wang
Last login: wed Apr 4 19:40:00 CST 2018 on pts/0
[wang@centos7 ~ 1 Sumask 0002
显而易见,umask值0022的权限带来的结果就是新建文件的权限越小,之所以这样设置是考虑到了安全因素。
这些值是在哪里进行设置的呢?
[root@centos7 ~]#cat /ect/bashrc
九、umask的其他用法
Umask-p:以补位的方式查看文件的权限,显示的是补位的十进制。
Umask-s:以rwx形式显示新建文件或目录缺省权限。
[root@centos7 ~] #umask -S
u=rwx, g=rx, O=X
[root@centos7 ~] #umask 026
r oot@centos7
#umask
u=rwx,
g=r
o
=
[ root@centos7 ~ ] #umask
0037
十、练习
1.复制/etc/fstab文件到/var/tmp下,设置文件所有者为tomcat读写权限,所属组为apps组有读写权限,其他人无权限
useradd: user" tomcat already exists
[ root@centos6
~
] #groupadd apps
[ root@centos6
~]
#gr oupmems -a tomcat -g apps
[
root@Centos6
~]
#Cp /etc/fstab /var/tmp/
Cp: overwrite
/var /tmp/ fstab ? y
[root@Centos6 ~]#cd !$cd /var /tmp/
[root@Centos6 tmp]#
1s
fstab kdecache -root
[root@Centos6 tmp]#11
tota1 8
-rw-rw----.1 tomcat app
899 Apr 4 07 :38 fstab
drwx--
----
.
6
rootroot 4096 Mar 27 18:54 kdecache -root
[root@Centos6 tmp ] #chown
660 fstab
[
root@Centos6 tmp]#11
total 8
-rw-rw----. 1
660 app
899 Apr 4 07 :38 fstab
drwx --
----
6
root root 4096 Mar 27 18:54 k decache -root
[root@Centos6 tmp ] #chmod 660 fstab
[
root@Centos6 tmp ] #chown tomcat:apps fstab
[root@Centos6 tmp ]#11
Tota
l
8
-rw- rw----.1 tomcat apps
899 Apr 4 07:38 fstab
drwx --
----.
6
rootroot 4096 Mar 27 18:54 k decache -root
2.误删除了用户git的家目录,请重建并恢复该用户家目录及相应的权限属性
[root@centos6 ~] #useradd git
[ root@centos6
~]
#rm -rf /home/git
[root@centos6
~]
#mkdir /home/git ; cp -a /etc/ske1/.[^.]* /home/git
[root@centos6 ~]#11 -d /home/git
drwxr-xr-x.4root root 4096 Apr4 12:04 /home/git
[root@centos6 ~]#11 -a/home/git
total 28
[root@centos6 -]#chown -R git . git /home/git/
[root@centos6 ~]#11 -d
/
home/git/
drwxr-xr-x. 4 git
git 4096 Apr 4 12:04 /home /git
[root@centos6 ]#11 /home/git/ -a
total 28
[root@centos6 ]#11 /home
total 12
drwxr-xr-x.4 git
git 4096 Apr 4 12:04 git
drWx-4 mage mage 4096 Apr
4
09:46 mage
Drwx
24 wang wang 4096 Mar 28 09: 32 wang
[root@centos6 ~] #chmod 700 /home/git
[root@centos6 ~]#11
total 108
mkdir /home/git : cp
-
a /etc/skel/. [^.]* /home/git
204
11
-
d /home/git
205
11 -a /home/git
206
chown -R git.git /home/git/
207
11 -d /home/git/
208
11 /home/git/ -a
209
11 /home
210 chmod 700 /home/git