LoginSignup
4
5

More than 5 years have passed since last update.

Vagrant の仮想ゲストにインターネットから ssh 接続する(Vagrant Share --ssh)

Posted at

Vagrant環境がファイアーウォールの内側にあったり、プロバイダーからプライベートアドレスしか割り当てられていなくても、インターネットからHTTPでVagrant の仮想ゲストにアクセスする方法は、「Vagrant の仮想ゲストにインターネットから http 接続する(Vagrant Share)」にまとめていますが、 ssh でも簡単に同様のことができます(ただし、httpの場合とは異なり、sshアクセスする側にもvagrantがインストールされている必要があります)。

おおまかな手順は、以下のとおりです。

手順概要

1.HashiCorpのAtlasにアカウントを作成(httpの場合と同じ

2.Atlasにログイン(vagrant loginコマンド)(httpの場合と同じ

3.ポートフォワーディングの設定(Vagrantfile)(デフォルトで設定されている)

4.Atlasへ接続し、キーペアを生成・配備し、アクセス用Share Nameを取得(vagrant share --sshコマンド)

5.(外部ホスト側)Atlasにログイン(vagrant loginコマンド)(httpの場合と同じ

6.(外部ホスト側)アクセス用Share Nameを使って、外部からアクセス(Vagrant connect --sshコマンド)

手順詳細

1.httpの場合と同じ

2.httpの場合と同じ

3.ポートフォワーディングの設定(Vagrantfile)(デフォルトで設定されている)

デフォルトで転送されていますので、設定は不要です。

4.Atlasへ接続し、キーペアを生成・配備し、アクセス用Share Nameを取得(vagrant share --sshコマンド)

vagrant share --sshを実行すると、以下の処理が行われます。

A.キーペア(公開鍵と秘密鍵)が生成されます。
B.公開鍵は、仮想ゲストへuploadされます。
C.秘密鍵は、プロンプトで要求されるパスワードを使って暗号化されて、vagrantshare.comのサーバへ送られます。なお、パスワードは、転送されません。
D.Share Nameと呼ばれるアクセス用の識別子が提示されます。

なお、vagrant share は、デフォルトで http を転送しますので、"--ssh"とした場合には、sshに加えてhttpも転送されています。httpは転送したくない場合には、"--disable-http"を指定します。

$ vagrant share --ssh
==> default: Detecting network information for machine...
    default: Local machine address: 127.0.0.1
    default:
    default: Note: With the local address (127.0.0.1), Vagrant Share can only
    default: share any ports you have forwarded. Assign an IP or address to your
    default: machine to expose all TCP ports. Consult the documentation
    default: for your provider ('virtualbox') for more information.
    default:
    default: Local HTTP port: 8080
    default: Local HTTPS port: disabled
    default: SSH Port: 2222
    default: Port: 2222
    default: Port: 8080
==> default: Generating new SSH key...
    default: Please enter a password to encrypt the key:
Error! Your console doesn't support hiding input. We'll ask for
input again below, but we WILL NOT be able to hide input. If this
is a problem for you, ctrl-C to exit and fix your stdin.
     default: Please enter a password to encrypt the key:

ここで、パスワードを入力します。なお、"Error! Your console doesn't support hiding input."のエラーは、WindowsでCygwin の mintty を使っているときに出ます。パスワードをこの入力画面上で非表示にできないというものです。

     default: Please enter a password to encrypt the key: xxxxxxxxxx
    default: Repeat the password to confirm:
Error! Your console doesn't support hiding input. We'll ask for
input again below, but we WILL NOT be able to hide input. If this
is a problem for you, ctrl-C to exit and fix your stdin.
     default: Repeat the password to confirm:

確認のためパスワードを再度入力します。

     default: Repeat the password to confirm: xxxxxxxxxx
    default: Inserting generated SSH key into machine...
==> default: Checking authentication and authorization...
==> default: Creating Vagrant Share session...
    default: Share will be at: miserable-okapi-9192
==> default: Your Vagrant Share is running! Name: miserable-okapi-9192
==> default: URL: http://miserable-okapi-9192.vagrantshare.com
==> default:
==> default: You're sharing your Vagrant machine in "restricted" mode. This
==> default: means that only the ports listed above will be accessible by
==> default: other users (either via the web URL or using `vagrant connect`).
==> default:
==> default: You're sharing with SSH access. This means that another user
==> default: simply has to run `vagrant connect --ssh miserable-okapi-9192`
==> default: to SSH to your Vagrant machine.
==> default:
==> default: Because you encrypted your SSH private key with a password,
==> default: the other user will be prompted for this password when they
==> default: run `vagrant connect --ssh`. Please share this password with them
==> default: in some secure way.

ここで、入力したパスワードは外部(インターネット)からアクセスする際に必要となります。また、"==> default: Your Vagrant Share is running! Name: miserable-okapi-9192"で"Name: "の後に提示されている文字列(Share Name)も必要となります。

5.(外部ホスト側)Atlasにログイン(vagrant loginコマンド)(httpの場合と同じ

外部ホスト側にも vagrant がインストールされている必要があります。

6.(外部ホスト側)アクセス用Share Nameを使って、外部からアクセス(Vagrant connect --sshコマンド)

ローカル(Vagrantの仮想ゲストが動作している側)での準備が整いましたので、いよいよ外部(インターネット)からsshアクセスします。

外部ホスト側にも vagrant と ssh がインストールされていることが必要です。

コマンドは、vagrant connect --sshです。
vagrant share --sshした際のパスワードとShare Nameが必要となります。

vagrant connect --sshの引数として、Share Name を渡します。途中でパスワード入力を要求されます。

$ vagrant connect --ssh miserable-okapi-9192
Loading share 'miserable-okapi-9192'...
The SSH key to connect to this share is encrypted. You will require
the password entered when creating to share to decrypt it. Verify you
access to this password before continuing.

Press enter to continue, or Ctrl-C to exit now.

ここで、「Enter」を押します。

Password for the private key:
Error! Your console doesn't support hiding input. We'll ask for
input again below, but we WILL NOT be able to hide input. If this
is a problem for you, ctrl-C to exit and fix your stdin.
 Password for the private key:

ローカル側でvagrant share --ssh時に入力したパスワードを入力します。

 Password for the private key: xxxxxxxxxx
Executing SSH...

ここで、数秒待っても先に進まない場合は「Enter」を押します。

Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.13.0-49-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Wed Apr 22 05:06:15 UTC 2015

  System load:  0.0               Processes:           78
  Usage of /:   2.9% of 39.34GB   Users logged in:     0
  Memory usage: 25%               IP address for eth0: 10.0.2.15
  Swap usage:   0%

  Graph this data and manage this system at:
    https://landscape.canonical.com/

  Get cloud support with Ubuntu Advantage Cloud Guest:
    http://www.ubuntu.com/business/services/cloud


Last login: Wed Apr 22 05:06:15 2015 from 10.0.2.2
vagrant@vagrant-ubuntu-trusty-64:~$

ゲストOS(ここでは、Ubuntu)のプロンプトが表示されました。
仮想ゲストへ ssh でログインできました。

なお、下記の警告が表示される場合があります(外部ホスト側)ので、「アクセスを許可する(A)」をクリックします。

image

補足

暗号化された秘密鍵は、vagrantshare.comのサーバへ転送されますが、これを暗号化する際に使ったパスワードは転送されません。そのため、vagrantshare.com側では、秘密鍵の暗号を解読できませんのでsshの通信内容を盗み見することはできません。また、暗号化された秘密鍵は、vagrant connect --sshを実行することで、vagrantshare.comのサーバを経由して、vagrant connect --sshを実行している外部のコンピュータに転送されることとなりますが、暗号化したパスワードを知られない限り、秘密鍵を得ることはできませんので、仮に Share Nameを推測されたり、総当たり等で突き止められとしても(暗号化したパスワードを知らない)他人がsshで仮想ゲストへログインすることはできません。

さらにセキュリティを強化したい場合には、vagrant share --sshの際に--ssh-onceオプションを指定すると、Share Nameが一度のsshセッションにしか使えなくなります。一旦 ssh セッションを切って、再接続しようとするとエラーとなります。

$ vagrant connect --ssh ecstatic-monster-0740
Loading share 'ecstatic-monster-0740'...
...
...
...
vagrant@vagrant-ubuntu-trusty-64:~$ exit
logout
Connection to 127.0.0.1 closed.
$ !!
vagrant connect --ssh ecstatic-monster-0740
Loading share 'ecstatic-monster-0740'...
The share 'ecstatic-monster-0740' hasn't enabled SSH sharing!
Ask the person sharing their Vagrant environment to specify
the --ssh flag when running `vagrant share`.

Another possibility is that the share did previously allow sharing,
but the one-time use private key has been used already. Please ask
the person sharing their environment to reshare. If you intend on
using SSH multiple times, make sure the share side is not specifying
the `--ssh-once` flag.
4
5
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
4
5