Guide to Setting Up OpenSSH on WSL

Aug 19, 2024

Setting Up OpenSSH on Ubuntu WSL

Introduction

  • Guide for setting up and using OpenSSH as a server on Ubuntu WSL (also applicable to other Debian-based WSL OS).
  • SSH allows remote terminal access to control WSL virtual machines.

Initial Setup

  1. Open Ubuntu WSL
    • Start a terminal session.
  2. Install OpenSSH Server
    • Run: sudo apt install openssh-server
    • Enter password and confirm installation.
  3. Edit SSH Configuration
    • Command: sudo nano /etc/ssh/sshd_config
    • Change default port (optional, e.g., to 2000).
    • Save changes (Ctrl + S) and exit (Ctrl + X).

Enabling SSH Server on Startup

  • Enable SSH to start automatically when Ubuntu WSL starts:
    • Run: sudo systemctl enable ssh
    • Check status: sudo systemctl status ssh.

Configure WSL for SystemD

  1. Edit WSL Configuration
    • Command: sudo nano /etc/wsl.conf
    • Add:
      [boot]  
      systemd=true  
      
    • Save and exit.
  2. Restart WSL
    • Command: wsl --shutdown to close.
    • Restart Ubuntu WSL.

Firewall and Port Configuration

  • If accessing externally, consider using port 2222.
  • Enable firewall rule (command provided in video description).
  • Changing the default port (if desired):
    • Edit: sudo nano /etc/ssh/sshd_config
    • Note: Port changes ignored in Ubuntu 22.10 and above.
  • Workaround for changing port:
    • Create config file: sudo nano /etc/systemd/system/ssh.socket.d/listen.conf.
    • Reload daemon: sudo systemctl daemon-reload.
    • Restart SSH: sudo systemctl restart ssh.
    • Verify port: sudo systemctl status ssh.

Connecting to SSH

  • Use Visual Studio Code with Remote SSH Plugin:
    • Configure localhosts and add new option for Ubuntu WSL.
    • Connect using the password.
  • Verify connection by running neofetch for system info.

Conclusion

  • Final steps include running firewall commands to allow access from outside the computer.
  • Thank you for watching!