Skip to content

Instantly share code, notes, and snippets.

@bubba-h57
Last active August 8, 2024 07:30
Show Gist options
  • Save bubba-h57/3b212d8a8d56e1c35218515dd220371c to your computer and use it in GitHub Desktop.
Save bubba-h57/3b212d8a8d56e1c35218515dd220371c to your computer and use it in GitHub Desktop.
Configuring Jetbrains Gateway and WSL

Step 1: SSH Daemon

In your WSL instance, re-install OpenSSH server as follows.

sudo apt remove --purge openssh-server
sudo apt install openssh-server

Edit /etc/ssh/sshd_config (e.g. sudo vi /etc/ssh/sshd_config) and add the following lines to the bottom of the file. Ensure you replace WSL_ACCOUNT_NAME with your WSL2 account name.

PasswordAuthentication yes
AllowUsers WSL_ACCOUNT_NAME 

(Optional) For security, allow only specific IP addresses to login. E.g. to allow 192.30.118.x and 194.57.240.32 only, do as follows. edit as follows.

Edit /etc/hosts.deny (e.g. sudo vi /etc/hosts.deny). and add the floowing line:

sshd: 192.30.118.0/24 194.57.240.32

Restart the SSH server.

sudo service ssh --full-restart

Allow SSH server to start without password. Run sudo visudo and find the %sudo ALL=(ALL:ALL) ALL. Immediately below that line, add this line.

%sudo ALL=NOPASSWD: /etc/init.d/ssh

Step 2: Auto Start SSH Daemon

In /etc/wsl.conf:

[boot]
command = service ssh start
See https://learn.microsoft.com/en-us/windows/wsl/wsl-config for the documentation of the WSL config files

Or check out this StackExchange answer.

Step 3: Resolve WSL Hostname

Microsoft hasn't built this in for you. Fortunately, Shane did. Install and setup the go-wsl2-host service. That will then handle the DNS resolution for your WSL instance.

Step 4. Jetbrains Gateway

Download and install Jetbrains Gateway. Once you have it running, configure an SSH connection to your WSL instance, and map a project directory. Choose the Jetbrains IDE you want to use and go to it. The gateway will download and install an agent (headless client) that will run locally within the WSL instance.

@maxwellamaral
Copy link

Thanks!

@MSYCCE
Copy link

MSYCCE commented Aug 4, 2022

After following the instructions, I am able to ping the wsl host, but Jetbrains Gateway fails to connect with a "Connection Refused" message. Thoughts?

@bubba-h57
Copy link
Author

Revert the /etc/hosts.deny and try it.

@MSYCCE
Copy link

MSYCCE commented Aug 4, 2022

Nope! I checked and there were no hosts named in hosts.deny. I went ahead and renamed it anyway but had the same result.

@MSYCCE
Copy link

MSYCCE commented Aug 4, 2022

I set the ssh port to 2022 in sshd_config and set the host in JetBrains Gateway to localhost port 2022 and it connected.

@MSYCCE
Copy link

MSYCCE commented Aug 4, 2022

I also set ListenAddress 0.0.0.0

@markusheiden
Copy link

markusheiden commented Sep 24, 2022

There is an easier way to expose the SSH server in WSL 2:

In %UserProfile%\.wslconfig:

[wsl2]
localhostForwarding = true

That will make (not just) the SSH server visible on 127.0.0.1 only.

With admin rights:

C:\Windows\System32\netsh.exe interface portproxy add v4tov4 listenaddress=YourIpAddress listenport=SshPort connectaddress=127.0.0.1 connectport=WslSshPort

That will make the SSH server visible under your IP address. To see if the port proxy had been created, use:

netsh interface portproxy show v4tov4

This can be automated via the Windows task scheduler.
I needed to delay the execution for 15 seconds to make it work reliably though and priorly delete the old port proxy via:

C:\Windows\System32\netsh.exe interface portproxy delete v4tov4 listenaddress=YourIpAddress listenport=SshPort 

Don't forget to configure your firewall.

@markusheiden
Copy link

markusheiden commented Sep 24, 2022

There is an easier way to autostart the SSH server in WSL 2:

In /etc/wsl.conf:

[boot]
command = service ssh start

See https://learn.microsoft.com/en-us/windows/wsl/wsl-config for the documentation of the WSL config files

@bubba-h57
Copy link
Author

Thanks @markusheiden

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