Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vagrant forwarding ssh from remote server

Tags:

I set up vagrant to run a vm on a host os. What I would like to do is be able to ssh from other machines directly into the vagrant vm (ie, I shouldn't ssh into the host and then vagrant ssh, etc. into the vagrant vm).

Currently, I can ssh not using vagrant ssh from the host os using ssh [email protected] -p 2222. However, if I run the same command (replacing 127.0.0.1 with the host's ip address), I get "ssh connect to host XXXXX port 2222: Connection refused."

I tried adding my own port forwarding rule to vagrant:

config.vm.network :forwarded_port, guest: 22, host: 2222 

But that doesn't allow ssh connection from either the host machine or any other machine in the network. Additionally, I spent a while with config.ssh in the vagrant docs. I think that most of those parameters though specify what port the vagrant vm is running ssh on.

I really don't think this should be that difficult. Does anyone know what I might be doing wrong, or what I should do differently to ssh into a vagrant vm from a remote server?

like image 919
Behram Mistree Avatar asked Oct 06 '13 01:10

Behram Mistree


People also ask

How do I connect to SSH with vagrant?

Simply CMD-SHIFT-P then "Remote-SSH: Connect to Host..." and the ssh . config entry you just added is automatically listed - you simply select it and voila, vscode connects to your remote vagrant vm!

What is the command to connect to a running virtual machine using vagrant?

Command: vagrant ssh [name|id] [-- extra_ssh_args] This will SSH into a running Vagrant machine and give you access to a shell.


1 Answers

If you don't want to change network to public you can override default port forwarding for ssh by this:

config.vm.network :forwarded_port, guest: 22, host: 2222, host_ip: "0.0.0.0", id: "ssh", auto_correct: true 

This will forward guest 22 port to 2222 on your host machine and will be available from any ip, so you can access it outside your local machine.

like image 118
TlmaK0 Avatar answered Oct 21 '22 12:10

TlmaK0