Linux权限处理常用的四个命令详解

时间:2022-01-22 16:41:47

原创Blog,转载请注明出处

http://blog.csdn.net/hello_hwc?viewmode=contents

权限

对于文件
r 可读
w 可写
x 可执行
对于目录
r 可以列出目录的内容(ls)
w 可以在目录中创建和删除文件(touch/rm)
x 可以进入目录(cd)


一、chmod
chmod用来改变权限
常用的方式
1、用ugo+rwx 或者ugo-rwx或者ugo=rwx改变权限
这里的ugo
u 用户
g 用户组
o 其他人
比如:ug+x 就是为用户和用户组增加可执行权限
举例:
[root@localhost testForCsdn]# ls -l testfile 
-rw-r----- 1 root root 0 Nov 1 22:14 testfile

为其他人赋予读写权限
[root@localhost testForCsdn]# chmod o=rw testfile 
[root@localhost testForCsdn]# ls -l testfile
-rw-r--rw- 1 root root 0 Nov 1 22:14 testfile

为其他人去掉写的权限
[root@localhost testForCsdn]# chmod o-w testfile 
[root@localhost testForCsdn]# ls -l testfile
-rw-r--r-- 1 root root 0 Nov 1 22:40 testfile

为用户组添加写的权限
[root@localhost testForCsdn]# chmod g+w testfile 
[root@localhost testForCsdn]# ls -l testfile
-rw-rw-r-- 1 root root 0 Nov 1 22:40 testfile

2、用数字的方式改变权限
r对应4
w对应2
x对应1
比如:5=4+1 那么5代表的就是可读和可执行权限
举例
创建一个空文件,并且查看权限,可以看到,当前权限是rw-r--r--
也就是说:
对说有着来说是rw- 可读可写不可执行
对所属组来说是r-- 只可写
对其他人来说是r-- 只可写
举例
创建一个脚本,并且赋予它所有者的可执行权限
[root@localhost testForCsdn]# touch test.script
[root@localhost testForCsdn]# ls -l test.script
-rw-r----- 1 root root 0 Nov 1 22:43 test.script
[root@localhost testForCsdn]# chmod 744 test.script
[root@localhost testForCsdn]# ls -l test.script
-rwxr--r-- 1 root root 0 Nov 1 22:43 test.script

二、chown
改变所有者
首先添加用户hwc
[root@localhost testForCsdn]# useradd hwc
[root@localhost testForCsdn]# chown hwc test.script
[root@localhost testForCsdn]# ls -l test.script
-rwxr--r-- 1 hwc root 0 Nov 1 22:43 test.script

三、chgrp
改变所属组
首先添加用户组hwcgroup
[root@localhost testForCsdn]# chgrp hwcgroup test.script
[root@localhost testForCsdn]# ls -l test.script
-rwxr--r-- 1 hwc hwcgroup 0 Nov 1 22:43 test.script

四、umask
文件和目录创建的默认权限
查看默认权限
[root@localhost ~]# umask
0022
解释下,这里的第一个0是特殊权限位,一般不作考虑
022是用户权限位,这里的是掩码值
对于文件
即实际的权限应该是完整的权限777-022-111 = 644
就是rw-r--r--
改变缺省的权限值
[root@localhost ~]# umask 027
[root@localhost ~]# umask
0027