实验内容:
第一个实验
1、创建group2组
2、创建xiaoxiao用户并指定group2作为它的基本组
3、给xiaoxiao用户设置密码为123
4、删除xiaoxiao用户的同时删除与之相关的文件和目录
第二个实验
1、root用户在/tmp下创建一个1.txt的文件,写入123的内容并查看,修改1.txt文件的其他人的权限为读写。
2、root用户在/tmp下创建root.txt文件,写入“这是root”内容。修改其他人的权限为 - 。创建一个xiaohong和xiaoming两个用户,并设置密码xh和xm,让xiaohong可以对root.txt有写的权限,让xiaoming可以对root.txt有读的权限。
3、xiaoming用户在/tmp下创建一个xiaoming.txt的文件,修改xiaoming.txt的属者和属组为xiaohong。
实验思路:
第一个实验:
注意点
-
删除用户的同时删除同名的基本组,如果基本组和用户名不同名,该组不会被删除。如果同名的组不是用户的基本组。同名的组不会被删除。
userdel -r 中的-r
选项的作用是删除用户的同时,也删除与该用户相关的主目录及邮件目录不过在题目中用户xiaoxiao的基本组不是xiaoxiao本身,所以所在的基本组不会被删除,也就是名为xiaoxiao的组不会被删除掉,可以groupdel xiaoxiao 来删掉
-
getent group zhangsan 可以看一个组的详细信息
[root@localhost ~]# getent group zhangsan
zhangsan:x:1003:组名:组的密码:GID:user_list(空)
我们创建了一个用户zhangsan时,创建用户时会创建同名的组,但用户不会自动添加到该组的用户列表中。需要手动将用户添加到组中。
[root@localhost ~]# usermod -aG group2 xiaoming
[root@localhost ~]# getent group group2
group2:x:1001:xiaoming
-aG这样zhangsan这个组中就会有名叫zhangsan的用户列表了usermod 常见选项
-l 修改用户名。 -g 修改用户的基本组 -G 修改用户的附加组。 -a 将用户添加到附加组中
第二个实验:
注意点
- echo "xh" | passwd --stdin xiaohong 常用来设置(修改)用户的密码,其中
--stdin
选项表示从标准输入读取密码。 - 在第三个小实验中通过root来修改sudoers文件,将xiaoming用户设置成可以执行chown命令这样一来就可以通过root提权来修改文件所属组了。
实验代码:
第一个实验
1、创建group2组
2、创建xiaoxiao用户并指定group2作为它的基本组
3、给xiaoxiao用户设置密码为123
4、删除xiaoxiao用户的同时删除与之相关的文件和目录
[root@localhost ~]# groupadd group2
[root@localhost ~]# useradd xiaoxiao
[root@localhost ~]# usermod -g group2 xiaoxiao
[root@localhost ~]# passwd xiaoxiao
Changing password for user xiaoxiao.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# id xiaoxiao
uid=1001(xiaoxiao) gid=1001(group2) groups=1001(group2)
[root@localhost ~]# userdel -r xiaoxiao
userdel: group xiaoxiao not removed because it is not the primary group of user xiaoxiao.
[root@localhost ~]#
[root@localhost ~]# tail -5 /etc/group
dnsmasq:x:978:
tcpdump:x:72:
redhat:x:1000:
group2:x:1001:
xiaoxiao:x:1002:
[root@localhost ~]# getent group xiaoxiao
xiaoxiao:x:1002:
[root@localhost ~]# groupdel xiaoxiao
[root@localhost ~]# tail -5 /etc/group
slocate:x:21:
dnsmasq:x:978:
tcpdump:x:72:
redhat:x:1000:
group2:x:1001:
第二个实验
1、root用户在/tmp下创建一个1.txt的文件,写入123的内容并查看,修改1.txt文件的其他人的权限为读写。
[root@localhost ~]# echo "123" > /tmp/1.txt
[root@localhost ~]# cat /tmp/1.txt
123
[root@localhost ~]# chmod o+rw /tmp/1.txt
2、root用户在/tmp下创建root.txt文件,写入“这是root”内容。修改其他人的权限为 - 。创建一个xiaohong和xiaoming两个用户,并设置密码xh和xm,让xiaohong可以对root.txt有写的权限,让xiaoming可以对root.txt有读的权限。
[root@localhost ~]# echo "这是root" > /tmp/root.txt
[root@localhost ~]# chmod o= /tmp/root.txt
[root@localhost ~]# useradd xiaohong
[root@localhost ~]# echo "xh" | passwd --stdin xiaohong
Changing password for user xiaohong.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# useradd xiaoming
[root@localhost ~]# echo "xm" | passwd --stdin xiaoming
Changing password for user xiaoming.
[root@localhost ~]# setfacl -m u:xiaohong:w /tmp/root.txt
[root@localhost ~]# setfacl -m u:xiaoming:r /tmp/root.txt
[root@localhost ~]# getfacl /tmp/root.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/root.txt
# owner: root
# group: root
user::rw-
user:xiaohong:-w-
user:xiaoming:r--
group::r--
3、xiaoming用户在/tmp下创建一个xiaoming.txt的文件,修改xiaoming.txt的属者和属组为xiaohong。
[root@localhost ~]# su - xiaoming
su: warning: cannot change directory to /home/xiaoming: Permission denied
-bash: /home/xiaoming/.bash_profile: Permission denied
[xiaoming@localhost root]$ touch /tmp/xiaoming.txt
[xiaoming@localhost root]$ su - root
Password:
[root@localhost ~]# visudo
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
xiaoming ALL=(ALL) NOPASSWD: /bin/chown
~
~
~
~
~
~
~
~
~
~
:wq!
[root@localhost ~]# su - xiaoming
su: warning: cannot change directory to /home/xiaoming: Permission denied
-bash: /home/xiaoming/.bash_profile: Permission denied
[xiaoming@localhost root]$ sudo chown xiaohong:xiaohong /tmp/xiaoming.txt