Enable SSH in WSL system
Solution 1
A few things to note when trying to use SSH on WSL2:
- As others have mentioned, since WSL does not yet support the
systemd
init system and infrastructure, you'll need to rely on other methods. The WSL version of Ubuntu still provides the oldinit.d
style scripts for most services. So usingsudo service ssh start
(orrestart
, orstatus
, orstop
, etc.) is what you'll use.
But that's just the beginning of it. WSL2 also runs in a VM with a virtual network interface that is NAT'd, so you won't be able to ssh
into the WSL instance from any other machine on the network without additional effort. WSL does provide automatic localhost forwarding, though, so you can ssh
in from Windows on the same machine, or from another WSL instance on the same machine.
If you just need terminal access to WSL from a remote machine, then here's a far easier solution:
- Install OpenSSH server in Windows (instructions).
- Access your WSL instance remotely using
ssh -t windows_user@windows_host wsl
. That just connects to the Windows host, allocates a pseudo-terminal with-t
, and runs thewsl
command using that pseudo-terminal.
If, on the other hand, you need real SSH access to the WSL instance, then the "usual answer" is fairly complicated still. See this Github comment and thread and my summary of your general options for port forwarding here.
That's "in general" though. For ssh
we can make it easier by using an ssh server in both the Windows host and the WSL instance. See a "short version" of this in my answer here and (far) more details if you need them in this answer.
The "far more details" answer also includes a solution to starting the ssh server remotely, since it won't automatically run at boot time under WSL.
Solution 2
For WSL you'll want to use the correct syntax:
sudo service ssh status
This will give you what you're looking for:
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2021-05-22 19:54:00 JST; 3h 25min ago
Docs: man:sshd(8)
man:sshd_config(5)
Process: 2129 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
Main PID: 2137 (sshd)
Tasks: 1 (limit: 9127)
Memory: 1.8M
CGroup: /system.slice/ssh.service
└─2137 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
5月 22 19:54:00 Carbon systemd[1]: Starting OpenBSD Secure Shell server...
5月 22 19:54:00 Carbon sshd[2137]: Server listening on 0.0.0.0 port 22.
5月 22 19:54:00 Carbon sshd[2137]: Server listening on :: port 22.
5月 22 19:54:00 Carbon systemd[1]: Started OpenBSD Secure Shell server.
Related videos on Youtube
vico
Updated on September 18, 2022Comments
-
vico over 1 year
I am trying to enable SSH in my Ubuntu running in WSL2 system:
sudo systemctl status ssh
I get this error:
System has not been booted with systemd as init system (PID 1). Can't operate. Failed to connect to bus: Host is down
What does it mean and how can I enable ssh?
-
Terrance almost 3 yearsSince WSL does not use systemd you will need to launch the ssh as a service instead.
sudo service ssh start
.
-
-
NotTheDr01ds almost 3 yearsWhile the command you cite is correct, the output is not what you will receive on WSL. On WSL,
service ssh status
uses the oldinit.d
style scripts, so you'll just see* sshd is running
orsshd is not running
as the output of the command. -
Admin almost 2 yearsThis may have recently changed? I have just installed Debian WSL on Windows 10, ran
sudo apt-get install openssh-server
andsudo service ssh start
. A Windows firewall dialog prompted me to permit access for a port and I was immediately able to ssh from a remote machine. It also was already enabled on restart! -
Admin almost 2 years@blee Hmmm. Maybe? But I'm suspecting something else might be at play here. First,
Get-Service |? { $_.Name -like "*ssh*" }
from PowerShell - Is the Windows OpenSSH server running? Second,wsl.exe -l -v
-- Is the WSL instance version 1 or 2? Version 1 won't have any issues with port forwarding. But if it really is WSL1, then I'd still be confused about why it's there on restart. -
Admin almost 2 yearsYou're right about Debian openssh-server being there on restart, I must have tricked myself about that. Derp. PowerShell does not show any ssh server running and says my Debian WSL is version 1. Moreover, I think I'll follow your advice and install Windows ssh as I'd really like to be able to shut the machine off remotely anyway. Thanks!