Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ssh to vagrant without actually running "vagrant ssh"?

People also ask

What is vagrant SSH?

Command: vagrant ssh [name|id] [-- extra_ssh_args] This will SSH into a running Vagrant machine and give you access to a shell. On a simple vagrant project, the instance created will be named default.

How do I know if vagrant is running?

Command: vagrant status [name|id] This will tell you the state of the machines Vagrant is managing. It is quite easy, especially once you get comfortable with Vagrant, to forget whether your Vagrant machine is running, suspended, not created, etc. This command tells you the state of the underlying guest machine.

How do I quit vagrant SSH?

Vagrant SSH Once you are done exploring the virtual machine, you can exit the session with CTRL-D.


There's a lot of answers already, but they all seem overly complicated or solve problems the asker didn't have.

simply:

# save the config to a file
vagrant ssh-config > vagrant-ssh

# run ssh with the file.
ssh -F vagrant-ssh default

I've had to re-implement "vagrant ssh" because it's -c option didn't pass on arguments properly. This is basically what it does (there might be more, but it works fine this way)

#!/bin/sh
PORT=$(vagrant ssh-config | grep Port | grep -o '[0-9]\+')
ssh -q \
    -o UserKnownHostsFile=/dev/null \
    -o StrictHostKeyChecking=no \
    -i ~/.vagrant.d/insecure_private_key \
    vagrant@localhost \
    -p $PORT \
    "$@"

As a one-liner (with thanks to kgadek):

ssh $(vagrant ssh-config | awk 'NR>1 {print " -o "$1"="$2}') localhost

To account for when you have more than one vagrant host, this will select the desired host, as well as cull blank lines from the config (using sed):

HOST=name-of-my-host
ssh $(vagrant ssh-config $HOST | sed '/^[[:space:]]*$/d' |  awk 'NR>1 {print " -o "$1"="$2}') localhost

In terminal run

vagrant ssh

In another terminal window/tab run

ps aux | grep ssh

There you will see the actual command executed by Vagrant, something like this:

ssh [email protected] -p 2222 -o Compression=yes -o DSAAuthentication=yes -o LogLevel=FATAL -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i ~/.vagrant.d/less_insecure_private_key -o ForwardAgent=yes

Just pass the entire vagrant ssh-config as a config file to ssh with the -F configfile parameter. The host alias to connect to is defined on the first line in vagrant ssh-config; Host default means you can connect with ssh default.

I couldn't see an option to read the config file from the standard input, so went with the temp file route. Here's a one-liner that also cleans up the temporary $TMPDIR.vagrant-ssh-config file afterwards. It needs to be executed in the same directory as your Vagrantfile, assuming you vagrant box is up and running.

vagrant ssh-config > $TMPDIR.vagrant-ssh-config && ssh default -F $TMPDIR.vagrant-ssh-config ; rm $TMPDIR.vagrant-ssh-config

Note: on my Mac OSX system, $TMPDIR expands to /var/folders/46/yltlhtgx8m5cg68_w95wgvy41324gn/T/ (right now). Use another variable, or another folder, if it's not set on your system.


I solved this in a very simple way: when you start the vagrant box it shows the ssh address like this

SSH address: 127.0.0.1:2222

then you can connect to the box by using the vagrant user, the host and the port you get

ssh [email protected] -p 2222

If you don't need to use stdin with ssh (for example you want to execute just a command and logout) you could use:

vagrant ssh-config --host default | ssh -F /dev/stdin default

This method was suggested in response to a similar question on google groups.

Unfortunately bash process substitution doesn't work either (see this question on unix.stackexchange for more details).

The best options you have, if you want an interactive shell, are to create a temp file and use that with ssh -F or use awk as suggested by the other answers.


You can add vagrant host configuration to your local ssh config.

  1. vagrant ssh-config >> ~/.ssh/config

  2. ssh vagrant@{host}

ex. cat ~/.ssh/config

Host kmaster
  HostName 127.0.0.1
  User vagrant
  Port 2222..
  ....
  • ssh vagrant@kmaster