Connecting to Vagrant with the Plain SSH Command

vagrant ssh-config command is used to output a valid SSH configuration

* This page contains promotional content

After the earlier Vagrant does not respond , it would be a problem if the vagrant ssh command became unusable, so — belatedly — I set up connecting over plain SSH.

Connecting with plain ssh rather than using vagrant ssh seems to be faster and more responsive

In my own Vagranfile the connection is already bridged, and the default port is 2222

config.vm.network "public_network", ip: "192.168.1.10", bridge: "en0: Ethernet"

If you have not configured the network part, write it as follows

config.vm.network "forwarded_port", guest: 22, host: 2210

ssh-config

Run vagrant ssh-config in the directory where the Vagrantfile is

$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/user/vagrant/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

On the local machine that is running vagrant, all you have to do is change the default part of this config to a host name and append it to ~/.ssh/config

This command appends it in one shot

$ vagrant ssh-config --host [myHost] >> ~/.ssh/config

From another PC on the same network you can connect by transferring private_key, pointing IdetifyFile at the location of private_key, and changing the IP in HostName
The config in my case this time looks like this

Host vagrant
  HostName 192.168.1.10
  User vagrant
#  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /home/user/.ssh/vagrant_key  ## changed private_key to vagrant_key
  IdentitiesOnly yes
  LogLevel FATAL

Incidentally, Port:2222 is the port forward from the host to vagrant, and since the bridged vagrant has an IP of its own directly, when you connect from somewhere other than the host you comment out Port and connect on the default port 22

Since the Vagrant Box is bridged and has a different IP from the host, you can also connect directly without writing anything into config

$ ssh -i /Users/user/vagrant/.vagrant/machines/default/virtualbox/private_key [email protected]

See also