一、用户组管理
创建组
groupad命令:groupadd [OPTION]... group_name
-g GID: 指明GID号;[GID_MIN,GID_MAX]
-r: 创建系统组; CentOS 6: ID<500 CentOS 7:ID<1000
组属性修改
groupmod命令:groupmod [OPTION]... group_name
-g:GID,改变组id
-n:改变组名
groupmod -n mutest mysystem
groupmod -g 10010 mytest
删除组
groupdel命令:
groupdel group_name
groupdel mytest
组密码
gpasswd命令:gpasswd [OPTION] GROUP
-a user: 将user添加至指定组中;
-d user: 从指定组中移除用户user
-A user1,user2,...: 设置有管理权限的用户列表
newgrp命令:临时切换基本组; 如果用户本不属于此组,则需要组密码newgroup mytest
更改和查看组成员
groupmems命令:groupmems [options] [action] options:
-g, --group groupname 更改为指定组 (只有root) Actions:
-a, --add username 指定用户加入组
-d, --delete username 从组中删除用户
-p, --purge 从组中清除所有成员
-l, --list 显示组成员列表
groups [OPTION].[USERNAME]... 查看用户所属组列表
二、用户权限管理
文件基本权限说明:[root@centos7 ~]# ll
total 4
-rw-r--r--. 1 root root 11 Aug 4 12:26 file
-:文件类型
rw-:所有者权限
r--:所属组权限
r--:其他用户权限
.:selinux是否开启
1:链接次数,引用次数
root:所属主
root:所有者
11:文件大小
Aug 4 12:26:文件创建时间
file:文件名
文件属性操作:
修改文件的属主、属组chown [OPTION]... [OWNER][:[GROUP]] FILE...
用法:
OWNER
OWNER:GROUP
:GROUP
命令中的冒号可用.替换;
-R: 递归 chown [OPTION]... --reference=RFILE FILE...
chown -R nginx.nginx /var/html/www/
chown gentoo:hadoop file
chown :root file
chown hadoop file
chown --reference=/etc/shadow file
修改文件属组,只有管理员才有此权利:chgrp [OPTION]... GROUP FILE... chgrp[OPTION]...
--reference=RFILE FILE...
-R 递归
chgrp gentoo chen.txt
chgrp --reference=/tmp/chen.out /root/file
chgrp -R hadoop /var/html/www/
文件权限详解:r:读 w:写 x:执行 r=4 w=2 x=1
管理员创建文件默认权限如下:
[root@centos7 ~]# ll
total 4
-rw-r--r--. 1 root gentoo 0 Aug 4 13:20 chen.txt
drwxr-xr-x. 2 root root 6 Aug 4 13:27 dir
-rw-r--r--. 1 root root 11 Aug 4 12:26 file
[root@centos7 ~]# umask
0022
umask:遮罩码
管理员:
目录权限:777-umask 755
文件权限:666-umask 644
[root@centos7 ~]# su - gentoo
[gentoo@centos7 ~]$ touch file
[gentoo@centos7 ~]$ mkdir dir
[gentoo@centos7 ~]$ ll
total 0
drwxrwxr-x. 2 gentoo gentoo 6 Aug 4 13:32 dir
-rw-rw-r--. 1 gentoo gentoo 0 Aug 4 13:31 file
[gentoo@centos7 ~]$ umask
0002
普通用户:
目录权限:777-umask 775
文件权限:666-umask 664
umask原理:
umask从目录文件上屏蔽掉最大权限相应的位,从而得出默认的权限
dir:777-umask
file:666-umaks 如果是基数加一,偶数不变
umask值 可以用来保留在创建文件权限
新建FILE权限: 666-umask
如果所得结果某位存在执行(奇数)权限,则将其权限+1
新建DIR权限: 777-umask
非特权用户umask是 002
root的umask 是 022
umask: 查看
umask #: 设定
umask002
umask –S 模式方式显示
umask –p 输出可被调用
全局设置: /etc./bashrc 用户设置::~/.bashrc
设定文件属性命令:chattr [ -RVf ] [ -v version ] [ mode ] files...
chattr +A 加上则访问文件三时间不变
chattr +i 不能删除,改名,更改
chattr -i 解锁
chattr +a 只能增加
chattr -a
lsattr 显示特定属性
[root@centos7 ~]# chattr +i file
[root@centos7 ~]# lsattr file
----i----------- file
[root@centos7 ~]# rm -rf file
rm: cannot remove ‘file’: Operation not permitted
[root@centos7 ~]# echo "love">>file
-bash: file: Permission denied
[root@centos7 ~]# chattr -i file..
[root@centos7 ~]# chattr +a file
[root@centos7 ~]# lsattr file
-----a---------- ./file
[root@centos7 ~]# rm -rf file
rm: cannot remove ‘file’: Operation not permitted
[root@centos7 ~]# echo "love" >> file
[root@centos7 ~]# cat file
love
三、访问控制列表ACL:Access ControlList,实现灵活的权限管理
除了文件的所有者,所属组和其它人,可以对更多的用户设置权限
CentOS7.0默认创建的xfs和ext4文件系统有ACL功能。
CentOS7.X之前版本,默认手工创建的ext4文件系统无ACL 功能。
需手动增加: tune2fs –o acl /dev/sdb1 mount –oacl /dev/sdb1 /mnt
ACL生效顺序:所有者,自定义用户,自定义组,其他人
ACL文件上的group权限是mask 值(自定义用户,自定义组 ,拥有组的最大权限),而非传统的组权限
getfacl 可看到特殊权限:flags
默认ACL权限给了x,文件也不会继承x权限。
base ACL 不能删除
setfacl -k dir 删除默认ACL权限
setfacl -b file1清除所有ACL权限
getfacl file1 | setfacl --set-file=-file2 复制file1 的acl权限给file2
setfacl命令:
setfacl - set file access control lists
setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
选项:-b:删除所有权限
-k:清除默认权限
-m,x:定义acl权限
-R:递归
setfacl -m u:wang:rwx file|directory
setfacl -Rm g:sales:rwX directory
setfacl -M file.acl file|directory
setfacl -m g:salesgroup:rw file|directory
setfacl -m d:u:wang:rx directory
setfacl -x u:wang file |directory
setfacl -X file.acl directory
setfacl -m m::rw file