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 生成隨機密碼與修改密碼全攻略:10 種實用指令大全

編輯:Linux 教學

本文匯總 Linux 下生成高強度隨機密碼的 10 種實用指令,並說明修改使用者密碼的標準方法,適用於腳本自動化、帳號初始化等場景。

修改使用者密碼(標準方法)

passwd root          # 修改 root 密碼,依提示輸入兩次
passwd username      # 修改指定使用者密碼

10 種生成隨機密碼指令(長度可調)

#指令特點
1date +%s | sha256sum | base64 | head -c 32; echoSHA256+Base64,32 字元
2< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32}; echo/dev/urandom 過濾符號,預設 32 字元
3openssl rand -base64 32OpenSSL 內建亂數,最簡潔
4tr -cd '[:alnum:]' < /dev/urandom | fold -w30 | head -n1純英數,30 字元
5strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d 'n'; echostrings 解析裝置亂數
6< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6超短 6 字元(測試用)
7dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 -w 0 | rev | cut -b 2- | revdd 讀取亂數轉 Base64
8< /dev/urandom tr -dc '12345!@#$%qwertQWERTasdfgASDFGzxcvbZXCVB' | head -c8; echo左手鍵盤區字元(單手輸入)
9randpw(){ < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16}; echo; }自訂函數,預設 16 字元
10date | md5sum最短指令,MD5 雜湊值

額外工具:mkpasswd / passwdgen

# 需先安裝
yum -y install expect      # 提供 mkpasswd
yum -y install passwdgen   # 提供 passwdgen

mkpasswd -l 16             # 生成 16 字元密碼
passwdgen                  # 互動式生成

建議與注意

  • 生產環境建議使用 openssl rand 或 /dev/urandom 方案,熵值充足且指令標準。
  • 避免使用 date | md5sum 等低熵來源作為高安全性密碼。
  • 密碼長度建議 ≥ 16 字元,包含大小寫、數字、特殊符號。
Copyright © Windows教程網 All Rights Reserved