• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

? SSH into Your Virtual Machine from WSL2: The "localhost" Mystery Solved! ?

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Hey everyone! ? Ever tried to SSH into a VM you've lovingly set up, only to be met with a frustrating "Connection refused" error when doing it from your Windows Subsystem for Linux 2 (WSL2)? ? You're not alone! I recently went down this rabbit hole, following all the usual port forwarding and SSH setup guides, yet still hitting a wall.

Turns out, there's a sneaky little detail often overlooked in these guides when it comes to WSL2. But fear not, fellow developers and tech enthusiasts! In this post, I'll walk you through the standard SSH setup and then reveal the secret sauce to finally SSHing into your VM from your WSL2 environment. Let's dive in! ?

Setting the Stage: SSH Setup and Port Forwarding ⚙


Before we unravel the mystery, let's ensure SSH is properly configured on both your VM and WSL2, and that port forwarding is in place.

Step 1: Install OpenSSH on Both the VM and WSL2

On your Virtual Machine (Ubuntu/Debian Example):


Open your VM's terminal and get those packages updated:


sudo apt update

Install the OpenSSH server:


sudo apt install openssh-server -y


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Start and enable the SSH service to run on boot:


sudo systemctl start ssh
sudo systemctl enable ssh

(Optional) Verify the SSH service is running smoothly:


sudo systemctl status ssh

You should see an active status in the output. ✅


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



On your WSL2 Environment (Ubuntu/Similar):

Open your WSL2 terminal.

Install the OpenSSH client if it's not already there (it often is):


sudo apt update
sudo apt install openssh-client

Test if SSH is available:


ssh -V


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



You should see the OpenSSH version printed like the one in above image?.

Step 2: Setup Port Forwarding (Host to VM)


If your VM is running in a NAT network (common in VirtualBox, VMware, Hyper-V), you need to tell your host machine (Windows) to forward connections on a specific port to your VM. Here's how to do it in VirtualBox:

  1. Open VirtualBox.
  2. Go to your VM's Settings > Network > Adapter 1 > Advanced > Port Forwarding.
    • Click the "+" icon to add a new rule:
  3. Fill the row as below:
    • Name: SSH (or any name you prefer)
    • Protocol: TCP
    • Host IP: Leave it blank
    • Host Port: 2222 (or any unused port on your Windows machine)
    • Guest IP: Leave blank (or the VM's internal IP)
    • Guest Port: 22


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.




Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



This rule tells VirtualBox to listen for incoming TCP connections on port 2222 of your Windows machine and forward them to port 22 of your VM.

Step 3: Ensure the VM's Firewall Allows SSH (Port 22)

  1. For Ubuntu/Debian (using ufw):

Check the firewall status:


sudo ufw status


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



If SSH isn't listed as allowed, enable it:


sudo ufw allow ssh

Alternatively, you can specify the port:


sudo ufw allow 22/tcp

Reload the firewall rules if needed:


sudo ufw reload
  1. Check sshd_config on the VM:

This file dictates how your SSH server behaves. Open it with


sudo nano /etc/ssh/sshd_config

Look for these lines and ensure they are (or are set to):


Port 22
ListenAddress 0.0.0.0
PasswordAuthentication yes

ListenAddress 0.0.0.0 is crucial for NAT and port forwarding as it makes the SSH server listen on all network interfaces.

Save (Ctrl+O, Enter) and exit (Ctrl+X) the file, then restart the SSH service:


sudo systemctl restart ssh
3. Confirm SSH is Listening on the Correct Port:


Run this command on your VM:


sudo ss -tuln | grep :22

This command uses ss (Socket Statistics) to display listening TCP (-t) and UDP (-u) sockets, showing only listening sockets (-l) with numeric addresses and ports (-n), and then filters the output for port :22. You should see a line indicating that sshd is listening on port 22. ?


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



So the above command is used to check if a service (like SSH) is listening on port 22 (the default SSH port)

The Moment of Truth (and the Unexpected Error!) ?


Now, let's try to SSH into your VM from WSL2 using the port we forwarded:


ssh user@localhost -p 2222

(Remember to replace user with your actual VM username!)


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


... And you might just get a "Connection refused" error! ? What went wrong? We followed all the steps, right?

The "localhost" Conundrum: The Heart of the Issue ?


Let's try SSHing into the VM from your Windows Command Prompt (CMD):


ssh user@localhost -p 2222

? It works! You're successfully SSHed into your VM.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



So, what's the difference between CMD and WSL2 when it comes to localhost?

Understanding "localhost"


The term "localhost" always refers to the current machine where you're running the command. However, the definition of "current machine" differs between Windows and WSL2.

Why it Works in CMD (Windows Terminal)


When you run ssh user@localhost -p 2222 in CMD, "localhost" refers to your Windows host machine. Since you've set up port forwarding in your virtualization software, Windows is listening on port 2222 and correctly forwarding those connections to port 22 of your VM. ?

Why it Doesn't Work in WSL2


When you run the same command in WSL2, "localhost" now refers to the WSL2 instance itself. WSL2 is essentially a lightweight virtual machine running Linux. It has its own network environment and its own localhost. There's likely nothing listening on port 2222 within your WSL2 environment, hence the "Connection refused" error. ?

? The Fix: Connecting via the Windows Host IP! ?


The solution is to tell WSL2 to connect to the Windows host machine's IP address, where the port forwarding is active.

Finding the Windows Host IP from WSL2:

Open your WSL2 terminal and run:


cat /etc/resolv.conf | grep nameserver

You'll likely see an output similar to:


nameserver 172.X.X.X

This nameserver IP address is the internal IP address of your Windows host machine as seen by WSL2.

Finally SSHing In!


Now, use this IP address in your SSH command from WSL2:


ssh -p 2222 user@172.X.X.X

(Replace user with your VM username and 172.X.X.X with the actual IP you found.)

? Congratulations! You should now be successfully logged into your VM's SSH session from your WSL2 environment! ?

? Tip: Also make sure you have disabled the Windows Firewall or added a specific inbound rule in the Windows Firewall to ensure that you can SSH into your VM via WSL2.

Conclusion: Conquer the "localhost"! ?


The "localhost" behavior in WSL2 can be a bit of a head-scratcher when you're used to how it works on the host machine. By understanding that WSL2 has its own network environment and using the Windows host's IP address, you can seamlessly connect to your VMs via SSH.

I hope this detailed explanation helps you overcome this common hurdle and streamlines your development workflow! Happy SSHing! ?


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу