用 openssl rand 生成随机密码
生成密码学安全的随机字节并以 base64 或十六进制输出——每台 Linux 都内置的密码生成器。
执行 openssl rand -base64 18 可得到 24 字符密码;执行 openssl rand -hex 32 可得到适合作为 API 密钥的 64 字符十六进制串。
01命令
命令
$ openssl rand -base64 18
openssl rand -hex 3202作用说明
openssl rand 从系统 CSPRNG 取随机字节;-base64 和 -hex 只是改变编码方式,实际熵量取决于字节数:18 字节经 base64 编码后得到 144 位熵、24 个可打印字符。-hex 32 产生 256 位值,是加密密钥和 secret 的标准长度。若密码策略禁止特殊符号,可通过 tr -d '/+=' 过滤。
03风险
!注意: base64 输出可能包含 / 和 + 等字符,在 URL 或某些 shell 中会出问题——先确认使用方的字符规则,或直接用 -hex 保证只有字母数字。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- openssl(1) — Linux man page依据: openssl man page: 'openssl rand - generate pseudo-random bytes' with -base64 and -hex output encoding options.
最后验证: 2026-09-17 · 分类: 安全与隐私