Skip to content

Instantly share code, notes, and snippets.

@leoluyi
Forked from dentechy/WSL-ssh-server.md
Last active February 15, 2022 16:15
Show Gist options
  • Save leoluyi/04f1b895d5ff4203032cdd5ff8bd544d to your computer and use it in GitHub Desktop.
Save leoluyi/04f1b895d5ff4203032cdd5ff8bd544d 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
powershell.exe "& 'C:\Windows\System32\bash.exe' -c 'sudo /etc/init.d/ssh start && sudo /usr/bin/systemctl start systemd-user-sessions.service'"

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. On the WSL put to the end of the /etc/sudoers file with command sudo visudo with the following lines:
      %sudo ALL=NOPASSWD: /etc/init.d/ssh start
      %sudo ALL=NOPASSWD: /usr/bin/systemctl start systemd-user-sessions.service
      %sudo ALL=NOPASSWD: /usr/sbin/sshd
      
    2. Put the bat file start-sshd.bat to the shell:Startup folder.
  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