标签:system div bee root用户 密码 www. 怎么 需要 print
有时候在初始化shell脚本中希望能顺便创建用户并指定密码,使用useradd命令可以达到该效果:
useradd -m -p encryptedPassword username
参数说明:
使用perl的crypt()方法即可加密明文密码,示例如下:
$ perl -e ‘print crypt("password", "salt"),"\n"‘
输出如下:
sa3tHJ3/KuYvI
对于useradd命令需要用到的加密密码,使用‘password‘作为盐,具体语法如下:
$ password="1YelloDog@"
$ pass=$(perl -e ‘print crypt($ARGV[0], "password")‘ $password)
$ echo $pass
paU5t8Al/qf6M
示例shell脚本用于创建用户:
adduser.sh
#!/bin/bash
# Script to add a user to Linux system
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e ‘print crypt($ARGV[0], "password")‘ $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
在root用户下执行该脚本:
# ./adduser.sh
Enter username : roja
Enter password : HIDDEN
User has been added to system!
...
#如果是生产环境,为了防止明文密码泄露,可以对shell脚本进行加密
password="py@123"
username="py"
pass=$(perl -e ‘print crypt($ARGV[0], "password")‘ $password)
useradd -m -p $pass $username
...
参考链接:
https://www.cyberciti.biz/tips/howto-write-shell-script-to-add-user.html
标签:system div bee root用户 密码 www. 怎么 需要 print
原文地址:https://www.cnblogs.com/cord/p/9226505.html