day22:用户管理

主要内容:

查询用户系列命令
保护用户密码
批量创建用户并设置随机密码

一、查询用户系列命令

1.id

作用:显示用户的UID GID
显示用户所属组;判断用户是否存在

[root@oldedu59 ~]# id 
uid=0(root) gid=0(root) groups=0(root)
[root@oldedu59 ~]# id oldboy
uid=1000(oldboy) gid=1000(oldboy) groups=1000(oldboy)
[root@oldedu59 ~]# id oldgirl
id: oldgirl: no such user
[root@oldedu59 ~]# 
2.w

作用:查询负载信息
显示谁登陆了系统在做什么;里面的信息是uptime和who的集合

[root@oldedu59 ~]# w
 16:28:08 up 3 days,  3:13,  3 users,  load average: 0.01, 0.02, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1                      17Apr19  6:54m  0.26s  0.26s -bash
root     pts/0    10.0.0.1         08:21    4:23m  2.04s  0.07s -bash
root     pts/1    10.0.0.1         14:22    0.00s  0.57s  0.10s w
w每一部的含义.png

(1)uptime: 查询负载
(2)who:显示登陆信息

[root@oldedu59 ~]# uptime 
 16:28:47 up 3 days,  3:13,  3 users,  load average: 0.01, 0.02, 0.05
[root@oldedu59 ~]# who
root     tty1         2019-04-17 08:34
root     pts/0        2019-04-25 08:21 (10.0.0.1)
root     pts/1        2019-04-25 14:22 (10.0.0.1)
3.whoami:显示当前用户的名字
[root@oldedu59 ~]# whoami
root
[root@oldedu59 ~]# su - oldboy
Last login: Thu Apr 25 16:02:21 CST 2019 on pts/1
[oldboy@oldedu59 ~]$ whoami
oldboy
4.last:显示用户的用户的登陆信息 谁 什么时候 从哪里登陆 什么时候推出;根据时间段找出谁登陆了系统
[oldboy@oldedu59 ~]$ last
root     pts/1        10.0.0.1         Thu Apr 25 14:22   still logged in   
root     pts/0        10.0.0.1         Thu Apr 25 08:21   still logged in   
root     pts/1        10.0.0.1         Wed Apr 24 14:54 - 08:46  (17:51)    
root     pts/0        10.0.0.1         Wed Apr 24 11:59 - 17:04  (05:04)    
root     pts/1        10.0.0.1         Wed Apr 24 08:38 - 11:59  (03:21)    
root     pts/1        10.0.0.1         Wed Apr 24 08:30 - 08:38  (00:07)    
root     pts/0        10.0.0.1         Tue Apr 23 14:11 - 10:20  (20:09)    
5.lastlog:显示系统中所有用户最近一次的登录时间
[oldboy@oldedu59 ~]$ lastlog
Username         Port     From             Latest
root             pts/1    10.0.0.1         Thu Apr 25 14:22:00 +0800 2019
bin                                        **Never logged in**
oldboy           pts/1                     Thu Apr 25 16:29:52 +0800 2019
stu5             pts/0                     Thu Apr 25 12:04:45 +0800 2019
stu6                                       **Never logged in**

二、保护用户密码

1.数字 大写字母 小写字母 符号

''或""或_或-
12位以上的 包含上面3种

2.记录密码

keepass 开源软件

三、批量添加用户并设置随机密码

1.批量添加用户stu01 stu02 stu03 并设置密码为123456

(1)生成用户名

[root@oldboyedu59 ~]# echo stu{01..3} |xargs -n1 
stu01
stu02
stu03

(2)命令拼接

利用sed/awk 里面

sed 's###g' 后两个井号之间写什么 屏幕就显示什么
awk 利用 "" 双引号里面写什么 屏幕就显示什么

[root@oldboyedu59 ~]# echo stu{01..3} |xargs -n1 |sed -r 's#.*#useradd &;echo 123456|passwd --stdin &#g'
useradd stu01;echo 123456|passwd --stdin stu01
useradd stu02;echo 123456|passwd --stdin stu02
useradd stu03;echo 123456|passwd --stdin stu03

sed中&含义

& sed命令中s###g 表示前面正则匹配到的内容

(3)此时命令是否能够执行

[root@oldboyedu59 ~]# useradd stu01;echo 123456|passwd --stdin stu01
useradd: user 'stu01' already exists
Changing password for user stu01.
passwd: all authentication tokens updated successfully.

(4)把命令交给bash运行

[root@oldboyedu59 ~]# echo stu{01..3} |xargs -n1 |sed -r 's#.*#useradd &;echo 123456|passwd --stdin &#g'|bash
useradd: user 'stu01' already exists
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
useradd: user 'stu02' already exists
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
useradd: user 'stu03' already exists
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
[root@oldboyedu59 ~]# grep 'stu' /etc/passwd
stu01:x:1002:1002::/home/stu01:/bin/bash
stu02:x:1003:1003::/home/stu02:/bin/bash
stu03:x:1004:1004::/home/stu03:/bin/bash
2.批量添加用户并设置8位随机密码

(1)生成用户名

[root@oldboyedu59 ~]# echo stu{01..3} |xargs -n1 
stu01
stu02
stu03

(2)生成随机密码

tr命令 +head

[root@oldedu59 ~]# tr -cd '0-9a-zA-Z' 

date + md5sum

[root@oldedu59 ~]# date +%N |md5sum |head -c8
194d3856[root@oldedu59 ~]# date +%N |md5sum |head -c8
51b2f3b7[root@oldedu59 ~]# date +%N |md5sum |head -c8
1d4172da[root@oldedu59 ~]# date +%N |md5sum |head -c8
55fea06b[root@oldedu59 ~]# 

(3)命令拼接:

useradd stu06;
pass=`date +%N`;
echo $pass |passwd --stdin stu06 ;
echo $pass stu06 >>/root/pass.txt

[root@oldboyedu59 ~]# echo stu{1..3} |xargs -n1 |sed -r 's#.*#useradd &;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin &;echo $pass & >>pass.txt#g'
useradd stu1;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin stu1;echo $pass stu1 >>pass.txt
useradd stu2;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin stu2;echo $pass stu2 >>pass.txt
useradd stu3;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin stu3;echo $pass stu3 >>pass.txt

(4)测试命令

[root@oldboyedu59 ~]# useradd stu1;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin stu1;echo $pass stu1 >>pass.txt
Changing password for user stu1.
passwd: all authentication tokens updated successfully.

(5)交于bash执行

[root@oldboyedu59 ~]# echo stu{1..3} |xargs -n1 |sed -r 's#.*#useradd &;pass=`date +%N|md5sum|head -c8`;echo $pass|passwd --stdin &;echo $pass & >>pass.txt#g'|bash
useradd: user 'stu1' already exists
Changing password for user stu1.
passwd: all authentication tokens updated successfully.
Changing password for user stu2.
passwd: all authentication tokens updated successfully.
Changing password for user stu3.
passwd: all authentication tokens updated successfully.
[root@oldboyedu59 ~]# grep 'stu' /etc/passwd
stu1:x:1014:1014::/home/stu1:/bin/bash
stu2:x:1015:1015::/home/stu2:/bin/bash
stu3:x:1016:1016::/home/stu3:/bin/bash

你可能感兴趣的:(day22:用户管理)