Logo

In this section

  1. SSH management
  2. Setup SSH key-based authentication

1. SSH management

OpenSSH server is the most commonly used SSH server on Linux systems.

Install SSH and check status

sudo apt install ssh -y 
sudo apt install openssh-server
sudo systemctl status ssh 

Expected status: inactive (dead)
By default, WSL2 does not support systemd. Alternative is the service command

sudo service ssh status  
sudo service ssh start
sudo service ssh enable

Start, restart and enable SSH to start on boot

sudo systemctl start ssh
sudo systemctl restart ssh
sudo systemctl enable ssh 

This establishes a remote connection to the SSH server running inside WSL2, which requires that the WSL2 instance is already running and that the SSH server is active.

Check if service installed /if running

dpkg -l | grep openssh-server

Check if the SSH daemon (sshd) is present on the system
which sshd

Check if SSH is listening on the port 22 (default)

sudo ss -tulpn | grep ssh

SSH Acces from PowerShell

2. Setup SSH key-based authentication

Setting up SSH key-based authentication is a secure and convenient way to connect to your WSL instance without entering your password every time. This is useful for task automation by running scripts.

2.1. Generate an SSH Key Pair on Your Windows Machine

    ssh-keygen -t rsa -b 4096

Accepting defaults in home dir (~/.ssh/) 2 files wwill be created:

2.2. Create hidden dir .ssh on WSL home

    mkdir .ssh

2.3. Save id_rsa.pub as authorized_keys and copy to hidden dir .ssh on Linux

2.4. Test

ssh barry75@172.25.185.251 -p 22
scp -r -P 22 C:/source barry75@barryonweb.com:/home/barry75