Skip to content

Instantly share code, notes, and snippets.

@yuchen
Forked from dentechy/WSL-ssh-server.md
Last active February 16, 2022 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuchen/23af93bc1c1511ff5e79707ed053e651 to your computer and use it in GitHub Desktop.
Save yuchen/23af93bc1c1511ff5e79707ed053e651 to your computer and use it in GitHub Desktop.
[WSL 相关] wsl 相关文档 #wsl #ssh

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment