linux用户和组管理及权限管理
useradd命令添加用户时使用的参数
-g设置主组,
-G设置基本组,
-s这种默认shell,
-M不创建家目录等内容。
熟悉rwx权限分别应用在文件和目录上的意义,八进制数子表示权限。
理解什么情况下需要设置特殊权限suid、sgid、sticky、acl。
1、useradd命令:在系统中添加用户
【例1】添加一个用户名称为:dushan
[root@centos7 ~]#useradd dushan
[root@centos7 ~]#id dushan
uid=1001(dushan) gid=1001(dushan) groups=1001(dushan)
【例2】在系统上添加一个用户名称为apache的用户,默认shell为/sbin/nolgoin且不创建家目录
[root@centos7 ~]# useradd -s /sbin/nologin -M apache
usermod命令:变更用户设置
【例】改变用户dushan的主组为g1,添加附加组g2并且保留原来附加组g3
[root@centos7 data]#usermod -g g1 -aG g2 dushan
[root@centos7 data]#id dushan
uid=1001(dushan) gid=1003(g1) groups=1003(g1),1004(g2),1005(g3)
2、groupadd命令:添加组
【例3】添加develop组
[root@centos7 ~]# groupadd develop
3、chown命令:修改文件所属关系
【例4】设置/data/dir目录及其子目录和文件属主属组为apache
[root@centos7 data]#chown -R apache.apache /data/dir
[root@centos7 data]#ll
total 8
lrwxrwxrwx 1 root root 19 Sep 6 09:47 apachectl -> /usr/sbin/apachectl
drwxr-xr-x 3 apache apache 33 Sep 6 09:59 dir
-rw-r--r-- 1 root root 98 Sep 6 12:20 fstab.out
-rw-r--r-- 1 root root 170 Sep 3 10:26 functions.bak
注意:chown -R apache.apache等价于chown -R apache:apache。
4、chmod命令:改变文件权限
【例5】修改权限为属主添加写和执行权限、属组没有读权限其它人有读和执行权限
[root@centos7 data]#touch checkid.sh
[root@centos7 data]#ll checkid.sh
-rw-r--r-- 1 root root 0 Sep 6 15:37 checkid.sh
[root@centos7 data]#chmod u+wx,g-r,o=rx checkid.sh
[root@centos7 data]#ll checkid.sh
-rwx---r-x 1 root root 0 Sep 6 15:37 checkid.sh
【例6】递归设置testdir目录权限为:属组添加可读、可写、可执行权限,但其子文件不添加执行权限
[root@centos7 ~]# chmod -R g=rwX testdir/
注意:大写X不添加给子目录执行,x添加执行权限
【例7】设置文件fstab.out权限为只有属主有读写权限
[root@centos7 data]#ll fstab.out
-rw-r--r-- 1 root root 98 Sep 6 12:20 fstab.out
[root@centos7 data]#chmod 600 fstab.out
[root@centos7 data]#ll fstab.out
-rw------- 1 root root 98 Sep 6 12:20 fstab.out
5、passwd命令:设置用户密码
【例8】修改用户dushan的登录密码
[root@centos7 ~]#passwd dushan
Changing password for user dushan.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
或
[root@centos7 ~]#echo 'dushan' | passwd --stdin dushan
Changing password for user dushan.
passwd: all authentication tokens updated successfully.
6、su命令:切换用户
【例9】完全切换dushan用户身份
[root@centos7 ~]#su - dushan
Last login: Thu Sep 6 16:26:43 CST 2018 on pts/0
[dushan@centos7 ~]$ pwd
/home/dushan
【例10】不完全切换到linux用户身份
[root@centos7 ~]#su dushan
[dushan@centos7 root]$ pwd
/root
注:su只是切换了root身份,但Shell环境仍然是普通用户的Shell;而su -连用户和Shell环境一起切换成root身份了。只有切换了Shell环境才不会出现PATH环境变量错误,报command not found的错误。
7、SUID权限设置
【例11】设置二进制可执行程序文件chmod拥有suid权限
[root@centos7 ~]#cp /usr/bin/chmod /data/
[root@centos7 ~]#cd /data
[root@centos7 data]#ll chmod
-rwxr-xr-x 1 root root 58584 Sep 6 16:31 chmod
[root@centos7 data]#chmod u+s chmod
[root@centos7 data]#ll chmod
-rwsr-xr-x 1 root root 58584 Sep 6 16:31 chmod
注:继承属主的权限 如/usr/bin/passwd,否则普通用户无法修改密码,vim如设置suid则普通用户可以自行修改/etc/passwd文件身份变成root!
[root@centos7 ~]#ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10 2014 /usr/bin/passwd
【例12】取消二进制可执行程序文件chmod拥有的suid权限
[root@centos7 data]#chmod u-s chmod
[root@centos7 data]#ll chmod
-rwxr-xr-x 1 root root 58584 Sep 6 16:31 chmod
8、SGID权限设置
【例13】设置二进制可执行程序文件chmod拥有sgid权限
[root@centos7 data]#chmod g+s chmod
[root@centos7 data]#ll chmod
-rwxr-sr-x 1 root root 58584 Sep 6 16:31 chmod
【例14】取消二进制可执行程序文件chmod拥有的sgid权限
[root@centos7 data]#chmod g-s chmod
[root@centos7 data]#ll chmod
-rwxr-xr-x 1 root root 58584 Sep 6 16:31 chmod
【例15】对/data目录设置sgid权限,作为协作目录
[root@centos7 ~]#ll -d /data
drwxrwxr-x. 3 root root 85 Sep 6 16:31 /data
[root@centos7 ~]#chmod g+s /data
[root@centos7 ~]#ll -d /data
drwxrwsr-x. 3 root root 85 Sep 6 16:31 /data
【例16】取消testdir目录sgid权限
[root@centos7 ~]#chmod g-s /data
[root@centos7 ~]#ll -d /data
drwxrwxr-x. 3 root root 85 Sep 6 16:31 /data
9、Sticky权限设置
【例17】对/data目录设置sticky权限,实现只有文件的所有者或root才能删除该目录下的文件
[root@centos7 data]#ll -d /data
drwxrwxr-x. 3 root root 85 Sep 6 17:07 /data
[root@centos7 data]#chmod o+t /data
[root@centos7 data]#ll -d /data
drwxrwxr-t. 3 root root 85 Sep 6 17:07 /data
【例18】取消testdir目录的sticky权限
[root@centos7 data]#chmod o-t /data
[root@centos7 data]#ll -d /data
drwxrwxr-x. 3 root root 85 Sep 6 17:07 /data
10、acl特殊权限
【例19】在/data/dir里创建的新文件自动属于g1组;
dushan用户对/data/dir文件夹只有读权限
[root@centos7 ~]#setfacl -Rm g:g1:rwx /data/dir
[root@centos7 ~]#setfacl -m u:dushan:r-- /data/dir
[root@centos7 ~]#getfacl /data/dir
getfacl: Removing leading '/' from absolute path names
# file: data/dir
# owner: root
# group: root
user::rwx
user:dushan:r--
group::r-x
group:g1:rwx
mask::rwx
other::r-x
【例20】删除全部acl权限:
[root@centos7 data]#setfacl -b /data/dir