练习一:
不使用useradd,passwd,groupadd命令,手工修改配置文件,实现创建一个用户qq(UID320),私有组是tencent(GID314),密码是123,用户的主目录是/qq,最终要求可以使用qq用户成功登录。
注意:
1.要先创建组/etc/group
2. 创建家目录/etc/qq,最重要的是从/etc/skel/中复制所有隐藏文件到家目录下,命令提示符才是正确的输入
3. 再去/etc/passwd最后一行输入用户qq的账户信息
4. 去/etc/shadow最后一行输入用户密码信息,密码是123的话,将另外一个用户密码设置为123,将qq用户的第二字段密码复制粘贴
[root@localhost bb]# vim /etc/passwd
[root@localhost bb]# tail -1 /etc/passwd
qq:x:320:314::/qq:/bin/
[root@localhost bb]# vim /etc/group
[root@localhost bb]# tail -2 /etc/group
tencent:x:314:qq
[root@localhost bb]# id qq
uid=320(qq) gid=314(tencent) groups=314(tencent)
[root@localhost bb]# su - qq
su: warning: cannot change directory to /qq: No such file or directory
[root@localhost bb]# cd /
[root@localhost /]# mkdir qq
[root@localhost /]# su - qq
-bash-4.1$
[root@localhost ~]# su - mima
[mima@localhost ~]$ su - qq
Password:
-bash-4.1$ (用户家目录文件来源:/etc/skel,手工添加的要复制文件到qq的家目录)
[root@localhost ~]# cp -r /etc/skel /qq
[root@localhost ~]# cp /etc/login.defs /qq
[root@10 skel]# cp .bash_logout .bash_profile .bashrc /home/qq
最后运行结果
[qq@10 ~]$
如何知道密码123的密文
[root@localhost bb]# useradd mima
[root@localhost bb]# tail -1 /etc/shadow
mima:!!:17898:0:99999:7:::
[root@localhost ~]# passwd mima
Changing password for user mima.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost ~]# tail -2 /etc/shadow
mima:$6$ItWtisvL$ne9H9rO/r2t9xNzCNjhrNwnXaRJIvH4g6CcI1TvmhzchUyf8JD31CW.uDPOeD4GLLMbZKoMvDol8oJL6tb5oR/:17898::::::
qq:$6$ItWtisvL$ne9H9rO/r2t9xNzCNjhrNwnXaRJIvH4g6CcI1TvmhzchUyf8JD31CW.uDPOeD4GLLMbZKoMvDol8oJL6tb5oR/:17898::::::
练习二:
1)新建2个组group1,group2
2)更改组group2的GID为103,更改组名为grouptest
3)删除组grouptest
[root@localhost ~]# groupadd group1
[root@localhost ~]# groupadd group2
[root@localhost ~]# groupmod -g 103 -n grouptest group2
[root@localhost ~]# tail -2 /etc/group
group1:x:505:
grouptest:x:103:
[root@localhost ~]# groupdel grouptest
[root@localhost ~]# tail -2 /etc/group
group1:x:505:
4) 新建用户user1,指定UID为777,目录为/opt/user1,主要组为group1,次要组为root,指定shell为/bin/bash
[root@localhost ~]# useradd -u 777 -g group1 -G root -s /bin/bash -d /opt/user1 user1
useradd: group 'group1' does not exist
[root@localhost ~]# groupadd group1
[root@localhost ~]# useradd -u 777 -g group1 -G root -s /bin/bash -d /opt/user1 user1
[root@localhost ~]# id user1
uid=777(user1) gid=500(group1) groups=500(group1),0(root)
(5) 使用命令修改用户user1的个人说明为This is a test
[root@localhost home]# usermod -c 'This is a test' user1
(6) 修改用户user1密码过期时间为2018-07-27
[root@localhost ~]# chage --help
-E, --expiredate EXPIRE_DATE set account expiration date to EXPIRE_DATE
[root@localhost ~]# chage -E '2018-07-27' user1
(7)用户user1的密码为111111,加锁用户user1并查看/etc/shadow,用户user1通过ssh登录,用root用户解锁user1,再通过ssh登录.
[root@localhost home]# passwd user1
Changing password for user user1.
New password:
BAD PASSWORD: it does not contain enough DIFFERENT characters
BAD PASSWORD: is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
[root@localhost home]# passwd -l user1
Locking password for user user1.
passwd: Success
[root@localhost home]# tail -1 /etc/shadow
user1:!!$6$td46MU1g$E2pPy/0mCPjjYLKkPsfk9m98NASbq/9CQmMHhU9/tEhrMTmeJ0bqREAU0AHr5Gqob57CCZh9ewC/wgpCn.ijI/:17898:0:99999:7:::
Xshell:\> ssh 172.16.10.14 (不能用user1用户登录,说明锁定成功)
(8) 更改用户主目录/home/user1为/home/user11
[root@localhost ~]# usermod -d /home/user11 user1
(9)将root更改为csh,并查看结果,并将root改为bash
[root@localhost ~]# usermod -l csh root
usermod: user root is currently used by process 1
(10) 列出用户user1的UID,GID等
[root@localhost ~]# id user1
uid=777(user1) gid=505(group1) groups=505(group1),0(root)
(11) 增加用户user3,增加组testgroup,将root,user1,user3加入到testgroup
[root@localhost ~]# useradd user3
[root@localhost ~]# groupadd testgroup
[root@localhost ~]# gpasswd -a root testgroup
Adding user root to group testgroup
[root@localhost ~]# gpasswd -a user1 testgroup
Adding user user1 to group testgroup
[root@localhost ~]# gpasswd -a user3 testgroup
Adding user user3 to group testgroup
(16) 使用passwd给user1用户密码锁定,用passwd查看user1密码锁定信息,最后用passwd给用户user1解锁
[root@localhost ~]# passwd -l user1
Locking password for user user1.
passwd: Success
[root@localhost ~]# passwd -S user1
user1 LK 2019-01-02 0 99999 7 -1 (Password locked.)
[root@localhost ~]# passwd -u user1
Unlocking password for user user1.
(17)创建一个用户mandriva,其ID号为2002,基本组为distro(组ID为3003),附加组为linux
[root@localhost ~]# groupadd -g 3003 distro
[root@localhost ~]# groupadd linux
[root@localhost ~]# useradd -u 2002 -g distro -G linux mandriva
Creating mailbox file: No such file or directory
[root@localhost ~]# id mandriva
uid=2002(mandriva) gid=3003(distro) groups=3003(distro),3004(linux)
(18)创建一个用户fedora,其全名为Fedora Community,默认shell为/bin/tcsh
[root@10 home]# useradd -c "Fedora Community" -s /bin/bash fedora
(19)修改mandriva的ID号为4004,基本组为linux,附加组为distro和fedora
[root@localhost ~]# usermod -u 4004 -g linux -G distro,fedora mandriva
[root@localhost ~]# id mandriva
uid=4004(mandriva) gid=3004(linux) groups=3004(linux),3003(distro),2003(fedora)
(20)给fedora加密码,并设定其密码最短使用期限为2天,最长为50天。
[root@localhost ~]# passwd --help
-x, --maximum=DAYS maximum password lifetime (root only)
-n, --minimum=DAYS minimum password lifetime (root only)
[root@localhost ~]# passwd -x 50 -n 2 fedora
Adjusting aging data for user fedora.
passwd: Success
(21)将mandriva的默认shell改为/bin/bash
[root@localhost ~]# usermod -s /bin/bash mandriva
22)添加系统用户hbase,且不允许其登陆系统。
[root@localhost ~]# useradd -s /sbin/nologin hbase