4.23笔记整理 用户管理
查询用户:
id 显示用户uid,gid 显示属组,查看用户是否存在
w 查询负载,显示谁在登录系统,在干啥
whami 我是谁
last 用户登录信息,谁,从哪登录,什么时间登录,什么时间退出,根据时间找出谁登录了系统
lastlog 显示所有用户最后登录的时间
[root@guanggege ~]# w
17:36:20 up 8:54, 2 users, load average: 0.01, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 10.0.0.1 08:45 4.00s 2.67s 0.00s w
oldboy pts/1 10.0.0.1 09:22 8:07m 0.06s 0.06s -bash
[root@guanggege ~]#
w每一部分含义:
批量添加用户并设置密码
如题:批量添加用户stu01,stu02,stu03,并设置密码为123456(简化版)
- 生成用户名
[root@guanggege ~]# echo stu{01..03}|xargs -n1
stu01
stu02
stu03
[root@guanggege ~]#
- 命令拼接:
使用sed/awk,sed’s###g’ 后两个#之间写什么就显示什么
awk “ ” 双引号里写什么就显示什么
[root@guanggege ~]# echo stu{01..03}|xargs -n1|sed -r 's#(.*)#useradd\1;echo 123456|passwd --stdin \1#g'
useraddstu01;echo 123456|passwd --stdin stu01
useraddstu02;echo 123456|passwd --stdin stu02
useraddstu03;echo 123456|passwd --stdin stu03
[root@guanggege ~]#
- 交给bash执行,执行前先用一个结果测试
[root@guanggege ~]# useraddstu03;echo 123456|passwd --stdin stu03
-bash: useraddstu03: command not found
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
[root@guanggege ~]# echo stu{01..03}|xargs -n1|sed -r 's#(.*)#useradd\1;echo 123456|passwd --stdin \1#g'|bash
bash: line 1: useraddstu01: command not found
Changing password for user stu01.
passwd: all authentication tokens updated successfully.
bash: line 2: useraddstu02: command not found
Changing password for user stu02.
passwd: all authentication tokens updated successfully.
bash: line 3: useraddstu03: command not found
Changing password for user stu03.
passwd: all authentication tokens updated successfully.
[root@guanggege ~]#
如题:批量添加用户stu04,stu05,stu06,并设置随机8位密码
- 生成用户名
[root@guanggege ~]# echo stu{01..03}|xargs -n1
stu01
stu02
stu03
[root@guanggege ~]#
- 生成随机密码:
① tr+head
[root@guanggege ~]# tr -cd 'a-zA-Z0-9'
② date+md5sum+head
[root@guanggege ~]# date +%N
768589518
[root@guanggege ~]#
- 命令拼接:
Useradd stu06
Pass=date +%N
Echo $pass|passwd --stdin stu06
Echo $pass stu06 >> /root/pass.txt
[root@guanggege ~]# echo stu{04..06}|xargs -n1|sed -r 's#(.*)#useradd \1;p=`date +%N`;echo $p|passwd --stdin \1;echo $p \1>>/oldboy/p.txt#g'
useradd stu04;p=`date +%N`;echo $p|passwd --stdin stu04;echo $p stu04>>/oldboy/p.txt
useradd stu05;p=`date +%N`;echo $p|passwd --stdin stu05;echo $p stu05>>/oldboy/p.txt
useradd stu06;p=`date +%N`;echo $p|passwd --stdin stu06;echo $p stu06>>/oldboy/p.txt
[root@guanggege ~]#
- 测试其中一行,成功后交给bash执行
[root@guanggege ~]# useradd stu04;p=`date +%N`;echo $p|passwd --stdin stu04;echo $p stu04>>/oldboy/p.txt
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
[root@guanggege ~]# cat /oldboy/p.txt
stu01
stu01
261011725 stu04
[root@guanggege ~]#
[root@guanggege ~]# echo stu{04..06}|xargs -n1|sed -r 's#(.*)#useradd \1;p=`date +%N`;echo $p|passwd --stdin \1;echo $p \1>>/oldboy/p.txt#g'|bash
useradd: user 'stu04' already exists
Changing password for user stu04.
passwd: all authentication tokens updated successfully.
useradd: user 'stu05' already exists
Changing password for user stu05.
passwd: all authentication tokens updated successfully.
useradd: user 'stu06' already exists
Changing password for user stu06.
passwd: all authentication tokens updated successfully.
[root@guanggege ~]#