方法一:通过系统环境变量($RANDOM)
[root@localhost script]# echo $RANDOM
15491
[root@localhost script]# echo $RANDOM
31703
方法二:通过openssl产生随机数
[root@localhost script]# openssl rand -base64 8
Ii3RMp2rWW4=
[root@localhost script]# openssl rand -base64 10
5vAx7WpCndOW+g==
方法三:通过时间获得随机数
[root@localhost script]# date +%s%N
1471315388251547446
[root@localhost script]# date +%s%N
1471315393753792762
方法四:通过urandom设备
[root@localhost script]# head /dev/urandom |cksum
1190288578 3139
[root@localhost script]# head /dev/urandom |cksum
2086682940 1895
方法五:UUID方式
[root@localhost script]# cat /proc/sys/kernel/random/uuid
caa88f84-9ffc-4815-b6a3-311b48ec46a9
[root@localhost script]# cat /proc/sys/kernel/random/uuid
2fe09db8-883b-45f3-ba43-78daeba053e9
方法六:expect 方式
[root@localhost script]# yum install -y expect
[root@localhost script]# mkpasswd -l 8
b8qHNj3{
[root@localhost script]# mkpasswd -l 8
GLo\mv14
若以上随机数长短不一,如何统一格式,使用md5sum命令
[root@localhost script]# echo $RANDOM |md5sum|cut -c 1-8
752a2fb4
[root@localhost script]# openssl rand -base64 10 |md5sum|cut -c 1-8
63abdb08
[root@localhost script]# date +%s%N |md5sum|cut -c 1-8
821eda55
[root@localhost script]# head /dev/urandom |cksum |md5sum|cut -c 1-8
37abe446
[root@localhost script]# cat /proc/sys/kernel/random/uuid |md5sum|cut -c 1-8
24147761
[root@localhost script]# mkpasswd -l 8 |md5sum|cut -c 1-8
781bdda1
转载于:https://blog.51cto.com/tenderness/1839136