Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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
# こういうことはしなくていい
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away