ランダムパスワードを自動生成するコマンド

mkpasswd
head -c 8 /dev/random

パスワードを作るときに、キーボードをガチャガチャ押して「rdftgyふじこlp@;」とかやってませんか?
ちゃんとランダムでセキュリティ上安全なパスワードを作りましょう。

mkpasswdを使う

mkpasswd

このコマンドで、ランダムな文字列が生成されます。
もしこのコマンドが使えなかったら、expectパッケージがインストールされていません。
yumでインストールしておきましょう。

# yum install expect

さてこのパスワード、デフォルトでは全9文字(小文字2、大文字2、数字2、特殊文字1)です。
文字数や使用文字の種類は、オプションで細かくカスタマイズすることができます。

例えば、「数字2+アルファベット6で全8文字のパスワード」を作りたい場合は、

# mkpasswd -l 8 -c 6 -d 2 -C 0 -s 0
riwp36jh

となります。
その他オプション一覧

  • -l 数字 … パスワードの長さを指定(デフォルトは9)
  • -d 数字 … パスワードに含める数字の数を指定(デフォルトは2)
  • -c 数字 … パスワードに含めるアルファベット(小文字)の数を指定(デフォルトは2)
  • -C 数字 … パスワードに含めるアルファベット(大文字)の数を指定(デフォルトは2)
  • -s 数字 … パスワードに含める特殊文字(記号など)の数を指定(デフォルトは1)
  • -2 … パスワードが、右手と左手で交互に入力される文字配列となる。(入力時に手元を見られて盗まれる可能性を低くするため)

最後の-2オプションは「へぇ~」という感じですね。

# mkpasswd -l 6 -c 6 -d 0 -C 0 -s 0 -2
yapsng

/dev/random を使う

Linuxが、様々な処理で乱数を必要とする時のために、/dev/random という乱数発生器が存在します。
ここから、headコマンドで8バイトほど取り出して、ランダムな8文字を作り出すことができます。

# head -c 6 /dev/random | uuencode -m –
begin-base64 644 –
FgGWD/8x
====

1行目と2行目はヘッダとフッタなので無視して構いませんが、気にる人や自動処理するときなどのために、sedに渡して削除して出力することもできます。

# head -c 6 /dev/random | uuencode -m – | sed -n ‘2s/=*$//;2p’
+VTO0nVo
-bash: uuencode: command not found

「uuencodeがない」と表示される場合は、sharutilsパッケージをインストールします。

[root@myserver ]# yum install uuencode                                                                                                                                                
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: www2.atomicorp.com
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
atomic                                                                                                                                                                | 1.9 kB     00:00     
base                                                                                                                                                                  | 1.1 kB     00:00     
extras                                                                                                                                                                | 2.1 kB     00:00     
updates                                                                                                                                                               | 1.9 kB     00:00     
Setting up Install Process
No package uuencode available.
Nothing to do
[root@rad-xen-vweb10 ~]# yum install sharutils
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * atomic: www2.atomicorp.com
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Setting up Install Process
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
The program yum-complete-transaction is found in the yum-utils package.
--> Running transaction check
---> Package sharutils.x86_64 0:4.6.1-2 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                         Arch                                         Version                                       Repository                                  Size
=============================================================================================================================================================================================
Installing:
 sharutils                                       x86_64                                       4.6.1-2                                       base                                       203 k

Transaction Summary
=============================================================================================================================================================================================
Install       1 Package(s)
Upgrade       0 Package(s)

Total download size: 203 k
Is this ok [y/N]: y
Downloading Packages:
sharutils-4.6.1-2.x86_64.rpm                                                                                                                                          | 203 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing     : sharutils                                                                                                                                                             1/1 

Installed:
  sharutils.x86_64 0:4.6.1-2                                                                                                                                                                 

Complete!

1 thought on “ランダムパスワードを自動生成するコマンド

  1. AlexisBrums

    Now compare that proverb to this famous aphorism.
    Another success aphorism comes from Chris Grosser.
    If you can do something, then you need to do it for the good of others.
    Like George Washington, Sandys believed that telling the truth is always the way to go.
    The complete quote was, A Jack of all trades and master of none, but oftentimes better than a master of one.
    search bar with “what use aphorism.” written
    And get this.
    Practice what you preach.
    Early to bed and early to rise makes a man healthy, wealthy, and wise.
    Because let’s face it, perseverance is the key to success in life.
    Thomas Jefferson also mirrored this general idea when he said, I find that the harder I work, the more luck I seem to have.
    This aphorism is short and sweet, but it teaches us a valuable truth.
    Sometimes, though.
    Let’s talk about that.
    Give it a try!
    Are you in.

    返信

匿名 へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です