LoginSignup
491
494

More than 5 years have passed since last update.

.ssh/configファイルでSSH接続を管理する

Last updated at Posted at 2016-08-09

複数のサーバーを管理していると、毎回めんどくさい作業がSSH接続。
下記のように毎回、sshコマンドを入力しなければならない。

# ポート名を指定するケース
ssh ユーザー名@ホスト名 -p ポート番号

# 公開鍵認証のケース
ssh ユーザー名@ホスト名 -i ~/.ssh/鍵のパス

# ポート名、公開鍵認証のケース
ssh ユーザー名@ホスト名 -i ~/.ssh/鍵のパス -p ポート番号
オプション 内容
-i 秘密鍵を指定
-p ポート番号を指定
-l ログインユーザー名を指定

.ssh/configとは

SSHクライアントの設定ファイル。(リモートサーバーに接続する際に必要な設定ファイル)
SSH接続時の情報を定義しておくことで、sshコマンドのオプションを省略でき、コマンドオプションでは指定できない情報も設定できる。

.ssh/configの作成

ファイルがない場合は作成する。
.sshディレクトリのパーミッションを変更。

terminal
$ mkdir ~/.ssh
$ chmod 700 .ssh
$ touch ~/.ssh/config

.ssh/configに記載

.ssh/config

# gehoAサーバー
Host gehoA
    HostName gehogeho.com
    User gehogeho
    IdentityFile ~/.ssh/test/gehogeho_a.key
    Port 20022
    TCPKeepAlive yes
    IdentitiesOnly yes

# gehoBサーバー
Host gehoB
    HostName gehogeho.com
    User gehogeho
    IdentityFile ~/.ssh/test/gehogeho_b.key
    Port 22
    TCPKeepAlive yes
    IdentitiesOnly yes

# gehoCサーバー
Host gehoC
    HostName gehogeho.com
    User gehogeho
    IdentityFile ~/.ssh/test/gehogeho_c.key
    Port 10022
    TCPKeepAlive yes
    IdentitiesOnly yes
キーワード 内容
Host ホスト名
HostName ホストのアドレスかIPアドレス
User ログインユーザ名
IdentityFile ログインするための秘密鍵のパス
Port ポート番号(デフォルトは22)
TCPKeepAlive 接続状態を継続したい場合:yes 継続しない場合:no
IdentitiesOnly IdentityFileが必要な場合:yes 必要ない場合:no
ServerAliveInterval 一定期間サーバからデータが送られてこないときに、タイムアウトする秒数。(例) 120

サーバーに接続する

AサーバーにSSH接続する場合

terminal
$ ssh gehoA

BサーバーにSSH接続する場合

terminal
$ ssh gehoB

CサーバーにSSH接続する場合

terminal
$ ssh gehoC
491
494
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
491
494