LoginSignup
1
1

More than 3 years have passed since last update.

Ansible 始める前の鍵交換

Posted at

なぜこの記事があるのか

Ansiblesshを利用するため、事前に鍵作成~公開鍵の配布が必要。

新規サーバを構築する際にどっちのサーバで鍵作成してどのファイルを配布するのか忘れがちなので記録する。

忘れがちなのはそもそも仕組みをちゃんと把握していないからだと思うけど。

手順

鍵作成

鍵を作成するのはAnsibleコマンド実行サーバ。構成管理「する」側。新しく作ったサーバではない。

$ whoami
ansi

$ ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansi/.ssh/id_rsa): ★Enterキーを押下
Created directory '/home/ansi/.ssh'.
Enter passphrase (empty for no passphrase): ★Enterキーを押下
Enter same passphrase again: ★Enterキーを押下
Your identification has been saved in /home/ansi/.ssh/id_rsa.
Your public key has been saved in /home/ansi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:XtkavVbn2AvqcEGNA+ESqibcUvLfesSgDNf/dZgPpCM ansi@ansible
The key's randomart image is:
+---[RSA 2048]----+
|       . o.      |
|      . o . o    |
| . ... . . + .   |
|..=..o  . .=.    |
| ++=. + S *.= . .|
|  +o. .E + B.+ = |
|     ...+.+.*.. o|
|      .. .o.... .|
|     ..   .o   . |
+----[SHA256]-----+

$ ls -ltr .ssh/
合計 8
-rw-r--r--. 1 ansi ansi  394  4月 28 14:36 id_rsa.pub
-rw-------. 1 ansi ansi 1675  4月 28 14:36 id_rsa

なお上記のとおりpassphraseを指定しないなら以下のように-N ""を指定するほうがEnterキーを押す回数が少ない。

$ whoami
ansi

$ ssh-keygen -t rsa -b 2048 -N ""
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ansi/.ssh/id_rsa): ★Enterキーを押下
Created directory '/home/ansi/.ssh'.
Your identification has been saved in /home/ansi/.ssh/id_rsa.
Your public key has been saved in /home/ansi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:OwY2YDgkFLQGd/o2f4GZ3urbWC1r1rN5ZvCvehD7RL4 ansi@ansible
The key's randomart image is:
+---[RSA 2048]----+
|=++ .            |
|.+.+             |
| o+ o            |
|.  + . +  . .    |
|    + B S  =     |
|   . = + ++ o    |
|      o O..* .   |
|       Bo+o.E    |
|     .=+o +O.o.  |
+----[SHA256]-----+

公開鍵配布

配布するのは「公開鍵」。そりゃそうだ、秘密のものを外に広めるものではない。同じく鍵生成したサーバで実行。

新規サーバの情報は以下のとおり。

パラメータ
IP address 192.168.56.3
rootパスワード udon123
$ ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.56.3
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/ansi/.ssh/id_rsa.pub"
The authenticity of host '192.168.56.3 (192.168.56.3)' can't be established.
RSA key fingerprint is SHA256:F+6H9OQJhmRICg/80RYzJiZCYw+QCJul6j/Bkatj/04.
RSA key fingerprint is MD5:0f:98:3b:1e:d5:a8:06:b8:55:5e:fc:8e:0f:b1:ab:86.
Are you sure you want to continue connecting (yes/no)? yes ★YESを入力

/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.3's password: ★新規サーバのrootのパスワード(udon123)を入力

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.56.3'"
and check to make sure that only the key(s) you wanted were added.

動作確認

$ ssh 192.168.56.3 -l root "hostname -I"
192.168.56.3

すばらしい。なおAnsibleにも接続確認用のコマンドがある。

$ ansible -i ~/hosts 192.168.56.3 -m ping -u root
192.168.56.3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1