Local Vagrant machine installed at IP address 10.0.0.23 with hostname lamp-vm.

Using vagrant ssh command, I can connect just fine and do everything I need.

This creates an error

$ ssh vagrant@lamp-vm -v -v

debug1: connect to address 10.0.0.23 port 22: Connection timed out
ssh: connect to host lamp-vm port 22: Connection timed out

My /etc/hosts file contains 10.0.0.23 lamp-vm.

My .ssh/config file looks like

Host lamp-vm
User vagrant
IdentityFile ~/.ssh/vagrant

I have tried the ssh command with and without the -i /path/to/.sh/identity_file as well.

How do I connect to my Vagrant Virtual Machine using SSH?


Solution 1:

It's old but as there is no answer, I'll provide one. Command:

vagrant ssh

Is the equivalent of

ssh vagrant@localhost -p 2222 -i .vagrant/machines/default/virtualbox/private_key

This is default behavior, if you changed something change command appropriately. First of all Vagrant will create vagrant user on your guest box, and you will use that user to ssh. As previous folks said, it will forward traffic from port 2222 on your host to port 22 on your guest, by default (when you use vagrant up, you see that message being displayed). And lastly Vagrant creates keys for ssh session so you don't have to, so you need to provide public key as argument when connection via ssh.

Solution 2:

I faced this problem too and this was my final configuration that allowed me to ssh into my vagrant machine from anywhere in my host machine.

Vagrantfile:

...
# Setting up private_network to have virtual host
config.vm.network :private_network, ip: "192.168.33.10"

# Enable ssh forward agent
config.ssh.forward_agent = true
...

ssh into machine:

ssh [email protected]

You will be prompted for password(default is vagrant):

[email protected]'s password:

And boom, you're in!

PS* You can use scp too anywhere in your host machine:

scp /path/to/src/file [email protected]:/path/to/destination/file

Solution 3:

This behavior is by design.

Vagrant uses VirtualBox NAT mode which means using port forwarding.

You can't SSH directly to your VM using NAT mode.

Using 'vagrant ssh' means vagrant will do the port forwarding for you so you don't have to worry about it. I think it will connect to localhost on port 2222 by default but it will try to also sort out any port number collisions.

If you need to SSH directly to your VM, switch the VM into host-only or bridged networking mode.

Solution 4:

I would have added this as a comment, but I currently do not have enough rep to do so. I've written a how-to for setting your VM to bridged mode here:

https://askubuntu.com/questions/116861/setting-up-a-network-between-a-host-and-guest-virtual-machine/116909#116909

I hope you find this useful!

Solution 5:

Windows / Vagrant / Ubuntu

This is what worked for me and you can quickly figure out if this will work by running this on the ssh client.

ssh [email protected] -p 2222 -v

The -v will put it in verbose mode and display debug info...

$ ssh [email protected] -p 2222 -v
OpenSSH_7.1p1, OpenSSL 1.0.2e 3 Dec 2015
debug1: Connecting to 127.0.0.1 [127.0.0.1] port 2222.
debug1: Connection established.
debug1: identity file /home/Jamie/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/Jamie/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.6
debug1: match: OpenSSH_6.6.1p1 Ubuntu-2ubuntu2.6 pat OpenSSH_6.6.1* compat 0x04000000
debug1: Authenticating to 127.0.0.1:2222 as 'vagrant'
debug1: SSH2_MSG_KEXINIT sent
Connection closed by 127.0.0.1

So... SSH2_MSG_KEXINIT means the keys are being exchanged. This shortly fails...

In this case, I deleted my keys and regenerated them doing this on the VM. (http://ask.xmodulo.com/sshd-error-could-not-load-host-key.html)

$ ls -al /etc/ssh/ssh*key
$ sudo rm -r /etc/ssh/ssh*key
$ sudo dpkg-reconfigure openssh-server

Once my keys were regenerated I was able to SSH into my Vagrant Box.