How to Use Remote SSH in VS Code with WSL2 Instead of the Default Windows SSH

How to Use Remote SSH in VS Code with WSL2 Instead of the Default Windows SSH

Discover how to set up `Remote SSH` in Visual Studio Code using WSL2 to overcome the default Windows SSH configuration issue. --- This video is based on the question https://stackoverflow.com/q/65271806/ asked by the user 'darkvalance' ( https://stackoverflow.com/u/12188327/ ) and on the answer https://stackoverflow.com/a/65282689/ provided by the user 'darkvalance' ( https://stackoverflow.com/u/12188327/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Remote SSH on VS Code in WSL2 password prompt Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Troubleshooting Remote SSH Connection in VS Code with WSL2 If you're a developer using Visual Studio Code (VS Code) on Windows Subsystem for Linux (WSL2), you might have encountered an issue trying to connect to your server via SSH. This issue arises when VS Code defaults to using Windows SSH instead of your WSL2 environment. This guide will guide you through how to resolve this issue effectively. The Problem When you attempt to connect to your remote server using SSH in VS Code, you might find that your connection is being routed through Windows SSH instead of utilizing your WSL2 setup. This can create complications, especially if you're dependent on Linux-based tools or configurations. You may have come across error logs similar to this: [[See Video to Reveal this Text or Code Snippet]] In this error, it's clear that the system is not recognizing the correct SSH binary, which indicates that VS Code defaults to a Windows environment. Solution: Configuring VS Code to Use WSL2 SSH To redirect VS Code to use SSH from WSL2, follow these steps: Step 1: Move Your SSH Keys First, you need to ensure that your SSH keys are accessible to the system. Copy your private keys from their original location in WSL2 to the following directory on your Windows system: [[See Video to Reveal this Text or Code Snippet]] Make sure to replace {user} with your actual Windows username. Step 2: Change Default Shell in VS Code The second step involves changing the default terminal shell used by VS Code to your WSL2 bash. Here’s how to do this: Open your VS Code settings. You can access settings by navigating to 'File' 'Preferences' 'Settings' or using the shortcut Ctrl + ,. Search for terminal shell settings. Type terminal.integrated.shell.windows in the search bar. Change the default shell path. Set the path to C:\WINDOWS\System32\bash.exe or C:\Windows\System32\wsl.exe depending on your system configuration. This change ensures that the terminal you open in VS Code routes through your WSL2 environment instead of the default Windows Command Prompt. Step 3: Verify SSH Configuration Ensure your SSH configuration is correct by checking your ~/.ssh/config file inside the WSL2 environment. Make sure it contains the correct settings for your remote servers. [[See Video to Reveal this Text or Code Snippet]] Step 4: Attempt to Connect Again With the above steps completed, go ahead and attempt to connect to your remote server via SSH in VS Code again. Open the command palette with Ctrl + Shift + P. Search for and select Remote-SSH: Connect to Host. Choose your server from the list. Conclusion Setting up Remote SSH in Visual Studio Code using WSL2 can enhance your development workflow significantly, allowing you to utilize your Linux environment directly from within Windows. By following the outlined steps, you can easily configure VS Code to use the WSL2 SSH and solve the connection issues you've faced. If you continue to experience problems, don't hesitate to check your SSH keys, configurations, or consult further resources online. Happy coding!