AKAI TSUKI

System development or Technical something

try ansible:setting ssh key.

SSH公開鍵認証で、SSH接続できるように設定します。

まずは、鍵を作成。

[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
<fingerprintが表示されます> root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+

<鍵の値>

+-----------------+
[root@localhost ~]#

鍵ができました。
対象のサーバ側へコピーして配置します。

[root@localhost ~]# ls -1 .ssh/
id_rsa
id_rsa.pub
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub
[root@localhost .ssh]# mv id_rsa.pub authorized_keys
[root@localhost .ssh]# scp authorized_keys root@192.168.2.220:/tmp
The authenticity of host '192.168.2.220 (192.168.2.220)' can't be established.
RSA key fingerprint is <fingerprintの値>.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.220' (RSA) to the list of known hosts.
authorized_keys                                                                       100%  408     0.4KB/s   00:00
[root@localhost .ssh]#

192.168.2.220サーバにて

[root@localhost ~]# ls -l /tmp/
合計 4
-rw-r--r-- 1 root root 408  1月 4 21:40 2015 authorized_keys
[root@localhost ~]# ls -l .ssh/
合計 12
-rw-r--r-- 1 root root  408  1月 4 21:22 2015 authorized_keys
-rw------- 1 root root 1675  1月 4 21:22 2015 id_rsa
-rw-r--r-- 1 root root  395  1月 4 21:40 2015 known_hosts
[root@localhost ~]# cp -p /tmp/authorized_keys ~/.ssh/
cp: `/root/.ssh/authorized_keys' を上書きしてもよろしいですか(yes/no)? yes
[root@localhost ~]#

もう一度ansibleを実行してみます。

[root@localhost .ssh]# ansible 192.168.2.220 -m ping
192.168.2.220 | success >> {
    "changed": false,
    "ping": "pong"
}

[root@localhost .ssh]#

無事、実行できました。

known_hostsファイルも作成されたので、
前回出ていたメッセージも表示されなくなりました。