ropz
ropz

Reputation: 128

Two separate Vagrant machines, windows host, PuTTY - how?

I'd like to create two vagrant machines via two vagrant files, and be able to ssh into them via PuTTY.

I thought it might be as simple as port forwarding one of them via, say, port 2223 instead of 2222, and using two PuTTY connections.

Despite my vagrant ssh-config looking like this:

HostName 127.0.0.1
User vagrant
Port 2223
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "XXXXXXXXXXXXXXXXXXXXXXXXXXX"
IdentitiesOnly yes
LogLevel FATAL

... I seem able to ssh into it via PuTTY on port 2222, which I'm hoping to reserve for access to the other instance which I've not yet set up. I'm new to vagrant and may be making a noob mistake. Help appreciated.

Upvotes: 0

Views: 891

Answers (2)

jmetcher
jmetcher

Reputation: 98

You can set any port you like by putting this in your vagrantfile:

config.vm.network "forwarded_port", guest: 22, host: 2223

replacing 2223 with your port of choice - different for each VM, obviously.

Note that this is in addition to the standard 2222 port forwarding, which will still be mapped for every VM. One of them "wins" and answers on 2222 as well as whatever custom port you set up.

The procedure in the accepted answer may well work, but it seems a little convoluted.

Upvotes: 1

Terry Wang
Terry Wang

Reputation: 13920

As per Vagrant Base Box specification, the default networking mode is NAT and port forwarding for SSH is enabled (guest 22 => host 2222).

What you've done, changing the sshd_config file within the guest won't work because that only changes the SSH port within the guest to 2223, NOT the host.

For the 2nd vagrant box, you need to do the following:

  1. Get the name or UUID VBoxManage list vms
  2. Use VBoxManage showvminfo VM_Name to get the list of port forwarding rules
  3. Delete its default guestssh NAT port forwarding rule
  4. Add a new port forwarding rule to do guest 22 => host 2223

For example:

By default the rule is named ssh

NIC 1 Rule(1):   name = ssh, protocol = tcp, host ip = 127.0.0.1, host port = 2222, guest ip = , guest port = 22

Delete it

VBoxManage modifyvm "VM_Name" --natpf1 delete "ssh"

Add a new rule

VBoxManage modifyvm base_box --natpf1 "guestssh, tcp,,2223,,22"

NOT DONE yet!!!

Do NOT use vagrant up to start this VM, because it'll add the ssh rule back.

You can use VirtualBox GUI or VBoxManage controlvm to start it. And connect to it using ssh -p 2223 vagrant@localhost, password is vagrant. You can also use the insecure key pair to do public key authentication, doesn't make much sense though.

NOTE: changing, adding and deleting port forwarding rules can be done using the VirtualBox GUI anyway, if it is easier for you.

Upvotes: 2

Related Questions