Top

Linux-批量创建账号并配置密码

一般创建账号和配置密码

1. 创建账号

[root@admin ~]# useradd -s /bin/bash username

2. 创建密码

[root@admin ~]# passwd username
Changing password for user username.
New password: `userpassword`
Retype new password: `userpassword`
passwd: all authentication tokens updated successfully.
\\这样创建密码如果需要批量创建的时候就变得很烦恼了.

使用脚本批量创建账号密码

1. 使用passwd的–stdin

[root@admin ~]# cat test-script.sh 
#!/bin/bash
##auto-create-user|password##
for i in `seq 1 $3`;
do
useradd -s /bin/bash $1$i
echo "$2" | passwd --stdin $1$i >/dev/null 2>&1
echo "success $1$i"
done
\\执行看看.
[root@admin ~]# bash test-script2.sh user0 123456 9
success user01
success user02
success user03
success user04
success user05
success user06
success user07
success user08
success user09
\\视乎Debian中的passwd没有--stdin选项

2. 使用chpasswd命令

[root@admin ~]# cat test-script2.sh 
#!/bin/bash
##auto-create-user|password##
for i in `seq 1 $3`;
do
useradd -s /bin/bash $1$i
echo "$1$i:$2" |chpasswd
echo "success $1$i"
done
\\执行看看.
[root@admin ~]# bash test-script2.sh user0 123456 9
success user01
success user02
success user03
success user04
success user05
success user06
success user07
success user08
success user09

3. 使用openssl passwd命令

[root@admin ~]# cat test-script3.sh 
#!/bin/bash
##auto-create-user|password##
for i in `seq 1 $3`;
do
useradd -s /bin/bash -p `openssl passwd -1 $2` $1$i
echo "success $1$i"
done
\\执行看看.
[root@admin ~]# bash test-script3.sh user0 123456 9
success user01
success user02
success user03
success user04
success user05
success user06
success user07
success user08
success user09