Skip to content

Instantly share code, notes, and snippets.

@dentechy
Last active July 14, 2024 03:54
Show Gist options
  • Save dentechy/de2be62b55cfd234681921d5a8b6be11 to your computer and use it in GitHub Desktop.
Save dentechy/de2be62b55cfd234681921d5a8b6be11 to your computer and use it in GitHub Desktop.
A step by step tutorial on how to automatically start ssh server on boot on the Windows Subsystem for Linux

How to automatically start ssh server on boot on Windows Subsystem for Linux

Microsoft partnered with Canonical to create Bash on Ubuntu on Windows, running through a technology called the Windows Subsystem for Linux. Below are instructions on how to set up the ssh server to run automatically at boot.

  1. Edit the /etc/ssh/sshd_config file by running the command sudo vi /etc/ssh/sshd_config and do the following
    1. Change Port to 2222 (or any other port above 1000)
    2. Change PasswordAuthentication to yes. This can be changed back to no if ssh keys are setup.
  2. Restart the ssh server:
    • sudo service ssh --full-restart
  3. With this setup, the ssh server must be turned on every time you run Bash on Ubuntu on Windows, as by default it is off. Use this command to turn it on:
    1. sudo service ssh start
  4. Follow the next steps which will create scripts that start the ssh server automatically:
    1. Create a sshd.bat file and edit it with the following commands:

      • vi sshd.bat
      • Add the following code: C:\Windows\System32\bash.exe -c "sudo /usr/sbin/sshd -D"
      • Save the file and move it to a more accessible location, e.g. mv ssh.bat /mnt/c/Users/YourUserName/Documents. Make sure to match your username! Take note of this location for the next step as in Windows language this corresponds to C:\Users\YourUserName\Documents
    2. Create a sshd.vbs file and edit it with the following commands:

      • vi sshd.vbs
      • Add the following code, making sure to put in your actual user name:
      Set WinScriptHost = CreateObject("WScript.Shell")
      WinScriptHost.Run Chr(34) & "C:\Users\YourUserName\Documents\sshd.bat" & Chr(34), 0
      Set WinScriptHost = Nothing
      
      • Save the file and move it to a more accessible location, e.g. mv sshd.vbs /mnt/c/Users/YourUserName/Documents.
      • Open start menu, type run. Then type shell:startup. Copy the vbs file over to the Startup folder
    3. Finally, you will need to configure the ssh server to start without requiring password. Run the command sudo visudo and add this line to the end of the file:

      • %sudo ALL=NOPASSWD: /usr/sbin/sshd
  5. If configured properly, the ssh server should now automatically start in the background when Windows starts.
@haipnh
Copy link

haipnh commented Nov 2, 2021

Another way without Task Scheduler:

  1. On the WSL put to the end of the /etc/sudoers file the following line:
    %sudo ALL=NOPASSWD: /etc/init.d/ssh start
  2. Put to the shell:Startup folder bat file with the content:
    powershell.exe "& 'C:\Windows\System32\bash.exe' -c 'sudo /etc/init.d/ssh start'"

That's it!

no sudo promt no Task Scheduler stuff no stucking daemon process like sudo /usr/sbin/sshd -D no WSL window open

Thanks ! What a concise way.

@quantenschaum
Copy link

It does not work as described, you need to run C:\Windows\System32\bash.exe -c "sudo /usr/sbin/sshd -D"``, otherwise /run/sshd` is missing.
I did not user the VBA thing, just created a task running on startup.

@sogemao
Copy link

sogemao commented Apr 10, 2022

I succeeded. I did this:
a、create “/etc/init.wsl” in linux,Write the following:
#! /bin/sh
/etc/init.d/ssh start

b、create “ubuntu.bat” on win,Write the following:
bash /etc/init.wsl

c、Move the “ubuntu.bat” to the Windows startup directory:
C:\Users\<YourUserName>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

@drunkpig
Copy link

use nssm is more prefect.

@pkumarg
Copy link

pkumarg commented Jan 31, 2023

This should have been easier. I'm not going to do this. I appreciate that you explain the steps to do it. But Linux does it better, and easy. So if Microsoft want to feed us their WSL2 bull**** then should make it better.

@anishsane
Copy link

I know that this is a pretty old thread.
wsl2 now has systemd support, which means that we can create startup services.

@hyeonbeenlee
Copy link

hyeonbeenlee commented May 11, 2023

For me, the two easiest ways to achieve this were:

Way 1: Installing the Docker Desktop with WSL2 based engine (set as default if WSL2 is installed), and turning on "Settings->General->Start Docker Desktop when you log in".

Way 2: Install 'Terminal' app from Microsoft Store->Settings->Startup->Set Default profile as WSL distro and check 'Launch on mahine startup'
-> I felt this is more stable than the first one when running Pytorch codes.

Test it by rebooting your Windows.
I've verified these on both WIndows 10 and 11 running WSL2 with Ubuntu-22.04 distro.
Hope this relieve your pain.

@laplacericky
Copy link

You don't need to modify the sudoers file. Just run this on startup instead: wsl.exe -u root -- sudo /usr/sbin/service ssh start using task scheduler

@eo1989
Copy link

eo1989 commented Jul 3, 2023

You don't need to modify the sudoers file. Just run this on startup instead: wsl.exe -u root -- sudo /usr/sbin/service ssh start using task scheduler

If you're logging into wsl as root, is it necessary to add sudo to the one-liner? seems unnecessary to do so. Just a question for clarification if other newbies such as myself stumbleupon this little wsl2/ssh treasure.

@laplacericky
Copy link

You don't need to modify the sudoers file. Just run this on startup instead: wsl.exe -u root -- sudo /usr/sbin/service ssh start using task scheduler

If you're logging into wsl as root, is it necessary to add sudo to the one-liner? seems unnecessary to do so. Just a question for clarification if other newbies such as myself stumbleupon this little wsl2/ssh treasure.

wsl.exe -u root -- /usr/sbin/service ssh start will work the same.

@Heng-Zhou
Copy link

Heng-Zhou commented Jan 3, 2024

The command to start SSH server is sudo /usr/sbin/service ssh start, so all we need to do to start SSH server when WSL2 is booting is to add this command in ~/.bashrc. This is a function of the guest Ubuntu OS, so we should configure completely inside the guest OS; why do we bother ourselves to create files outside of WSL on Windows? Since this command requires sudo password, we can create a file, say, service-ssh-start in /etc/sudoers.d/ directory and add a line <WSL username> ALL=(root) NOPASSWD: /usr/sbin/service ssh start in it to avoid prompting for sudo password.

@cognivore
Copy link

It's a bit strange. It somehow used to start automatically for me, but I recently had to turn HyperV off and on again, and it stopped. No idea what changed, just reporting my experience. Now I run wsl and sudo service ssh start every time I log on like a clown.

@donutti2
Copy link

This guide is for "after login" not for "on boot, before login".

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