本文将介绍Linux中的文件权限管理相关内容

用户管理
创建用户
在使用root用户创建完用户后,可通过paswwd命令设置新用户的密码。值得一提的是,非root用户执行passwd命令时不可指定用户名,即只能修改当前用户的密码

删除用户

通过添加-r选项,则会同时删除相应的HOME用户目录

用户组管理
创建组

修改组名
1 2
| groupmod -n <新组名> <旧组名>
|

删除组

修改用户所属组
Linux当中,一个用户有且只有一个主要组,但可以有若干个(含0个)次要组
1. 修改用户的主要组
1 2 3 4 5
| usermod -g <组名> <用户名>
id <用户名>
|

2. 修改用户的次要组
1 2
| usermod -G <组名1>[,<组名2>,<组名3>,...] <用户名>
|

3. 追加组到用户的次要组当中
1 2
| usermod -a -G <组名1>[,<组名2>,<组名3>,...] <用户名>
|

4. 从用户的次要组中删除指定组

文件所有权、权限
修改文件所有者、所属组
chown命令可以修改文件的文件所有者、所属组。但普通用户无权执行该命令,只有root账户才可以。在chown命令中,所有者与所属组之间可以使用 点(.)或 冒号(:) 两种方式进行连接。但会产生一个问题,如果用户的账号中包含数点(例如 aaron.com)会造成系统误判。故推荐使用冒号(:)连接所有者和所属组
1 2 3 4 5 6 7 8 9 10 11 12 13
| chown [用户名] readme.txt
chown .[组名] readme.txt
chown :[组名] readme.txt
chown [用户名].[组名] readme.txt
chown [用户名]:[组名] readme.txt
|

使用-R选项可递归修改文件、目录的所有者、所属组

修改文件所属组
与chown命令不同,chgrp命令允许普通用户修改文件所属组。但该用户必须同时是原属组和新属组的成员。使用 -R选项 即可对指定目录下的所有文件及子目录进行递归处理
1 2 3 4 5 6 7 8
| chgrp [组名] [文件名]
chgrp G4 a.txt
chgrp -R G2 work
|

修改文件权限
可通过chmod命令修改文件权限,命令形式如下
1
| chmod [option] <mode> <file>
|
mode参数支持直接使用3位八进制数字来设置文件权限

使用-R选项可递归修改文件、目录权限

此外,mod参数还支持使用符号模式
其中:
- u、g、o、a :分别代表文件所有者user、文件的所属组group、其它用户other、所有用户all(相当于ugo的并集)
- +、-、= :分别表示对指定的用户在现有权限的基础上增加权限(+)、移除权限(-)、设置权限(=)
- r、w、x :分别表示读权限、写权限、执行权限


参考文献
- Linux命令行与shell脚本编程大全·第4版 Richard Blum、Christine Bresnahan著