LoginSignup
65
63

More than 5 years have passed since last update.

SSHの鍵多すぎ問題を解決する

Last updated at Posted at 2016-11-08

以前にFacebookである人が、SSHの接続情報の管理に ~/.ssh/config を使ってたんだけど、鍵が多すぎて修羅場みたいなことを書いてて、その時は特に何も思いつかなかったんですが、以下のような方法はどうでしょうね?

ワーキングディレクトリにSSHのコンフィグを置く。

.ssh-config というファイル名で内容は以下のような感じのあれです。

Host example.com
  HostName xxx.xxx.xxx.xxx
  User vagrant
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /path/to/private_key
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

上の例は vagrant ssh-config で出したものを例にしているので、細かいところは気にしないでください。笑

SSHコマンドのエイリアスを作成

~/.bash_profile に以下のような関数を設置。なにをしているかというと、ワーキングディレクトリに .ssh-config というファイルがあると、-F オプションにそれを渡しているだけ。

function ssh {
    if [ -f ./.ssh-config ]; then
        /usr/bin/ssh -F ./.ssh-config $@
    else
        /usr/bin/ssh $@
    fi  
}

以上で、ワーキングディレクトリに移動したときにそこに .ssh-config があるとそれが読み込まれて ssh example.com だけで接続できる感じ。

各プロジェクトのディレクトリに .ssh-config を置いとくといい感じかも。

まだ実際にそういう運用はしたことないですが、試しに仕込んでしばらく様子を見ます。

65
63
4

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
65
63