Linux 生成随机字符串的方法

随机字符串常用于创建随机账号或密码,Linux 可用以下方法生成随机字符串。

1.生成由大写字母组成的随机字符串:

1
2
3
$ head /dev/urandom | tr -dc A-Z | head -c 20

NRXFYZRTUEDXTVPJAYJW

2.生成由小写字母组成的随机字符串:

1
2
3
$ head /dev/urandom | tr -dc a-z | head -c 20

rizsfwebsmfowsogsqfi

3.生成由纯数字组成的随机字符串:

1
2
3
$ head /dev/urandom | tr -dc 0-9 | head -c 20

06983118429648544871

4.生成由大写字母、小写字母、数字组成的随机字符串:

1
2
3
$ head /dev/urandom | tr -dc A-Za-z0-9 | head -c 30

kFac0BEcbWS9eTZWZwn52ps53kGp6q

5.写成 Shell 脚本:

1
2
3
4
5
6
#!/bin/bash


pass=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 30)

echo $pass

References


Linux 生成随机字符串的方法
https://www.frytea.com/post/20240630125400.html
作者
Tianlun Song
发布于
2024年6月30日
更新于
2024年6月30日
许可协议