SSH into remote Ubuntu Virtual Machine using VS Code, with a username of my choice, not the currently logged in Windows user

3 min read 22-10-2024
SSH into remote Ubuntu Virtual Machine using VS Code, with a username of my choice, not the currently logged in Windows user

In the modern age of remote development, accessing your virtual machines (VMs) securely is paramount. If you're using Visual Studio Code (VS Code), it provides a streamlined way to connect to remote Ubuntu VMs using SSH. In this guide, we will walk you through the process of SSH-ing into a remote Ubuntu VM with a custom username of your choice, rather than the currently logged-in Windows user.

Understanding the Problem

Often, when attempting to connect to a remote server via SSH, the default username may be set to your current Windows account username. However, there are scenarios where you may want to log in using a different username that has been configured on the Ubuntu server. The goal is to customize your SSH access while leveraging the powerful features of VS Code for a seamless development experience.

Original Code Problem Statement

To connect to your remote Ubuntu VM, one might initially think to use the SSH command like this:

ssh <current_user>@<ip_address>

However, if you want to use a different username, the command needs to be modified.

Steps to SSH into a Remote Ubuntu VM Using VS Code

1. Install the Remote - SSH Extension

First, ensure that you have the Remote - SSH extension installed in VS Code. This extension allows you to open any folder on a remote machine using SSH.

  • Open VS Code.
  • Go to the Extensions view (Ctrl + Shift + X).
  • Search for "Remote - SSH" and install it.

2. Configure SSH Settings

Next, you need to set up your SSH configuration file. Follow these steps:

  1. Open your SSH config file:

    • On Windows, this file is typically located at C:\Users\<YourUsername>\.ssh\config. If it doesn’t exist, you can create one.
  2. Add a Host entry:

    • Insert the following configuration, replacing <custom_username>, <remote_ip_address>, and the desired key file path as necessary:
    Host my-ubuntu-vm
        HostName <remote_ip_address>
        User <custom_username>
        IdentityFile C:\Users\<YourUsername>\.ssh\id_rsa
    

3. Connect to Your Remote VM

Now that your SSH configuration is set up, it’s time to connect:

  • Press F1 in VS Code and type Remote-SSH: Connect to Host....
  • Choose my-ubuntu-vm from the dropdown list.

VS Code will now attempt to connect to your remote Ubuntu VM using the specified username. If this is your first time connecting, you might need to accept the server's fingerprint.

4. Open Your Project

Once connected, you can open a folder or file on the remote VM and start coding right away. The integrated terminal in VS Code will now operate as if you're directly on the remote machine.

Example Use Case

Consider a scenario where you have a remote Ubuntu server set up for a web application project. Your main development work occurs in VS Code on your Windows machine. Using the instructions above, you can set up a username specifically for this project, ensuring that you keep your Windows user account separate from your development activities.

Troubleshooting Tips

  • If you encounter issues connecting, double-check your SSH configuration file for typos or incorrect paths.
  • Ensure that your remote server is running and that you have the correct IP address and port.
  • Check your firewall settings to ensure that they allow SSH traffic.

Conclusion

Connecting to a remote Ubuntu VM via SSH using a custom username in VS Code is a straightforward process that enhances your development workflow. By following the steps outlined above, you can easily set up a productive environment tailored to your specific needs.

Additional Resources

By using this guide, you can simplify your workflow and improve your efficiency when working on remote servers. Happy coding!