秘密鍵の作成は、ssh-keygenコマンドで行いますが、複数のユーザの秘密鍵を作成する際に、多少手間がかかってしまう場合があります。

そのような場合は、コマンドを組み合わせて実行することで簡単に作成することができます。

はじめに、スクリプトの作成を行います。
今回は、test1、test2、test3、test4、test5のユーザの秘密鍵を作成します。

vi test.sh

su - test1 -c "ssh-keygen -N password;cat .ssh/id_rsa.pub >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys"
su - test2 -c "ssh-keygen -N password;cat .ssh/id_rsa.pub >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys"
su - test3 -c "ssh-keygen -N password;cat .ssh/id_rsa.pub >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys"
su - test4 -c "ssh-keygen -N password;cat .ssh/id_rsa.pub >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys"
su - test5 -c "ssh-keygen -N password;cat .ssh/id_rsa.pub >> .ssh/authorized_keys;chmod 600 .ssh/authorized_keys"

上記スクリプトを実行すると、下記のようになり、Enter file in which to save the key の後にEnterを押下します。

[root@cimacoppi 0104]# ./test.sh
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test1/.ssh/id_rsa):
Created directory '/home/test1/.ssh'.
Your identification has been saved in /home/test1/.ssh/id_rsa.
Your public key has been saved in /home/test1/.ssh/id_rsa.pub.
The key fingerprint is:
6c:85:70:c8:7a:0e:a1:81:87:d1:c1:f5:57:47:b9:bf
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test2/.ssh/id_rsa):
Created directory '/home/test2/.ssh'.
Your identification has been saved in /home/test2/.ssh/id_rsa.
Your public key has been saved in /home/test2/.ssh/id_rsa.pub.
The key fingerprint is:
41:80:f9:60:73:00:fb:a2:72:6a:9b:c8:b4:13:19:5e
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test3/.ssh/id_rsa):
Created directory '/home/test3/.ssh'.
Your identification has been saved in /home/test3/.ssh/id_rsa.
Your public key has been saved in /home/test3/.ssh/id_rsa.pub.
The key fingerprint is:
e6:32:b4:8a:ae:fa:6f:ff:6c:5f:80:19:3e:7b:e0:b6
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test4/.ssh/id_rsa):
Created directory '/home/test4/.ssh'.
Your identification has been saved in /home/test4/.ssh/id_rsa.
Your public key has been saved in /home/test4/.ssh/id_rsa.pub.
The key fingerprint is:
11:c6:10:df:f7:51:73:39:62:99:ec:af:25:25:ae:dd
Generating public/private rsa key pair.
Enter file in which to save the key (/home/test5/.ssh/id_rsa):
Created directory '/home/test5/.ssh'.
Your identification has been saved in /home/test5/.ssh/id_rsa.
Your public key has been saved in /home/test5/.ssh/id_rsa.pub.
The key fingerprint is:
2f:1e:79:88:c8:cf:f6:75:c7:c0:98:ce:96:5f:4c:c1

後は、id_rsaファイルのダウンロードとなります。

これで、かなりの時間短縮が可能になりそうです。

こちらの記事はなかの人(be_hase)監修のもと掲載しています。
元記事は、こちら