LoginSignup
3
7

More than 5 years have passed since last update.

vagrant up時にsshポートフォワードが衝突して起動しない場合の対処

Posted at

Vagrant で複数のプロジェクトの仮想マシンを同時に立ち上げようとしたとき、SSHのフォワードポートがすでに埋まってると言われて、同時起動しなくなることがあります。

Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 2222 is already in use on the host machine.
:

基本、Vagrant は 2222 が埋まっていれば 2200 などの代替ポートを使おうとします。しかし vagrant suspend していたものを再稼働させたときだけは、SSHへのフォワードポートの衝突を自動解決しません。

エラーメッセージの最後にこうあります

Sometimes, Vagrant will attempt to auto-correct this for you. In this case, Vagrant was unable to. This is usually because the guest machine is in a state which doesn't allow modifying port forwarding. You could try 'vagrant reload' (equivalent of running a halt followed by an up) so vagrant can attempt to auto-correct this upon booting. Be warned that any unsaved work might be lost.

vagrant reload して、ゲストをきっちり再起動させると、自動的に衝突が解消されます。

==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key

Fixed port collision とありますね。2222 が使えなかったので 2200 になりました。以後、2つのマシンで vagrant suspendvagrant up のみを繰り返しているうちは、このマシンはずっと 2200 を使い、別のマシンは 2222 を使い続けます。

もし、慌てて別のポート番号を設定しようと Vagrantfile で固定ポートを指定すると、いよいよこいつが衝突したときは、解消方法がなくなって詰みます。

config.vm.network :forwarded_port, guest: 22, host: 1234
# こういうことはしなくていい
3
7
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
3
7