I would like to reproduce the way how Vagrant logs to my VM within a shell script using ssh
command, so I create an alias to my Vagrant instance.
What is the command syntax to use the regular ssh
command to access it?
I would like to reproduce the way how Vagrant logs to my VM within a shell script using ssh
command, so I create an alias to my Vagrant instance.
What is the command syntax to use the regular ssh
command to access it?
Vagrant stores the private key in
~/.vagrant.d/insecure_private_key
and uses it to connect to every machine throughssh
, considering that it is configured to connect on port 2200 (default) it would be something like:ssh vagrant@localhost -p 2200 -i ~/.vagrant.d/insecure_private_key
Note: make sure that the private key is owned by the user running
Vagrant
.Though if your aim is to have a multi-machine environment you may do so using
config.vm.define
.Here's an example illustrating an environment with 2 machines, one called
web
and the other isdatabases
:Then you will have all Vagrant commands available per machine, i.e.
vagrant ssh web
andvagrant provision databases
.I solved this in a very simple way: when you start the vagrant box it shows the ssh address like this
then you can connect to the box by using the vagrant user, the host and the port you get
In terminal run
In another terminal window/tab run
There you will see the actual command executed by Vagrant, something like this:
If you just want the bare minimum command to connect to your box, you need to know the port that it's using (printed when doing
vagrant up
, or visible doingvagrant ssh-config
) and where's your private SSH key (also visible when doingvagrant ssh-config
)Then it's just a matter of providing the key and port:
ssh -p 2222 -i $HOME/vagrantenv/.vagrant/machines/default/virtualbox/private_key vagrant@127.0.0.1
Just pass the entire
vagrant ssh-config
as a config file tossh
with the-F configfile
parameter. The host alias to connect to is defined on the first line invagrant ssh-config
;Host default
means you can connect withssh 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 yourVagrantfile
, assuming you vagrant box is up and running.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.There is a way that replicates how a remote user might login to the system
config.vm.network "private_network", ip: "192.168.33.10"
This adds a private IP for the host (make it what you wish in the 192.168 range so long as its not already used
your_virtual_host_name.pem
You'll find the key under .vagrant\machines\default\virtualbox\private_key
Go to your home directory and do your usual Unix ssh, so
ssh -i your_virtual_hostname.pem username@192.168.33.10
where username, may well be vagrant if you have a standard box, look at the output of vagrant ssh-config for ssh standard details for the box.
That's it