Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 下產生 crypt 加密密碼的六種方法:mkpasswd、htpasswd、openssl、Perl、Python、MySQL

編輯:Linux 教學

Kickstart、useradd 等自動化部署常需預先產生 crypt 格式密碼雜湊

1. mkpasswd (最標準、互動式)

mkpasswd
# 提示輸入密碼,輸出 crypt 雜湊字串

套件:whois (Debian/Ubuntu) 或 expect (RHEL/CentOS)。

2. htpasswd (Apache 工具,輸出 user:hash)

htpasswd -nd username
# 輸出:username:hash
# 取 hash 部分即可

需安裝 apache2-utils (Debian/Ubuntu) 或 httpd-tools (RHEL/CentOS)。

3. openssl (廣泛內建)

openssl passwd -crypt 'MyPassword'
# 直接輸出 crypt 雜湊

將 MyPassword 替換為實際密碼。

4. Perl (需指定 Salt)

perl -e "print crypt('password', 'sa');"
# 'sa' 為 2 字元 Salt,不同 Salt 產生不同雜湊

5. Python (需匯入 crypt 模組)

python -c 'import crypt; print(crypt.crypt("password", "Fx"))'
# 'Fx' 為 Salt

6. Ruby (同樣需 Salt)

ruby -e 'print "password".crypt("JU"); print "\n"'

7. PHP (CLI 模式)

php -r "print(crypt('password', 'JU') . \"\n\");"

8. MySQL ENCRYPT() 函數

echo "SELECT ENCRYPT('password');" | mysql
# 輸出 crypt 雜湊

關鍵提醒:Salt (鹽) 必不可少

  • 所有上述語言版本必須提供 Salt (通常 2 字元),否則預設產生 MD5 雜湊 (以 $1$ 開頭) 而非傳統 DES crypt (無前綴)。
  • Salt 字元範圍:./0-9/A-Z/a-z。
  • 命令列直接帶入密碼會殘留於 Shell 歷史與 ps 程序表,**生產環境請改用腳本讀取標準輸入或環境變數**。

安全建議

  • 現代系統建議使用 SHA-512 crypt ($6$) 或 Yescrypt/Argon2,而非傳統 DES crypt (僅前 8 字元有效、無抗暴力破解能力)。
  • openssl passwd -6 'password' 產生 SHA-512 雜湊。
  • mkpasswd -m sha-512 同理。
Copyright © Windows教程網 All Rights Reserved