nabeo がピーしているブログ (仮)

どーも、nabeop です

OpenSSH で複数の公開鍵を使い分ける

人間、生きていると複数の公開鍵を優先順位をつけて使い分けたくなる時があります。例えば、OpenSSH 8.8/8.8p1 で ssh-rsa 鍵による署名が無効になるので、この機会に ssh-rsa 鍵を捨てたいけど、本当に全部のサーバを置き換えたか自信がないので、古い鍵にフォールバックできるようにしておきたい、などです。

ssh_config (5) では以下のように IdentityFile を複数定義することができると書いてあります。

 IdentityFile
         Specifies a file from which the user's DSA, ECDSA, Ed25519 or RSA authentication identity is read.  The default is
         ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa.  Additionally, any identities represented by the
         authentication agent will be used for authentication unless IdentitiesOnly is set.  If no certificates have been explic-
         itly specified by CertificateFile, ssh(1) will try to load certificate information from the filename obtained by appending
         -cert.pub to the path of a specified IdentityFile.

         Arguments to IdentityFile may use the tilde syntax to refer to a user's home directory or the tokens described in the
         TOKENS section.

         It is possible to have multiple identity files specified in configuration files; all these identities will be tried in
         sequence.  Multiple IdentityFile directives will add to the list of identities tried (this behaviour differs from that of
         other configuration directives).

         IdentityFile may be used in conjunction with IdentitiesOnly to select which identities in an agent are offered during
         authentication.  IdentityFile may also be used in conjunction with CertificateFile in order to provide any certificate
         also needed for authentication with the identity.

「all these identities will be tried in sequence.」なので、~/.ssh/config で以下のように書いたら ~/.ssh/id_ed25519~/.ssh/id_rsa/ の順番で鍵を使ってくれそうです。

IdentityFile ~/.ssh/id_ed25519
IdentityFile ~/.ssh/id_rsa

これは真でもあり偽でもあります。

具体的には ssh-agent に ~/.ssh/id_rsa が登録されている状態で、設定ファイルを上記のように書き換えた場合、既に ssh-agent に登録されていた ~/.ssh/id_rsa が使用する鍵ペアの公開鍵として使われ、 次に ~/.ssh/id_ed25519 の鍵ペアが使われていました。

このため、ssh-add -D して ssh-agent に記憶喪失になってもらうか、ssh-agent をリスタートさせる必要がありました。

ssh-ed25519 公開鍵を受け付けない ssh サーバを準備して意図した優先順位で鍵ペアが使われているかを確認したら、ssh -v の出力から意図通りに ssh-rsa にフォールバックしてくれることを確認しました。

フォールバックに成功した例:

# id_ed25519 -> id_rsa の順番で使いたいと表明している
debug1: Will attempt key: /Users/nabeop/.ssh/id_ed25519 ED25519 SHA256:PMVZkeWRos6nqGxKtIJXfAlJK6H2z1GOjxnWPTm9/T8 explicit agent
debug1: Will attempt key: /Users/nabeop/.ssh/id_rsa RSA SHA256:wMZ0uFG/ig9QLVRaamjzL6JKCDmB6SuqM4V5lpaqkqU explicit agent
# id_rsa にフォールバックした
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/nabeop/.ssh/id_ed25519 ED25519 SHA256:PMVZkeWRos6nqGxKtIJXfAlJK6H2z1GOjxnWPTm9/T8 explicit agent
debug1: Authentications that can continue: publickey
debug1: Offering public key: /Users/nabeop/.ssh/id_rsa RSA SHA256:wMZ0uFG/ig9QLVRaamjzL6JKCDmB6SuqM4V5lpaqkqU explicit agent
debug1: Server accepts key: /Users/nabeop/.ssh/id_rsa RSA SHA256:wMZ0uFG/ig9QLVRaamjzL6JKCDmB6SuqM4V5lpaqkqU explicit agent
Authenticated to 127.0.0.1 using "publickey".

ssh-ed25519 が使えた例:

# id_ed25519 -> id_rsa の順番で使いたいと表明している
debug1: Will attempt key: /Users/nabeop/.ssh/id_ed25519 ED25519 SHA256:PMVZkeWRos6nqGxKtIJXfAlJK6H2z1GOjxnWPTm9/T8 explicit agent
debug1: Will attempt key: /Users/nabeop/.ssh/id_rsa RSA SHA256:wMZ0uFG/ig9QLVRaamjzL6JKCDmB6SuqM4V5lpaqkqU explicit
# id_ed25519 が使われた
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/nabeop/.ssh/id_ed25519 ED25519 SHA256:PMVZkeWRos6nqGxKtIJXfAlJK6H2z1GOjxnWPTm9/T8 explicit agent
debug1: Server accepts key: /Users/nabeop/.ssh/id_ed25519 ED25519 SHA256:PMVZkeWRos6nqGxKtIJXfAlJK6H2z1GOjxnWPTm9/T8 explicit agent
Authenticated to 127.0.0.1 using "publickey".