Is there an easy way to have WSL Ubuntu services start automatically on Windows startup?
This also works on WSL 2.
Solution 1: use VBE script
Click on Start and search for shell:startup. also you can go manually to:
C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
now create a new file sshserver.vbe
and add this text and save:
Set ws = CreateObject("Wscript.Shell")
ws.run "wsl -d Ubuntu -u root service ssh start", vbhide
Solution 2: Task Scheduler
Click on Start and search for Task Scheduler.
-
On Task Scheduler click on Action and select Create Basic Task
-
On Create Basic Task Fill out Name and Description "Auto Start SSH server"
-
On Trigger select: When the computer starts.
-
On Action select Start a program
-
On Start a Program type path:
C:\Windows\System32\wsl.exe
, next on add Add arguments (optional):-d Ubuntu -u root service ssh start
.
You should have something like this on the last step.
Related videos on Youtube
PF92
Updated on September 18, 2022Comments
-
PF92 over 1 year
I have a question about the WSL version of Ubuntu. Is there an easy way to have bash start automatically when Windows starts and pass a command to it to start the SSH server? So that the SSH server runs all the time when Windows runs.
-
PF92 over 4 yearsWhen I try this command I get: "/bin/bash: -d: invalid option". When I remove -d Ubuntu but keep -u root there is a similar error. With "wsl service ssh start" it runs as a non-root user and fails. "wsl sudo service ssh start" asks for a password so it cannot be autorun at startup. Perhaps after editing visudo/sudoers it could work...
-
Cornea Valentin over 4 yearsuse wsl not bash if you look close all my calls was made from windows not inside Linux. correct one: C:\Windows\System32\wsl.exe -d Ubuntu -u root service ssh start
-
Cornea Valentin over 4 yearsi forgot to mention this it's for wsl 2
-
Cornea Valentin over 4 yearsanother solutin to use bash it's to add in your /etc/sudoers this : << username >> ALL=(ALL:ALL) NOPASSWD: ALL , replace << username >> with your username then call: C:\Windows\System32\bash.exe -c "sudo service ssh start"
-
WinEunuuchs2Unix over 4 yearsI thought WSL2 was still in beta?
-
PF92 over 4 yearsSo wsl.exe on my laptop is an older version probably. Also, all Linux processes are killed when the shell exits. This command works for me: "wsl.exe sh -c 'sudo /usr/sbin/service ssh start; cat'", adding cat keeps the ssh server running. I used 'visudo' to add this line to sudoers: "<username> ALL = (ALL:ALL) NOPASSWD: /usr/sbin/service ssh start" (only running this command is allowed without a password). Later I will try if it works on Windows startup using one of the methods.
-
PF92 over 4 yearsYes it works on startup, using the method with Startup folder and VB script (with wsl command changed like in the previous comment). Thanks for the help.
-
CodingMatters over 4 yearsC:\Users\<< username >>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup on windows 10 home ver 1909 the dir location is different to above. C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
-
cmario about 4 yearshow can I do in not WSL 2?