How can I enable a service such as OpenSSH server to start when WSL2 is started ? Currently I have to restart it every time WSL2 is started with sudo service ssh start.
- 17,574
- 4
- 44
- 81
- 563
- 2
- 6
- 14
-
2Note for reviewers -- Not a duplicate of [this question](https://superuser.com/q/1112007/1210833) or [this one](https://superuser.com/q/1343558/1210833). Those questions deal with the much more complicated use-case of starting a WSL when *Windows* boots, not when WSL2 starts. – NotTheDr01ds Jan 28 '22 at 13:55
1 Answers
The preferred way of running any service when WSL starts depends on your Windows version:
Windows 11
You can now execute an arbitrary command line when starting an instance by creating/editing /etc/wsl.conf (via sudo) with the following:
[boot]
command="service ssh start"
This command runs as root and generates no output. If you need to run multiple commands, they should be semicolon separated (or something like &&) inside the command= string.
Update: There appears to me to be a bug in this feature that will cause the WSL instance to terminate if it is not in use, even if the process started with the boot.command is still running. This may not be a problem for many users, as you may be running the instance anyway and notice or care if it stops when you exit the shell, but you should be aware of the behavior.
Windows 10
On WSL with Windows 10, you'll need to start the service via one of your user's shell startup scripts.
Use the following syntax in your ~/.bash_profile:
wsl.exe -u root service ssh status || wsl.exe -u root service ssh start
wsl.exe -u root has the advantage of not requiring the sudo password when starting up every time. From PowerShell and CMD, it can be called without the exe, but from within WSL it does require the extension.
Of course, you can also use sudoers to suppress the requirement for the password, but WSL just makes this unnecessary.
Note that this will generate one or two messages every time you start. To suppress this, use syntax such as:
wsl.exe -u root service ssh status > /dev/null || wsl.exe -u root service ssh start > /dev/null
Other options
These methods are for running commands when WSL starts, which is a fairly straightforward case. There are more complicated variations of this in other Super User questions that will:
- Run when the Windows user logs in
- Run when the Windows computer boots
- 17,574
- 4
- 44
- 81
-
The Windows 11 way doesn't work for me. "service ssh status" reports that ssh is running, but I can neither connect with Putty on it and also not with Visual Studio 2022 (cross-plattform development). – Bonita Montero Jan 28 '22 at 14:14
-
@BonitaMontero And just to sanity check, you *can* connect to it when starting it manually? If so, what happens if you do a `sudo service ssh restart` manually? Can you then connect to it? – NotTheDr01ds Jan 28 '22 at 14:18
-
When I automatically start ssh, I can't connect, when I restart afterwards, I can connect both with VS2022 as well as with Putty. – Bonita Montero Jan 28 '22 at 18:26
-
@BonitaMontero Very odd. I have two different instances on Windows 11 and they both work fine with starting OpenSSH server in the `[boot]` `command`. The only differences I can think of (and they shouldn't matter) is that I'm starting them on port 2222 (for Ubuntu) and 2224 (for openSUSE - also no `service` command there). And I'm connecting using PowerShell and the Windows OpenSSH client. I'll keep thinking about possible reasons why yours isn't able to connect, but mine is. – NotTheDr01ds Jan 28 '22 at 20:12
-
@BonitaMontero I'm also assuming that you are connecting from the Windows host itself, not from another machine on the network, right? – NotTheDr01ds Jan 28 '22 at 20:13
-
I've had success with running [dispatcher](https://github.com/131/dispatcher) (win / x64 version) via the windows task scheduler. – Mark Jun 13 '23 at 23:07