6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ssh_configでの落とし穴

Last updated at Posted at 2018-04-05

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を明示してしまうという方法が有効。

  1. ssh config最強設定

6
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?