Skip to content

Instantly share code, notes, and snippets.

@ryanwilsonperkin
Created May 3, 2017 13:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanwilsonperkin/b73db9b5d44d868455f9492eb3b74764 to your computer and use it in GitHub Desktop.
Save ryanwilsonperkin/b73db9b5d44d868455f9492eb3b74764 to your computer and use it in GitHub Desktop.
Faster SSHing into vagrant

Running vagrant ssh can be slow because it boots up a ruby process, does a bunch of analysis to determine your ssh parameters, and then actually invokes the command. If you want a faster login, try the following:

# Compute the ssh parameters, append them to your global ssh config file
# The --host parameter specifies what you want the vagrant machine to be called
vagrant ssh-config --host vagrant >> ~/.ssh/config

# Now just ssh into that machine directly
ssh vagrant

A couple caveats:

  1. Vagrant still needs to be running for you to ssh into it. vagrant ssh fails with a helpful error message when the machine isn't running, while ssh vagrant will just fail silently. Not super helpful.
  2. If you do a bunch of fiddling with vagrant, it's possible that the parameters in your .ssh/config file may become out of date. I've been running this configuration for several months now without any problems but ymmv. To fix that issue, open your .ssh/config file, delete the block that starts with "Host vagrant" and then run the first command again.
@chriscz
Copy link

chriscz commented Sep 30, 2023

Here's a little script I wrote, it also checks that the config is valid (by testing the ssh connection) and regenerates it if not:
https://gist.github.com/chriscz/ee552707efc142844c049b1778e99957
Tested on Fedora 36.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment