LoginSignup
81
79

More than 3 years have passed since last update.

~/.ssh/configを分割して管理する

Last updated at Posted at 2014-06-02

2021-1-26
こちらにInclude対応版の記事を書きました

2021-1-13
こちらに最新版の記事を書きました

管理するサーバが増減するたびに変更を加えるOpenSSHの設定ファイル~/.ssh/config

長くなりすぎて管理しづらくなると何とかこれを分割したくなりますね?ファイルが分割できれば、gitで管理したり、一部をメンバーとプロジェクトで共有できたりできて便利そうです。ここでは私が利用している~/.ssh/configの分割方法をご紹介します。

設定を分割する

~/.ssh/conf.dというディレクトリを作ってこの中に~/.ssh/configを分割して配置します。プロジェクトメンバーと共有したい設定はmyproject.confのようにシンボリックリンクで解決します。

~/.ssh
├── authorized_keys
├── conf.d
│   ├── config
│   ├── myproject.conf -> ~/tumf/projects/myproject/ssh.conf
│   └── myservers.conf
├── config
├── id_dsa
├── id_dsa.pub
└── known_hosts

すべての設定に共通するような設定は~/.ssh/conf.d/configにおいてください。

設定を結合する

OpenSSHに分割した設定を読んでくれるような機能があれば良いのですが、そのような機能は見当たらないので~/.ssh/conf.d/*.confを以下のコマンドで結合します。

cat ~/.ssh/conf.d/config ~/.ssh/conf.d/*.conf

この結合コマンドを、~/.ssh/conf.d内を修正するたびに実行しても構わないのですが、この一手間を省くために私は以下のようなエリアスを設定しています。

alias ssh="cat ~/.ssh/conf.d/config ~/.ssh/conf.d/*.conf > ~/.ssh/config;ssh"
alias scp="cat ~/.ssh/conf.d/config ~/.ssh/conf.d/*.conf > ~/.ssh/config;scp"
alias git="cat ~/.ssh/conf.d/config ~/.ssh/conf.d/*.conf > ~/.ssh/config;git"
alias knife="cat ~/.ssh/conf.d/config ~/.ssh/conf.d/*.conf > ~/.ssh/config;knife"

かなりベタな感じですが ~/.ssh/conf.dBitbucketあたりにpushすると複数のクライアントで共有できて便利です。

もっといい方法があれば是非教えて下さい。

81
79
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
81
79