加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用shell脚本批量创建用户

发布时间:2020-12-15 21:50:12 所属栏目:安全 来源:网络整理
导读:1、批量添加与删除特定用户: ①添加用户: [[email?protected] ~]# vim list.txt #准备一个用户名列表文件zhangsanlisiwangwuzhaoliu#保存退出。[[email?protected] ~]# vim useradd.sh#!/bin/basha=$(cat /root/list.txt)for b in $ado useradd ${b} echo
1、批量添加与删除特定用户:

①添加用户:

[[email?protected] ~]# vim list.txt          #准备一个用户名列表文件
zhangsan
lisi
wangwu
zhaoliu

#保存退出。

[[email?protected] ~]# 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
#保存退出。
[[email?protected] ~]# . useradd.sh 
[[email?protected] ~]# 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

②删除刚刚添加的用户:

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

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

[[email?protected] ~]# 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
保存退出
[[email?protected] ~]# . piliang.sh                         #执行脚本
[[email?protected] ~]# 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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读