由于 CentOS 7.x 的 passwd 已经提供了 --stdin 的功能,因此如果我们可以提供帐号密码的话, 那么就能够很简单的创建起我们的帐号密码了。下面鸟哥制作一个简单的 script 来执行新增用户的功能喔!
[root@study ~]# vim accountadd.sh
#!/bin/bash
# This shell script will create amount of linux login accounts for you.
# 1. check the "accountadd.txt" file exist? you must create that file manually.
# one account name one line in the "accountadd.txt" file.
# 2. use openssl to create users password.
# 3. User must change his password in his first login.
# 4. more options check the following url:
# http://linux.vbird.org/linux_basic/0410accountmanager.php#manual_amount
# 2015/07/22 VBird
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# 0. userinput
usergroup="" # if your account need secondary group, add here.
pwmech="openssl" # "openssl" or "account" is needed.
homeperm="no" # if "yes" then I will modify home dir permission to 711
# 1. check the accountadd.txt file
action="${1}" # "create" is useradd and "delete" is userdel.
if [ ! -f accountadd.txt ]; then
echo "There is no accountadd.txt file, stop here."
exit 1
fi
[ "${usergroup}" != "" ] && groupadd -r ${usergroup}
rm -f outputpw.txt
usernames=$(cat accountadd.txt)
for username in ${usernames}
do
case ${action} in
"create")
[ "${usergroup}" != "" ] && usegrp=" -G ${usergroup} " || usegrp=""
useradd ${usegrp} ${username} # 新增帳號
[ "${pwmech}" == "openssl" ] && usepw=$(openssl rand -base64 6) || usepw=${username}
echo ${usepw} | passwd --stdin ${username} # 建立密碼
chage -d 0 ${username} # 強制登入修改密碼
[ "${homeperm}" == "yes" ] && chmod 711 /home/${username}
echo "username=${username}, password=${usepw}" >> outputpw.txt
;;
"delete")
echo "deleting ${username}"
userdel -r ${username}
;;
*)
echo "Usage: $0 [create|delete]"
;;
esac
done
接下来只要创建 accountadd.txt 这个文件即可!鸟哥创建这个文件里面共有 5 行,你可以自行创建该文件!内容每一行一个帐号。 而是否需要修改密码?是否与帐号相同的信息等等,你可以自由选择!若使用 openssl 自动猜密码时,使用者的密码请由 outputpw.txt 去捞~鸟哥最常作的方法,就是将该文件打印出来,用裁纸机一个帐号一条,交给同学即可!
[root@study ~]# vim accountadd.txt
std01
std02
std03
std04
std05
[root@study ~]# sh accountadd.sh create
Changing password for user std01.
passwd: all authentication tokens updated successfully.
....(后面省略)....
这支简单的脚本你可以在按如下的链接下载:
http://linux.vbird.org/linux_basic/0410accountmanager/accountadd.sh.