Multihop SSH Tunnel to access Vagrant VM Service
2021-01-05T21:09:04Z
Multihop SSH tunnel to access Vagrant VM Service
Following is the scenario:
- A localmachine (192.168.0.17)
- A remote machine (192.168.0.19)
- Another vagrant machine (call it master) running inside above remote machine. This Vagrant VM has private IP 10.0.0.10.
- A web-service is listening on 10.0.0.10:31421 port inside Vagrant VM.
- We want this web-service to be accessible in the browser running in localmachine (i.e 192.168.0.17)
Get ssh port of Vagrant VM
vagrant port master
In my case it was 2200.
what does vagrant ssh port means?
Normally ssh into Vagrant VM is done by running following command.
vagrant ssh master
But you can also login to master using following command.
ssh -p 2200 vagrant@127.0.0.1 -i ~/VAGRANT/.vagrant/machines/master/virtualbox/private_key
Create Tunnel
Run the following command from host having 192.168.0.17 IP.
ssh -L "*":9090:localhost:8080 user1@192.168.0.19 "ssh -L 8080:10.0.0.10:31421 vagrant@127.0.0.1 -p 2200 -i ~/VAGRANT/.vagrant/machines/master/virtualbox/private_key"
Above command will create an ssh tunnel that will forawrd the port of the service listening on 10.0.0.10:31421 to 192.168.0.17. You can then access this service on https://192.168.0.17:9090 and https://localhost:9090/
VagrantFile used to create VM.
Following Vagrantfile was used to create VM inside remote-machine (192.168.0.19)
= [
all_hosts {
vagrant_hostname: "master",
full_hostname: "master.virtual.machine",
vmbox: "ubuntu/bionic64",
#vmbox_version: "31.20191023.0",
ip: "10.0.0.10",
memory: 4096,
cpus: 3
},
# {
# vagrant_hostname: "worker1",
# full_hostname: "worker1.virtual.machine",
# vmbox: "ubuntu/bionic64",
# #vmbox_version: "31.20191023.0",
# ip: "10.0.0.12",
# memory: 2048,
# cpus: 1
# },
]
# individual machine names must be mentioned is below command line in
# order to bring machines. (due to autostart: false)
# vagrant up master worker1
Vagrant.configure("2") do |config|
.each do |host|
all_hosts.vm.define host[:vagrant_hostname], autostart: false do |this_host|
config.vm.network :private_network, ip: host[:ip]
this_host.vm.hostname = host[:full_hostname]
this_host.vm.box = host[:vmbox]
this_host.vm.box_version = host[:vmbox_version]
this_host
.vm.provider "virtualbox" do |m|
this_host.memory = host[:memory]
m.cpus = host[:cpus]
mend
end
end
end