Categories
Devops

Accessing vagrant machine using VS Code

I have started using VS Code a lot recently. Most of the time, it was accessing remote machines on the cloud using the Remote SSH plugin or logging in to the same machines and using VS Code directly. But, when I tried accessing my vagrant machine the same way, a big bummer.

I had an error with ssh connection.

After spending some time on google and reading, I managed to access the vagrant machine quickly.

By default, Vagrant sets up some SSH config so that it is easier to get into the VMs run with it. So to access vagrant VM, we need to traverse to the directory where we have Vagrantfile and then execute vagrant ssh command. And it simply connects with it. 

The vagrant ssh functionality obscures the parameters being used under the covers to open that SSH connection. And to access the vagrant machine using VS Code, we will require those parameter details.

Very Easy. Right?

I used the command vagrant ssh-config to print ssh parameter details.

bash-3.2$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "/Users/kunal/vagrant vm/docker-machine/.vagrant/machines/default/virtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL

Copy the output into an SSH config file. 

For my case, I added it to my default SSH config at ~/.ssh/config. 

After that, you can easily open this file in VS Code or generate a custom config file for VSCode to use by pressing ⌘⇧P and selecting Remote-SSH: Open Configuration File…

Connect VS Code to the machine with ⌘⇧P Remote-SSH: Connect to Host… and select default (where “default” is the machine name that Vagrant assigned – refer ssh-config output).

It will take a little while to connect to the VM. Now, your files on the vagrant machine are available to view and edit inside VS Code.

Leave a Reply

Your email address will not be published. Required fields are marked *