mun*_*ish 11 linux shell administration shell-script useradd
我找到了一个指南,解释了如何设置用户密码。我正在尝试使其自动化并向用户发送电子邮件,例如:
userid created with password XYZ.
request to change the initial password.
Run Code Online (Sandbox Code Playgroud)
根据上面的文档,需要使用 Python 创建一个加密的密码并将其输入到usermod
命令中,如下所示:
usermod -p "<encrypted-password>" <username>
Run Code Online (Sandbox Code Playgroud)
有没有其他更简单的方法来做到这一点?我不想下载任何特殊的实用程序来做到这一点;它应该尽可能地概括。
编辑:即使上面链接中给出的方法对我来说似乎也不起作用:
bash-3.00# python
Python 2.4.6 (#1, Dec 13 2009, 23:43:51) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import crypt; print crypt.crypt("<password>","<salt>")
<sOMrcxm7pCPI
>>> ^D
bash-3.00# useradd -g other -p "sOMrcxm7pCPI" -G bin,sys -m -s /usr/bin/bash mukesh2
UX: useradd: ERROR: project sOMrcxm7pCPI does not exist. Choose another.
UX: useradd: sOMrcxm7pCPI name should be all lower case or numeric.
Run Code Online (Sandbox Code Playgroud)
Dom*_*ore 14
你可以用chpasswd
它来做,就像这样:
echo "username:newpassword" | chpasswd
Run Code Online (Sandbox Code Playgroud)
如果方便的话,您可以chpasswd
从 以外的程序通过管道进入echo
,但这可以解决问题。
编辑:要在 shell 脚本中生成密码然后设置它,您可以执行以下操作:
# Change username to the correct user:
USR=username
# This will generate a random, 8-character password:
PASS=`tr -dc A-Za-z0-9_ < /dev/urandom | head -c8`
# This will actually set the password:
echo "$USR:$PASS" | chpasswd
Run Code Online (Sandbox Code Playgroud)
有关 的更多信息chpasswd
,请参见http://linux.die.net/man/8/chpasswd
(生成密码的命令来自http://nixcraft.com/shell-scripting/13454-command-generate-random-password-string.html)