权限管理命令
Linux中用户分为
u: user
g: group
o: others
a: all
用户权限分为
r: read
w: write
x: execute
chmod:
作用:change mode,改变文件或目录权限
用法:chmod [{ugoa}{+-=}{rwx}] [目标文件或目录]
[jerry@localhost Test]$ touch chmodtest.file
[jerry@localhost Test]$ ll chmodtest.file
-rw-rw-r--. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
[jerry@localhost Test]$ chmod u+x chmodtest.file
[jerry@localhost Test]$ ll chmodtest.file
-rwxrw-r--. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
[jerry@localhost Test]$ chmod ug-w chmodtest.file
[jerry@localhost Test]$ ll chmodtest.file
-r-xr--r--. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
[jerry@localhost Test]$ chmod g+w,o-r chmodtest.file
[jerry@localhost Test]$ ll chmodtest.file
-r-xrw----. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
- [-R –recursive]递归修改目录及其子目录和文件的权限
但更多的是用“数字的和”来表示所需的权限,用三位数字分别表示所有者,所属组和其他人的权限
r: 4
w: 2
x: 1
例如:rwx: 7 rw-: 6 r–: 4
用法:chmod [三位八进制数字] [目标文件或目录]
[jerry@localhost Test]$ ll chmodtest.file
-r-xrw----. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
[jerry@localhost Test]$ chmod 741 chmodtest.file
[jerry@localhost Test]$ ll chmodtest.file
-rwxr----x. 1 jerry jerry 0 Oct 21 21:23 chmodtest.file
深入理解rwx:
只有文件的所有者和root可以更改文件的权限
chown:
作用:change owner,改变文件或目录的所有者
用法:chomd [用户] [文件或目录]
只有root可以改变文件或目录的所有者,用户必须已经存在在系统中
chgrp:
作用:change file group ownership
用法:chgrp [用户组] [文件或目录]
Tips: 一个用户可以属于多个组,但是总有且只有一个缺省组
umask:
作用:显示,设置新建文件的缺省权限,需要用”掩码” 777 - 755 = 022
[jerry@localhost Test]$ su root
Password:
[root@localhost Test]# umask
0022
[root@localhost Test]# umask -S
u=rwx,g=rx,o=rx
[root@localhost Test]# touch umaskroot.file
[root@localhost Test]# ll umaskroot.file
-rw-r--r--. 1 root root 0 Oct 21 21:58 umaskroot.file
[root@localhost Test]# ls -ld umaskroot.dir
drwxr-xr-x. 2 root root 6 Oct 21 22:01 umaskroot.dir
[root@localhost Test]# 777 - 022 = 755 rwxr-xr-x
- [-S] 以rwx的形式显示新建文件的缺省权限
- mkdir一个目录,它的权限与umask一致
- touch一个文件,它的权限与umask不一致,少了x权限
出于安全性考虑,Linux中任何新建的文件都没有可执行权限