在Linux系统中生成随机密码有多种方法,以下是常用的10种方案,涵盖不同工具和场景需求:
openssl
openssl rand -base64 12
pwgen
工具pwgen -s 16 1
sudo apt install pwgen
(Debian/Ubuntu)或 sudo yum install pwgen
(RHEL/CentOS)。-s
表示安全密码,-y
包含符号)。/dev/urandom
tr -dc 'A-Za-z0-9!@#$%^&*()' < /dev/urandom | head -c 16; echo
-dc
删除非指定字符,head -c
控制长度。gpg
gpg --gen-random --armor 1 16
date
+ md5sum
date +%s | sha256sum | base64 | head -c 16; echo
mkpasswd
(whois包)mkpasswd -m sha-512 -S $(openssl rand -base64 12) -s 16
sudo apt install whois
。apg
工具apg -a 1 -m 16 -n 1
sudo apt install apg
。-a 0
为随机模式)。python3 -c "import secrets; print(secrets.token_urlsafe(12))"
secrets
模块(比random
更安全)。perl -e 'print join("", (0..9, A..Z, a..z, qw(!@#$%^&*))[rand 72] for 1..16)'
xkcdpass
(基于短语的密码)xkcdpass --numwords=4 --delimiter='-' --case=alternating
pip install xkcdpass
。Correct-Horse-Battery-Staple
。pwgen -1
生成单次密码。pass
或keepassxc
)。根据系统环境和需求选择合适的方法,优先使用openssl
、pwgen
或/dev/urandom
等系统内置工具。