/dev/random和/dev/urandom是核心随機數源裝置,用于産生随機數。産生随機數的原理是利用目前系統的熵池來計算出固定一定數量的随機比特,然後将這些比特作為位元組流傳回。熵池就是目前系統的環境噪音,熵指的是一個系統的混亂程度,系統噪音可以通過很多參數來評估,如記憶體的使用,檔案的使用量,不同類型的程序數量等等。如果目前環境噪音變化的不是很劇烈或者目前環境噪音很小,比如剛開機的時候,而目前需要大量的随機比特,這時産生的随機數的随機效果就不是很好了。
這就是為什麼會有/dev/urandom和/dev/random這兩種不同的檔案,後者在不能産生新的随機數時會阻塞程式,而前者不會(ublock),當然産生的随機數效果就不太好了,這對加密解密這樣的應用來說就不是一種很好的選擇。/dev/random會阻塞目前的程式,直到根據熵池産生新的随機位元組之後才傳回,是以使用/dev/random比使用/dev/urandom産生大量随機數的速度要慢。
如果你的系統不存在這兩個檔案,可以通過以下指令來進行建立。
<code>mknod</code> <code>-m 644 </code><code>/dev/random</code> <code>c 1 8</code>
<code>mknod</code> <code>-m 644 </code><code>/dev/urandom</code> <code>c 1 9</code>
<code>chown</code> <code>root:root </code><code>/dev/random</code> <code>/dev/urandom</code>
當一個linux系統啟動沒有太多互動式操作時,熵池可能處于一個相當可預測的狀态。這降低了熵池的實際噪聲量低于估計值。為了抵消這種影響,它有助于跨關閉和初創企業進行熵池資訊。為此,請将下列行添加到Linux系統啟動順序中運作的适當腳本中:
<code>echo</code> <code>"Initializing random number generator..."</code>
<code> </code><code>random_seed=</code><code>/var/run/random-seed</code>
<code> </code><code># Carry a random seed from start-up to start-up</code>
<code> </code><code># Load and then save the whole entropy pool</code>
<code> </code><code>if</code> <code>[ -f $random_seed ]; </code><code>then</code>
<code> </code><code>cat</code> <code>$random_seed ></code><code>/dev/urandom</code>
<code> </code><code>else</code>
<code> </code><code>touch</code> <code>$random_seed</code>
<code> </code><code>fi</code>
<code> </code><code>chmod</code> <code>600 $random_seed</code>
<code> </code><code>poolfile=</code><code>/proc/sys/kernel/random/poolsize</code>
<code> </code><code>[ -r $poolfile ] && bytes=</code><code>'cat $poolfile'</code> <code>|| bytes=512</code>
<code> </code><code>dd</code> <code>if</code><code>=</code><code>/dev/urandom</code> <code>of=$random_seed count=1 bs=$bytes</code>
另外,在Linux系統關閉期間運作的适當腳本中添加以下行:
本文轉自 SoulMio 51CTO部落格,原文連結:http://blog.51cto.com/bovin,如需轉載請自行聯系原作者