
我想useradd
在 CLI 中使用命令時為新用戶建立加密的登入密碼。我知道使用選項-p
將允許我建立密碼,但使用此選項不會加密密碼。我還知道我可以passwd [username]
在通過創建新用戶後單獨使用該命令創建加密密碼useradd
,但就像我說的,我想知道如何透過該useradd
命令創建加密密碼。
答案1
你可以使用 Perl:
perl -e "print crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")"
或使用 Pythoncrypt
模組:
python -c "import crypt; print crypt.crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")"
foo
:要加密的密碼$6
:加密類型,在本例中為 SHA-512$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)
:加密鹽,在本例中是隨機的 32 個字元的字串。
和這個結合useradd
:
useradd [...] -p"$(perl -e "print crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")")" [...]
或者:
useradd [...] -p"$(python -c "import crypt; print crypt.crypt(\"foo\", \"\$6\$$(</dev/urandom tr -dc 'a-zA-Z0-9' | head -c 32)\$\")")" [...]
答案2
passwd --expire
您可以透過建立使用者 ID,然後使用它來跳過整個管理員建立密碼的使用者管理麻煩。從man passwd
:
-e, --expire
Immediately expire an account's password. This in effect can force
a user to change his/her password at the user's next login.
答案3
由於 passwd 在 Ubuntu 中不支援 --stdin 你可以嘗試這個:
perl -e "print crypt('password','sa');"
看https://administratosphere.wordpress.com/2011/06/16/generate-passwords-using-crypt3/
答案4
以下內容對我使用 Ubuntu 18.04 LTS 有效:
echo 'your_password' > /tmp/pw.txt
pw="$(makepasswd --crypt-md5 --clearfrom=/tmp/pw.txt)"
sudo useradd -p "${pw}" your_username
rm -f /tmp/pw.txt
這可能首先需要makepasswd
使用安裝(在 Ubuntu 或 Debian 上;谷歌搜尋其他發行版):
sudo apt install makepasswd