Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A step by step tutorial on how to automatically start ssh server on boot on the Windows Subsystem for Linux running Ubuntu 18.04

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


This works with Ubuntu 18.04LTS. I make no promises about other distributions, but direct anyone else to an older Gist which apparently works for 16.04LTS.

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. Uninstall and reinstall the ssh server using the following commands:
    1. sudo apt remove openssh-server
    2. sudo apt install openssh-server
  2. 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
  3. 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 /etc/init.d/ssh start"
      • Save the file and move it to a more accessible location, e.g. mv sshd.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: /etc/init.d/ssh
  4. If configured properly, the ssh server should now automatically start in the background when Windows starts.

My thanks go to dentechy for his original GitHubGist on this subject. I found that the instructions he provides do not work if you've upgraded your Windows Subsystem Linux Ubuntu to 18.04LTS since the UsePrivilegeSeparation option has been depreciated on the ssh server for 18.04LTS. The above is my workaround.

@macxfadz

This comment has been minimized.

Copy link

commented Jul 7, 2018

Thank you very much.

@mloskot

This comment has been minimized.

Copy link

commented Sep 29, 2018

For those who have installed multiple WSL distributions, it is important to switch the default from Xenial to Bionic:

C:\>wslconfig /l
Windows Subsystem for Linux Distributions:
Ubuntu (Default)
kali-linux
Ubuntu-18.04
Debian

C:\>wslconfig /setdefault Ubuntu-18.04

C:\>c:\Windows\System32\bash.exe
mloskot@bionic:/mnt/c$
mloskot@bionic:/mnt/c$ lsb_release -d
Description:    Ubuntu 18.04.1 LTS

(mloskot@bionic: is my custom prompt)

More on https://docs.microsoft.com/en-us/windows/wsl/wsl-config

I think it should be possible to change the startup command in sshd.bat to:

C:\Windows\System32\bash.exe -c "sudo /usr/sbin/service ssh --full-restart"

and have sudo visudo

%sudo ALL=(ALL) NOPASSWD: /usr/sbin/service ssh --full-restart
@DWagenk

This comment has been minimized.

Copy link

commented Oct 6, 2018

Then type shell:startup. Copy the vbs file over to the Startup folder

This starts the server on login of your current windows-user. I use the Task Scheduler to execute the vbs file On startup, so I don't have to log into the physical computer (or enable autologin) to start the ssh server. Pitfall: also select Run whether user is logged on or not, that's the whole point...


If you are interested in also running windows executable from inside the ssh session (like you could when starting WSL on the local machine, because the Windows PATH variable is automatically appended to the Linux PATH variable in that case) you can use the following procedure:

add an export_winpath.bat script

set WINPATH=%PATH%
set WSLENV=WINPATH/pu
C:\Windows\System32\bash.exe -c "echo \"$WINPATH\" > ~/.winpath"

The path conversion is done through the WSL environment variable handling, see this blogpost for details. Also add the line

WinScriptHost.Run Chr(34) & "C:\Users\YourUserName\Documents\export_winpath.bat" & Chr(34), 0

to your vbs script, so whenever your ssh server is (re)started from windows, the content of PATH is also made accessible inside WSL in ~/.winpath file.

Now append it to your WSL PATH variable like this:

export PATH=$PATH:$(cat ~/.winpath)

and add the above line to ~/.bashrc, if you want that to happen automatically on every login to WSL.

@dowcet

This comment has been minimized.

Copy link

commented Jan 6, 2019

So no need to use a higher port like 2222 per @dentechy, it seems, as long as port 22 is open in Windows Firewall.

@Pakbon

This comment has been minimized.

Copy link

commented Jan 31, 2019

I found there's an even easier way to do this.

  • Create sshdstart.sh script in your (wsl) home folder
  • Write in .sh script sudo service ssh start
  • Create a task in task scheduler
  • Start on boot
  • Start program "bash" with arguments "/home/yourusername/sshdstart.sh"
  • Do step 3, iii of above guide
@coatl5d

This comment has been minimized.

Copy link

commented Apr 12, 2019

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.