Skip to content

Instantly share code, notes, and snippets.

@LRKasri
Created April 8, 2024 14:04
Show Gist options
  • Save LRKasri/0478996e0c38c1a2d846491876782d6e to your computer and use it in GitHub Desktop.
Save LRKasri/0478996e0c38c1a2d846491876782d6e to your computer and use it in GitHub Desktop.
VSCode using SSH to connect to remote WSL2

VSCode using SSH to connect to remote WSL2

  • Before starting:
    • I have tried the New-ItemProperty method myself but it keep asking me to type my password and show that my password is wrong, so I tried some other methods and this is working best for me.
    • This markdown will contain each step from how to set up SSH to service to finally use VSCode to remotely connected to my remote PC. There is a minor bug that when using nvidia-smi in terminal, it is not correctly shown. However, you can still use you GPU without any problem in your program.
    • This walkthrough is novice-friendly, since I am a novice myself, and please leave a comment if anything is confusing or no need to do.
    • Thank you for reading, hope you enjoy the journey and have fun using WSL2 remotely!

Prerequisite/Testing connection

  1. One remote Windows computer and one local computer(any OS you like)
  2. Public IP address (If not, some technique like NAT traversal is needed)
  3. VSCode installed in your local machine

Step1: Installation of SSH on WSL2 and Windows

  1. Installation of SSH on Windows: please refer to Get started with OpenSSH for Windows

  2. Installation of SSH on WSL2:

    # Run this on WSL2 terminal
    sudo apt remove openssh-server # To ensure the latest version
    sudo apt install openssh-server

Step 2: Test the connection between your local and remote windows

# Run in powershell/cmd/terminal
ping <windows_ip> # You can obtain <windows_ip> from running ipconfig on windows powershell
# If the connection is OK, please continue to next step

extra step: If not, please follow this to allow ICMP and test connection

  1. Enter Control Panel -> System and Security -> Windows Defender Firewall
  2. Click into Advanced settings
  3. Enter Inbound Rules -> New Rule
  4. Choose Custom -> Protocol type: ICMPv4 -> Scope: *Both* Any IP address -> Action: Allow the connection -> Profile: Apply all -> Name: *Any name will work*

extra extra step: If still not working, please check your Internet connection (IP setup or ...)

Step 3: Test the connection between remote WSL2 and remote windows

ssh <WSL2_id>@localhost
# If not working try to re-install SSH service on WSL2
# Run this on windows powershell
# wsl --shutdown
# wsl
# Run this on WSL2
# sudo service ssh restart

Step 4: Test the connection between WSL2 and Internet

# Run this on WSL2
ping google.com

Remote SSH to WSL2 with SSH Jump

Step 1: enable automatic start of SSH when WSL2 starts

#Run this in WSL2
sudo systemctl enable --now ssh 

Step 2: Test if SSH Jump works

#In your local machine
ssh -J windows_user@windows_ip wsl_usrs@localhost

Step 3: Manage you local SSH config and connect to WSL2 remote in VSCode

Host wsl
	HostName localhost
	User wsl_user
	ProxyJump windows_user@windows_ip
	StrictHostKeyChecking no
	UserKnownHostsFile=/dev/null

Some other tips

  1. If you want to SSH into your remote machine more easily without typing password every time, you can use ssh-keygen in both your local machine and remote Windows. Then copy your content in id_rsa.pub into ~/.ssh/authorized_keys
    1. local_key -> remote Windows & remote_Windows_key -> remote WSL2
  2. If in come cases, your port 22 is blocked as due to the control of your Internet provider, you need to change the sshd_config with new <port> and always remember to restart OpenSSH when you change the config. Try to follow these steps:
    1. Change the C:\ProgramData\ssh\sshd_config
    2. Change firewall settings:
      1. Enter Control Panel -> System and Security -> Windows Defender Firewall
      2. Click into Advanced settings
      3. Enter Inbound Rules -> New Rule
      4. Choose Port -> TCP and <port> -> Allow -> all the way Next -> set the Name of the rule

Credits

[1] https://gist.github.com/mattbell87/f5bd7b78c8d0ad7f0dfc3addae4f4897?permalink_comment_id=4762099#gistcomment-4762099

[2] https://blog.csdn.net/Hydius/article/details/130138387

[3] https://blog.csdn.net/qq_34965596/article/details/119176798

[4] https://www.zhihu.com/question/439351137

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment