Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 自動化創建帶密碼用戶:openssl 生成密文 + useradd -p 實現無交互批量建賬

編輯:Linux 系統常見問題解答

實驗室賬號管理系統需自動化創建用戶並發送密碼郵件。本文演示利用 openssl passwd 生成 crypt 密文,配合 useradd -p 實現無交互創建帶密碼用戶,結合 SSH 免密分發實現多服務器同步。

1. 生成密文密碼

openssl passwd -crypt "明文密碼"
# 輸出示例:S0dAmthh3vjPQ

-crypt 產生傳統 DES crypt 格式,兼容 useradd -p。

2. 一條命令創建用戶

useradd -p "S0dAmthh3vjPQ" test

可額外加參數:-g 組 -d 家目錄 -s shell -m 創建家目錄。

3. 多服務器同步方案

  1. 管理節點配置 SSH 免密登錄至所有被管服務器。
  2. 遠程執行:ssh user@web1 "useradd -p '密文' username"
  3. 或使用 Ansible user 模塊批量推送:
    - name: Create user
      user:
        name: "{{ username }}"
        password: "{{ crypted_password }}"
        groups: wheel
        append: yes

安全提示

  • 密文雖不可逆,但 DES crypt 強度極低,現代 GPU 可暴力破解。
  • 生產環境建議:
    • 使用 openssl passwd -6 生成 SHA-512 密文(需 useradd -p 支持 $6$ 格式)
    • 或創建用戶後用 chpasswd / passwd --stdin 明文管道設置(腳本中注意權限)
    • 結合 pwgen 生成高強度隨機密碼,首次登錄強制修改。

掌握密文生成與自動化創建,是構建賬號管理平台的基石。

Copyright © Windows教程網 All Rights Reserved