SSH to Vagrant from VScode

Liz Rice
2 min readJan 2, 2020

--

I’ve just been introduced to the life-changing experience of connecting to a remote machine from inside VScode using the Remote-SSH extension (thanks Itay Shakury!) But I needed one tiny extra bit of information so I could use this to connect to Vagrant VMs on my laptop.

I use Vagrant a lot for example, to run a VM with Kubernetes as installed by vanillakubeadm. By default Vagrant sets up some SSH config so that it’s super easy to get into the VMs you run with it — all you need to do iscd to the directory holding the Vagrantfile, and then simply run vagrant ssh.

This is great, but it obscures the parameters being used under the covers to open that SSH connection — and you’ll need that information in order to configure Remote-SSH. Turns out it’s really easy!

# Change to the directory containing the Vagrantfile
~$ cd vagrant/machine
# Get the SSH config that Vagrant uses
~/vagrant/machine$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/liz/vagrant/machine/.vagrant/machines/default/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL

Copy the output of this into an SSH config file — I added it to my default SSH config at ~/.ssh/config. In VScode you can easily open this file, or generate a custom config file for VSCode to use, by pressing ⌘⇧P and selecting Remote-SSH: Open Configuration File…

Connect VScode to the machine with ⌘⇧P Remote-SSH: Connect to Host… and select default (where “default” is the machine name that Vagrant assigned). The first time you connect it will take a little while to download the VScode server onto the VM. Voilà, your files on the host available to view and edit inside VScode.

Bonus: if you put the config in your default SSH config file, you can now also SSH into the box from your laptop terminal with ssh default, saving you the bother of moving into the Vagrant machine’s directory.

--

--

Liz Rice

Containers / eBPF / security / open source @isovalent @ciliumproject / cycling / music @insidernine