Showing posts with label SSHTunnel. Show all posts
Showing posts with label SSHTunnel. Show all posts

Wednesday, 6 January 2021

Multihop SSH Tunnel to access Vagrant VM Service

Multihop SSH Tunnel to access Vagrant VM Service

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 = [
    {
        "master",
        "master.virtual.machine",
        "ubuntu/bionic64",
        #vmbox_version: "31.20191023.0",
        "10.0.0.10",
        4096,
        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|

    all_hosts.each do |host|
        config.vm.define host[], false do |this_host|
            this_host.vm.network , host[]
            this_host.vm.hostname = host[]
            this_host.vm.box = host[]
            this_host.vm.box_version = host[]

            this_host.vm.provider "virtualbox" do |m|
                m.memory = host[]
                m.cpus = host[]
            end
        end
    end
end