なにかの技術メモ置き場

なにかの技術メモ置き場

@インフラエンジニア

CentOS8でパスワード無しのssh公開鍵認証ができない

事象

CentOS8でパスワード無しのssh公開鍵認証ができない。
補足すると、パスワード入力を求められてしまう。

先に結論

鍵の形式がDSAだと不可。RSA、ECDSAは可。

環境

構成

用途 ホスト名 OS SSH 備考
SSHクライアント sshclient CentOS 8.1.1911 OpenSSH_8.0p1 -
SSHサーバ sshserver CentOS 8.1.1911 OpenSSH_8.0p1 -

詳細

設定

SSHクライアント側で鍵を作成する。
RSA形式は古いので、新しいDSA形式で作成する。※この考えこそが古かった・・・

[root@sshclient ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): # 空Enter
Enter same passphrase again: # 空Enter
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:VEfMUB6mYCEZPIefCqUqTzBjRtFd/Yv2UeTWuZGlKcM root@sshclient.localdomain
The key's randomart image is:
+---[DSA 1024]----+
| .o ..+==.o**    |
| . . .*o.+ =oo  .|
|.    o +..o.+ .o+|
|+o  o  .o  .E+o= |
|o+ . . .S . +o  o|
|. o   .  o o   . |
| +      . . .    |
|  .        .     |
|                 |
+----[SHA256]-----+

秘密鍵(id_dsa)と公開鍵(id_dsa.pub)が生成された。

[root@sshclient ~]# ls -l /root/.ssh/
合計 12
-rw------- 1 root root 1401  4月 20 14:59 id_dsa
-rw-r--r-- 1 root root  622  4月 20 14:59 id_dsa.pub
-rw-r--r-- 1 root root  548  4月 20 14:07 known_hosts

公開鍵をSSHサーバ側に転送する。

[root@sshclient ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@sshserver
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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@sshserver's password: # パスワードを入力

Number of key(s) added: 1

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

準備はたったこれだけ。
昔はscpしてcatでauthorized_keysに追記してchownして・・・とやっていたが、1コマンドで済むことを今さら知ってしまった。

動作確認

SSHサーバにssh接続する。

[root@sshclient ~]# ssh root@sshserver
root@sshserver's password: # パスワード入力を求められてしまう
Last login: Mon Apr 20 14:57:13 2020 from 172.16.0.121
[root@sshserver ~]# # 認証はできる

ノンパスで設定したはずなのに、パスワード入力を求められてしまった。

[root@sshserver ~]# ls -l /root/.ssh/
合計 8
-rw------- 1 root root 622  4月 20 15:01 authorized_keys
-rw-r--r-- 1 root root 180  4月 20 13:30 known_hosts

パーミッションも600で問題ない。その他トラブルシュートしたが原因を見つけられず。

対処

切り分けとしてECDSA形式の鍵を使用したところ、すんなり成功した。
なお、RSA形式でも成功した。DSA形式のみが今回の事象に該当する。

設定

SSHクライアント側で鍵を作成する。

[root@sshclient ~]# ssh-keygen -t ecdsa
Generating public/private ecdsa key pair.
Enter file in which to save the key (/root/.ssh/id_ecdsa):
Enter passphrase (empty for no passphrase): # 空Enter
Enter same passphrase again: # 空Enter
Your identification has been saved in /root/.ssh/id_ecdsa.
Your public key has been saved in /root/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:SsPm0OU9aw2Sth7a+xZW0RVKs82HvZMuYUApecCkzvI root@sshclient.localdomain
The key's randomart image is:
+---[ECDSA 256]---+
|        ooo..+ oo|
|        .+.oo Bo |
|       .. o. +.oo|
|     ooo o  o   +|
|    ..*oS +. o + |
|     =o+ oo=. o .|
|      oEo.o... . |
|       + o.   .  |
|      . ++.      |
+----[SHA256]-----+

公開鍵をSSHサーバ側に転送する。

[root@sshclient ~]# ssh-copy-id -i /root/.ssh/id_ecdsa.pub sshserver
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_ecdsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/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@sshserver's password: # パスワードを入力

Number of key(s) added: 1

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

動作確認

SSHサーバにssh接続する。

[root@sshclient ~]# ssh root@sshserver
Last failed login: Mon Apr 20 15:03:16 JST 2020 from 172.16.0.121 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Mon Apr 20 15:01:46 2020 from 172.16.0.121
[root@sshserver ~]# # パスワード入力を求められずに接続できた