【转】Linux系统产生随机数的6种方法
1、通过系统环境变量($RANDOM)产生随机数
[root@localhost ~]# echo $RANDOM
3849
[root@localhost ~]# echo $RANDOM
14931
[root@localhost ~]# echo $RANDOM
12488
[root@localhost ~]# echo $RANDOM |md5sum |cut -c 1-8 #获取8位随机字符
2e45f930
[root@localhost ~]# echo $RANDOM |cksum |cut -c 1-8 #获取8位随机数字
44020023
2、通过openssl产生随机数
openssl rand 用于产生指定长度个bytes的随机字符
-base64 / -hex 对随机字符串进行base64编码或用hex格式显示
[root@localhost ~]# openssl rand -base64 10
yNop46cVrQxNIQ==
[root@localhost ~]# openssl rand -base64 8
pB492c604MI=
[root@localhost ~]# openssl rand -base64 5
gAqEI9c=
[root@localhost ~]# openssl rand -base64 6 #获取8位随机字符
Ynp6VIpK
[root@localhost ~]# openssl rand -base64 9 #获取12位随机字符
fiZ8sfsISLNt
[root@localhost ~]# openssl rand -base64 8 |cksum |cut -c 1-8 #获取8位随机数字
79268014
3、通过时间获得随机数(date)
[root@localhost ~]# date +%s%N
1523515467269826678
[root@localhost ~]# date +%s%N
1523515470209375226
[root@localhost ~]# date +%N
967517148
[root@localhost ~]# date +%s
1523515480
[root@localhost ~]# date +%N |cut -c 1-8 #获取8位随机数字
48604877
4、使用/dev/urandom产生随机数
[root@localhost ~]# head /dev/urandom | cksum
4079719468 1897
[root@localhost ~]# head /dev/urandom | cksum
335118151 3245
[root@localhost ~]# head /dev/urandom | cksum
2907270140 3053
5、使用计算机生成的uuid码产生随机字符
[root@localhost ~]# cat /proc/sys/kernel/random/uuid
9db39860-5e2f-46c0-8198-d40d551a1402
[root@localhost ~]# cat /proc/sys/kernel/random/uuid
d158d445-abdc-4909-81da-7bafae83cce1
[root@localhost ~]# cat /proc/sys/kernel/random/uuid |cut -c 1-8 #获取8位随机字符
1c5ad58a
[root@localhost ~]# cat /proc/sys/kernel/random/uuid |cut -c 1-8 #获取8位随机字符
d332cf7f
6、安装软件expect,然后使用命令mkpasswd生成随机字符
[root@localhost ~]# yum -y install expect
[root@localhost ~]# mkpasswd -l 8
6cvHu1P_
[root@localhost ~]# mkpasswd -l 8
g7jR3!Lt
[root@localhost ~]# mkpasswd -l 8
Y9wCwk4-
[root@localhost ~]# mkpasswd
h8{o4wVWf
[root@localhost ~]# mkpasswd
9bnI&zD4u
[root@localhost ~]# mkpasswd|md5sum
d9fabd642a8d4e260c95604bdd7d9cea -
[root@localhost ~]# mkpasswd|md5sum
c5ddd81803a9308bcf31999c2441c5d9 -
原文:https://www.icode9.com/content-3-136220.html