Skip to content

Instantly share code, notes, and snippets.

@bityob
Forked from dentechy/WSL-ssh-server.md
Last active April 23, 2024 21:08
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bityob/8a92b050c30f7bb5cbbbd6ea9264e6e9 to your computer and use it in GitHub Desktop.
Save bityob/8a92b050c30f7bb5cbbbd6ea9264e6e9 to your computer and use it in GitHub Desktop.
A step by step tutorial on how to automatically start ssh server when launching Windows Subsystem for Linux

How to automatically start ssh server when launching 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. Uninstall and reinstall the ssh server using the following commands:
    1. sudo apt remove openssh-server
    2. sudo apt install openssh-server
  2. 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 UsePrivilegeSeparation to no
    3. Change PasswordAuthentication to yes. This can be changed back to no if ssh keys are setup.
  3. Restart the ssh server:
    • sudo service ssh --full-restart
  4. 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
  5. Add in the end of .bashrc
    # Run ssh on logging, only if not running yet
    if [ ! "$(ps -elf | grep -v grep | grep /usr/sbin/sshd)" ];
        then sudo /usr/sbin/sshd;
    fi
  6. 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
@serivas
Copy link

serivas commented Apr 3, 2020

On the 5th point, is there any way to use "sudo service ssh start" instead? I get errors when skipping the init.d script :S

EDIT: Figured it out, just had to change to %sudo ALL=NOPASSWD: /usr/sbin/service ssh *

@sciafri
Copy link

sciafri commented Feb 2, 2021

Based the fact that I get errors or warnings, the following things should be changed:

  1. UsePrivilegeSeparation is depreciated, and based on this comment thread JetBrains/clion-wsl#2 which links to the release log - it is on by default. Do not include it in the sshd_config or you will get a warning that its deprecated.
  2. Change #5 to the following as servias mentioned above to use service ssh start as otherwise you will get an error:
    # Run ssh on logging, only if not running yet if [ ! "$(ps -elf | grep -v grep | grep /usr/sbin/sshd)" ]; then sudo service ssh start; fi
  3. Change 6 to the following as servias mentioned above to support password-less start of ssh via service ssh start"
    add this line to the end of the file:"
    %sudo ALL=NOPASSWD: /usr/sbin/service ssh *

@bityob
Copy link
Author

bityob commented Feb 2, 2021

Based the fact that I get errors or warnings, the following things should be changed:

  1. UsePrivilegeSeparation is depreciated, and based on this comment thread JetBrains/clion-wsl#2 which links to the release log - it is on by default. Do not include it in the sshd_config or you will get a warning that its deprecated.
  2. Change #5 to the following as servias mentioned above to use service ssh start as otherwise you will get an error:
    # Run ssh on logging, only if not running yet if [ ! "$(ps -elf | grep -v grep | grep /usr/sbin/sshd)" ]; then sudo service ssh start; fi
  3. Change 6 to the following as servias mentioned above to support password-less start of ssh via service ssh start"
    add this line to the end of the file:"
    %sudo ALL=NOPASSWD: /usr/sbin/service ssh *

Will review it and update. Thanks

@Immick
Copy link

Immick commented Apr 23, 2024

@bityob
Linux version 5.15.146.1-microsoft-standard-WSL2 (root@65c757a075e2) (gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.37) #1 SMP Thu Jan 11 04:09:03 UTC 2024

The given instruction doesn't work anymore. with my WSL. Could you tell me please how to fix it?

Thank you!

@bityob
Copy link
Author

bityob commented Apr 23, 2024

@Immick

I verified now and it seems to work with even less commands -

  1. sudo apt install openssh-server -y
  2. sudo vim /etc/ssh/sshd_config - Change the port from Port 22 to Port 3333
  3. Restart sshd for changes to take affect - sudo service ssh --full-restart
  4. Now you should be able to ssh to your WSL instance using ssh <username>@localhost -p 3333

Verified on Ubuntu 22.04 distro and WSL package versions: 5.15.146 and 5.15.133

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