Why Did No One Tell Me About This VSCode Remote - SSH Feature?
I just discovered the VSCode Remote - SSH feature, and it's a game-changer (at least for me). This tool lets you code directly on a remote server (Linux VM for example) through SSH, right from your local VSCode. It brings your remote environment to your local workspace, making remote development much easier.
The Visual Studio Code Remote - SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code's feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.
No source code needs to be on your local machine to gain these benefits since the extension runs commands and other extensions directly on the remote machine.
What Problem Does it Solve?
Let me share a challenge I faced until recently. My main machine runs Windows, but for my work with automation tools like Python, Ansible, Terraform, and others such as containerlabs and Git, I prefer a Mac or Linux command line environment. I didn't want to install these tools directly on Windows due to potential issues.
Initially, I thought about using WSL, but complications with Hyper-V and nested virtualization made that impractical. If I turned on WSL2, I wouldn't be able to use other VMs that require nested virtualization.
As a workaround, I set up Ubuntu Desktop as a VM on VMware Workstation. This meant frequently switching between my host machine and the VM, which wasn't ideal, but it was manageable.
Then, I discovered something today that changed everything. I can now work directly on my Linux machine using VSCode installed on my Windows host. All I needed to do was install the Remote - SSH extension in VSCode, SSH to my Ubuntu from local VSCode and that's it.
How to Use It?
Open VSCode in your host machine, install the extension called 'Remote - SSH' as shown in the following image.
Once you’ve installed the Remote - SSH extension, click on the blue icon in the bottom left corner of the VSCode window. From there, select 'Connect to Host...' to initiate a connection to your remote environment.
After that, when prompted, enter your ssh credentials with the syntax ssh admin@10.10.10.10
for example. Once entered, you will be prompted to enter the password and you are good to go.
It’s that easy to get started and integrate your VM's workflow into your local setup. You can open any files or folders from the remote machine and start working on them as how you would do it on the remote machine.
For example, I have some folders on the Ubuntu VM as shown below. I typically access the VM via the VMWare console.
Now, I can access the same files and folders via the VScode installed on my Windows machine.
I've got to say, discovering this feels like one of the coolest things I've come across in quite some time. Maybe it's common knowledge, and I've just been living under a rock, but either way, I'm glad I found it. I hope sharing this can help make your work a bit easier too. If it’s new to you, give it a try, it might just be the trick you didn’t know you needed.
Using SSH Keys
If you don't want to enter the password every time you log in, you can use SSH keys. For example, to add your SSH key to 10.10.10.35, run the following command.
ssh-copy-id suresh@10.10.10.35
Then click on 'Connect to Host' and select 'Configure SSH Hosts' which will open the configuration file, here you can permanently add your hosts. So, next time, all you need to do is, select the host from the list. (Here you can see, I have three hosts)
Host ubuntu-desktop
HostName 10.10.0.25
User suresh
Host server-01
HostName 10.10.10.40
User suresh
Host runner-01
HostName 10.10.10.35
User suresh