SSHの設定
実例をいれつつ、というより流用できるように、なので例が長くなります。
なにか発生するたびに実例を追加していく予定。
TL;DR
man ssh_config
を読みましょう。
あと、ssh_config
は重複があると最初の項目が優先1です。
落とし穴1
sshでgithubで複数アカウントをやるとして、
# Github
Host github.com
# Alt/Use for Over HTTPS at 443
# Hostname ssh.github.com
# Port 443
Hostname github.com
# gist(use ssh)
Host gist.github.com
# Over HTTPS
# Hostname ssh.github.com
# Port 443
Hostname gist.github.com
# altssh server direct setting
Host ssh.github.com
Hostname ssh.github.com
Port 443
# 2nd or later account
# Example
Host example.github.com
Hostname github.com
# key define : first match check
IdentityFile ~/.ssh/id_rsa.github.example
# github common setting
Match originalhost github.com,gist.github.com,ssh.github.com
IdentityFile ~/.ssh/id_ecdsa.github
IdentityFile ~/.ssh/id_ed25519.github
# fallback
IdentityFile ~/.ssh/id_rsa.github
# github global setting
Host *github.com
User git
TCPKeepAlive yes
IdentitiesOnly yes
Compression yes
# Note:key
# https://api.github.com/users/tsuyoshicho/keys
ここでハマったのは、前はMatch
ではなく*github.com
へメインアカウントの鍵ファイルを定義していたことです。
こうすると、2ndアカウントの鍵の定義が
$ ssh -G git@example.github.com
user git
hostname github.com
<中略>
identityfile ~/.ssh/id_rsa.github.example
identityfile ~/.ssh/id_ecdsa.github
identityfile ~/.ssh/id_ed25519.github
identityfile ~/.ssh/id_rsa.github
<後略>
になるんですが、これが穴です。
期待としては、ここで選んだ鍵で(先頭から)接続するはずで、それは問題ないですが、ここにssh-agent
がからむと鍵の選択が適切に実施される保障がないようです。(メインアカウントRSAで繋がった、たぶんssh-add
した順にssh-agent
が選択してしまうっぽい)
なので、メインアカウントでの接続の鍵はMatch
節にまとめるようにしました。あとそのときはoriginalhost
である必要もあるので注意。
落とし穴2
# operation server
Host op.example.com
User tsuyoshicho
TCPKeepAlive yes
IdentitiesOnly no
PubkeyAuthentication no
ProxyCommand none
# server instance
Host dev1.example.com
Hostname [2001:db8::1]
User tsuyoshicho
TCPKeepAlive yes
# key define : first match check
IdentityFile ~/.ssh/id_rsa.example
IdentitiesOnly yes
ProxyCommand none
operation serverは公開鍵認証はないサーバなのですが、ここで鍵が指定されてないだけだと、ssh-agent
とやり取りしてToo many authentication failures
になってしまう。
解決方法としては公開鍵認証だけの指定を止める(IdentitiesOnly
)だけではなく、公開鍵認証を止める(PubkeyAuthentication
)こと。
別の方法としてPreferredAuthentications
で公開鍵認証よりパスワード認証を先にする(もしくは公開鍵認証を外す)、というのもあるが、ちょっとアレなのでそれはなし。
おまけ
なお、このサーバは内部ネットワークなので最後にHost *
で全サーバに設定しているProxyCommand
の設定が不要なのだが、ここだけ外すにはnone
を明示してしまうという方法が有効。