I would like to reproduce the way Vagrant logs in to my VM within a shell script using an ssh
command, so I create an alias to my Vagrant instance.
What
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.
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
My Env. is Win7 + Centos. The answer with most agreement doesn't work for me. After failing after trying ssh -p [port] [usrname]@127.0.01
, I just use XShell to add a new session with the vagrant port and user name.
It works.
Maybe Xshell is a candinate.
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
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 doing vagrant ssh-config
) and where's your private SSH key (also visible when doing vagrant 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
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.