How to ssh to vagrant without actually running "vagrant ssh"?
ShellCommand LineSshVagrantShell Problem Overview
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 is the command syntax to use the regular ssh
command to access it?
Shell Solutions
Solution 1 - Shell
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
Solution 2 - Shell
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
Solution 3 - Shell
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
Solution 4 - Shell
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.
Solution 5 - Shell
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 vagrant@127.0.0.1 -p 2222
Solution 6 - Shell
You can add vagrant host configuration to your local ssh config.
-
vagrant ssh-config >> ~/.ssh/config
-
ssh vagrant@{host}
ex. cat ~/.ssh/config
Host kmaster
HostName 127.0.0.1
User vagrant
Port 2222..
....
-
ssh vagrant@kmaster
Solution 7 - Shell
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.
Solution 8 - Shell
If you just want to set it up so you can use normal the normal ssh commandline, as well as scp and such, you can run vagrant ssh-config
and append the output to your default ssh configuration. If you replace the line "Host default" with a more descriptive hostname, you should be good to go.
vagrant ssh-config |sed -e "s/Host default/Host my_cool_dev_box/" >> ~/.ssh/config
ssh my_cool_dev_box
Solution 9 - Shell
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 [email protected]
Solution 10 - Shell
A lot of the other answers assume you have Vagrant installed.
I have Vagrant installed on Windows 10, but I can't vagrant ssh
because I'm using PuTTy as my SSH client, which vagrant won't accept.
> The ssh
executable found in the PATH is a PuTTY Link SSH client.
Vagrant is only compatible with OpenSSH SSH clients.
However, in Windows 10 we also have Bash on Ubuntu on Windows. So, I just use that with the following command:
ssh vagrant@127.0.0.1 -p2222 -i .vagrant/machines/default/virtualbox/private_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=Fatal
It's easy enough to install Vagrant on Win10-Ubuntu but it also wants you to install Virtualbox for some reason, which I'd rather not do.
N.B. I've tried with the ssh default -F vagrant-ssh-config
method, but I just get
> Permission denied (publickey,password).
I'm guessing this is because the IdentityFile
path is a Windows path, whereas in Bash, it should begin with /mnt/c/
. I suppose you could just write out the file and then modify it if that works better for you.
Solution 11 - Shell
There is a way that replicates how a remote user might login to the system
- Edit the Vagrantfile for your instance adding in
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
- Restart the instance with vagrant reload from the command line
- Copy the vagrant private_key file to some Linux equivalent you should have running on your box (e.g. Cygwin if on windows, I use windows 10) to your cygwin home directory, renaming it along the way to something describing the host the key is to be used for, e.g.
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 [email protected]
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
Solution 12 - Shell
Vagrant stores the private key in ~/.vagrant.d/insecure_private_key
and uses it to connect to every machine through ssh
, 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 is databases
:
config.vm.define 'web', primary: true do |web|
web.vm.box = 'CentOS64'
web.vm.hostname = 'vic-develop'
web.vm.network 'private_network', ip: '192.168.50.10', virtualbox__intnet: true
web.vm.synced_folder '../code', '/var/www/project', :mount_options => ["dmode=777,fmode=777"]
web.vm.provision 'ansible' do |ansible|
ansible.playbook = 'development-web.yml'
ansible.sudo = true
end
end
config.vm.define 'databases' do |db|
db.vm.box = 'CentOS64'
db.vm.network 'private_network', ip: '192.168.50.20', virtualbox__intnet: true
db.vm.network :forwarded_port, guest: 3306, host: 8206
db.vm.provision 'ansible' do |ansible|
ansible.playbook = 'development-db.yml'
ansible.sudo = true
end
end
Then you will have all Vagrant commands available per machine, i.e. vagrant ssh web
and vagrant provision databases
.
Solution 13 - Shell
ssh vagrant@<host>
password: vagrant
Examples:
-
or after checking the IP (from the inside, using
vagrant ssh
)ssh [email protected]
Solution 14 - Shell
You can add ssh config for your vagrant host to ssh config.
-
Get ssh config for vagrant machine in vagrant folder:
vagrant ssh-config
-
Open
{UserDir}/.ssh/config
and append there result from the previous command. Note: the first lineHost default
mean the alias which you will use later forssh
command. Name it as your vagrant machine or dir. If you have only one vagrant dir - you can name itHost vagrant
-
Ssh to vagrant:
ssh vagrant
. The last name is alias from the previous step.
Solution 15 - Shell
You can take any of the ssh-config
arguments, and pass them to ssh on the commandline as -o Key=value
. So, for a simple one-host vagrant setup (you might have to do a little more work with grep
or perl
for a multihost setup), you can do something like the following (or replace perl
with sed
if you want):
ssh `vagrant ssh-config | tail -8 | perl -pe 's/^\s+/-o@/; s/\s/\=/;s/@/ /;s/\n/ /'` vagrant@localhost
Solution 16 - Shell
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.