首页 > 系统服务 > 详细

使用shell脚本批量创建用户

时间:2019-05-24 23:21:35      阅读:158      评论:0      收藏:0      [点我收藏+]
1、批量添加与删除特定用户:

①添加用户:

[root@localhost ~]# vim list.txt          #准备一个用户名列表文件
zhangsan
lisi
wangwu
zhaoliu

#保存退出。

[root@localhost ~]# vim useradd.sh
#!/bin/bash
a=$(cat /root/list.txt)
for b in $a
do
        useradd ${b}
        echo "123456" | passwd  --stdin ${b} &> /dev/null
done
#保存退出。
[root@localhost ~]# . useradd.sh 
[root@localhost ~]# tail -5 /etc/passwd
benet:x:1000:1000:benet:/home/benet:/bin/bash
zhangsan:x:1001:1001::/home/zhangsan:/bin/bash
lisi:x:1002:1002::/home/lisi:/bin/bash
wangwu:x:1003:1003::/home/wangwu:/bin/bash
zhaoliu:x:1004:1004::/home/zhaoliu:/bin/bash

②删除刚刚添加的用户:

[root@localhost ~]# vim userdel.sh
#!/bin/bash
a=$(cat /root/list.txt)
for b in $a
do
        userdel -r ${b} &> /dev/null
done
#保存退出
[root@localhost ~]# . userdel.sh             #执行脚本
[root@localhost ~]# tail -1 /etc/passwd
benet:x:1000:1000:benet:/home/benet:/bin/bash

2、批量添加和删除用户名有规律的账号:
①添加:

[root@localhost ~]# vim piliang.sh
#!/bin/bash
name="user"
i=1
while [ $i -le 10 ]
do
        useradd ${name}$i
        echo "123456" | passwd --stdin ${name}$i &> /dev/null
        let i++
done
保存退出
[root@localhost ~]# . piliang.sh                         #执行脚本
[root@localhost ~]# tail /etc/passwd       #查看用户
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
user4:x:1004:1004::/home/user4:/bin/bash
user5:x:1005:1005::/home/user5:/bin/bash
user6:x:1006:1006::/home/user6:/bin/bash
user7:x:1007:1007::/home/user7:/bin/bash
user8:x:1008:1008::/home/user8:/bin/bash
user9:x:1009:1009::/home/user9:/bin/bash
user10:x:1010:1010::/home/user10:/bin/bash

②删除刚才创建的用户:
[root@localhost ~]# vim pish.sh
...........
#!/bin/bash
name="user"
i=1
while [ $i -le 10 ]
do
        userdel -r ${name}$i &> /dev/null
        let i++
done
#保存退出
[root@localhost ~]# . pish.sh 
[root@localhost ~]# tail -2 /etc/passwd
tcpdump:x:72:72::/:/sbin/nologin
benet:x:1000:1000:benet:/home/benet:/bin/bash

使用shell脚本批量创建用户

原文:https://blog.51cto.com/14154700/2399859

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!