29

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?

terdon
  • 104,119
vico
  • 4,717
  • 23
  • 66
  • 89

4 Answers4

21

Update note #1: Systemd is now supported on WSL2. Please see this answer for details on how to enable it. Once Systemd is enabled, the SSH server can now be started automatically with sudo systemctl enable ssh and queried with sudo systemctl status ssh.

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 old init.d style scripts for most services. So sudo service ssh start (or restart, or status, or stop, etc.) is what you'll use.

In addition to enabling SSH, however, you likely want to be able to connect to it from a remote system. WSL2 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.


Update note #2: The method below no longer works on recent WSL releases which are installed from the Microsoft Store. The Store version of WSL has a known limitation that does not allow it to be started from a remote SSH session. Leaving the answer here for now, but I'll update it soon with a different (less optimal, unfortunately, solution).

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 the wsl 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.

NotTheDr01ds
  • 22,082
8

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.

matigo
  • 24,752
  • 7
  • 50
  • 79
0

At first, you need to install OpenSSH-server in the WSL system, it's possible by the next command:

sudo apt-get install openssh-server

Next step is to enable and start the SSH daemon by the next line:

sudo systemctl enable ssh --now

And you will need the WSL internal IP, possible to view it by the following line:

ip addr | grep eth0

All following commands need to run in PowerShell:

1)to enable port forwarding in host Windows system

netsh interface portproxy add v4tov4 `
listenaddress=<windows_local_ip> `
listenport=<listen_port_on_windows> `
connectaddress=<wsl_internal_ip> `
connectport=<ssh_service_port>

2)to add a Firewall rule which allows access to the host system port

netsh advfirewall firewall add rule `
name="wsl_sshd" `
dir=in `
action=allow `
protocol=TCP `
localport=<listen_port_on_windows>

That's it, you can connect to your WSL system ssh server by command line like

ssh <wsl_username>@<windows_ip> -p <listen_port_on_windows>

P.S. In Windows 11, appear some issues with port forwarding and you will need to restart the "IP Helper" service after every reboot of the Windows OS.

PRIHLOP
  • 2,108
  • 16
  • 15
-1

You should enable dbus it's more like port forwarding

#enable dbus
sudo systemctl enable dbus
sudo /etc/init.d/dbus start

if the result is the same as the picture below then don't worry it's not a problem

the picture

and then

sudo service ssh restart
David SHainidze
  • 2,718
  • 2
  • 8
  • 13