14

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?

8 Answers 8

17

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.

2
  • This is the real and right answer! Works without issues, with mobaxterm for example. You need also to specify full path for private_key
    – sineverba
    Sep 16, 2019 at 20:39
  • If you want to know the exact command vagrant ssh runs, run it with --debug
    – Heuriskos
    Mar 15, 2023 at 11:57
8

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
1
  • While this works, it's quite fragile - I found that our Vagrantfile caused some edits to /etc/network/interfaces on the VirtualBox VM, which meant my SSH connection would drop. A localhost connection (ssh -p 2222 vagrant@localhost) won't be affected by this.
    – RichVel
    Mar 7, 2016 at 16:50
7

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.

3
  • Thanks Philip but how would I go about solving that? Sorry for the inexperience.
    – csi
    Oct 23, 2012 at 16:41
  • 1
    I am using host-only mode and the problem persists.
    – csi
    Oct 23, 2012 at 17:01
  • Should be the accepted answer. Very helpful to understand this - going via localhost on port 2222 was the route to a working Vagrant setup (for some reason I couldn't get the insecure_private_key key to work yet.) I found that the standard 'insecure private key' didn't work, so I ended up specifying a different private key and username in the Vagrantfile, but the localhost port 2222 part didn't need changing.
    – RichVel
    Mar 7, 2016 at 16:52
6

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!

1
  • +1 here and on AU.SE; nice write-up.
    – msanford
    Oct 23, 2012 at 18:25
4

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.

1
  • 1000% fixed my issue. Adding the -v flag to the client connecting showed exactly the SSH2_MSG_KEXINIT message, as stated above, and server-side auth.log corroborated the story. In my case, the keys were just missing entirely. I ended up generating them with the linked tutorial's ssh-keygen suggestion and it works flawlessly now. Jan 2, 2021 at 7:08
0

Destroyed the virtual machine
Reloaded the virtual machine
Everything worked

Not sure why but obviously something didn't load properly on the first provisioning.

0

Check out this comment: https://github.com/mitchellh/vagrant/issues/1936#issuecomment-21139674

Basically, even when using private networking, vagrant still does ssh on 127.0.0.1. Try that ssh command @127.0.0.1 instead of your private ip/hostname.

0

By executing vagrant ssh-config, you can see the SSH details:

Host default
  HostName <host_ip>
  User <user>
  Port <port>
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile <identity_file_path>
  IdentitiesOnly yes
  LogLevel FATAL
  ForwardAgent yes

Then, make sure you have added config.ssh.forward_agent = true to your Vagrantfile.

Finally, you can have SSH connection to your box, directly by executing ssh <user>@<host_ip> -p <port> -i <identity_file_path>.

P.S. Vagrant also have built-in SSH functionality. You can SSH to your boxes simply by running vagrant ssh command.

P.S. If you have changed the Vagrantfile and have the instance running, it is needed to suspend the box and start the box again by executing the following commands:

vagrant suspend
vagrant up

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .