AWSコマンドでAWS上の仮想マシンの名称とPublicIP、PrivateIPの一覧を取得させる
転職先ではAWSとAzureを…
-->
先日、ssh接続する際に.ssh/configで指定していた鍵ではない秘密鍵を使って接続しようとしてたところ、公開鍵を登録していない秘密鍵を指定しても接続されてしまうという事があった。
なんだこりゃと思って-vオプションを付与して見たところ、以下のような動きをしていた。
blacknon@BS-PUB-DEVELOP:~$ cat .ssh/config Host 192.168.0.119 HostName 192.168.0.119 User root Port 22 IdentityFile /home/blacknon/.ssh/id_rsa blacknon@BS-PUB-DEVELOP:~$ blacknon@BS-PUB-DEVELOP:~$ ssh -v -i testkey root@192.168.0.119 OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g 1 Mar 2016 debug1: Reading configuration data /home/blacknon/.ssh/config debug1: /home/blacknon/.ssh/config line 1: Applying options for 192.168.0.119 debug1: Reading configuration data /etc/ssh/ssh_config debug1: /etc/ssh/ssh_config line 19: Applying options for * debug1: Connecting to 192.168.0.119 [192.168.0.119] port 22. debug1: Connection established. debug1: identity file testkey type 1 debug1: key_load_public: No such file or directory debug1: identity file testkey-cert type -1 debug1: identity file /home/blacknon/.ssh/id_rsa type 1 debug1: key_load_public: No such file or directory debug1: identity file /home/blacknon/.ssh/id_rsa-cert type -1 debug1: Enabling compatibility mode for protocol 2.0 debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.1 debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1 debug1: match: OpenSSH_6.6.1 pat OpenSSH_6.6.1* compat 0x04000000 debug1: Authenticating to 192.168.0.119:22 as 'root' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256@libssh.org debug1: kex: host key algorithm: ecdsa-sha2-nistp256 debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none debug1: expecting SSH2_MSG_KEX_ECDH_REPLY debug1: Server host key: ecdsa-sha2-nistp256 SHA256:3pbP7iBBSKrGlSX/dES1WdvfOsRX7Zjyb9G8tXeeHi4 debug1: Host '192.168.0.119' is known and matches the ECDSA host key. debug1: Found key in /home/blacknon/.ssh/known_hosts:2 debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug1: rekey after 134217728 blocks debug1: SSH2_MSG_NEWKEYS received debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Next authentication method: gssapi-keyex debug1: No valid Key exchange context debug1: Next authentication method: gssapi-with-mic debug1: Unspecified GSS failure. Minor code may provide more information No Kerberos credentials available debug1: Unspecified GSS failure. Minor code may provide more information No Kerberos credentials available debug1: Unspecified GSS failure. Minor code may provide more information debug1: Unspecified GSS failure. Minor code may provide more information No Kerberos credentials available debug1: Next authentication method: publickey debug1: Offering RSA public key: testkey debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password debug1: Offering RSA public key: /home/blacknon/.ssh/id_rsa debug1: Server accepts key: pkalg ssh-rsa blen 279 debug1: Authentication succeeded (publickey). Authenticated to 192.168.0.119 ([192.168.0.119]:22). debug1: channel 0: new [client-session] debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: network debug1: Sending environment. debug1: Sending env LANG = ja_JP.UTF-8 Last login: Wed Jun 21 07:57:32 2017 from 192.168.0.171 [root@BS-PUB-CENT7-02 ~]#
どうやら、オプションで指定した秘密鍵で接続できなかった場合、そのまま.ssh/configに記載されている秘密鍵で接続されてしまうようだ。
(オプションで指定した鍵で接続できる場合は、そのままその鍵で接続される)
新しい鍵を登録したりする際のテスト時などにちょっと紛らわしい…。
違う鍵を使って接続する場合は、対象のホストを一時的にコメントアウトするなり、.ssh/configを一時的にリネームするといった対応をすると良さそう。