2

Is there a way to make the WSL2 ip static? I read about it and I was told there was no way to make the IP static, then is there a way to make a SecureCRT connection to WSL2 without having to change the IP adddress in the configs so I don't have to re-enter the ip every time?

5
  • WSL2 has no IP, the host Windows OS does. Aug 2, 2021 at 17:48
  • 1
    WSL2 has an IP since I can do ifconfig and get an IP address inside the Ubuntu shell.
    – Sayaman
    Aug 2, 2021 at 18:05
  • Yes, you surely can and that one is the same as the Windows' IP. If you can make the host IP static it will be static as well in the WSL2 environment. Aug 2, 2021 at 18:10
  • 1
    I want to ssh from the host, so the IP I have to enter is different since it's using an internal IP address.
    – Sayaman
    Aug 2, 2021 at 18:12
  • 1
    @ChanganAuto I'm sorry, but that's just not correct. WSL2 uses a virtual NIC provided by a subset of Hyper-V, with its own IP that is completely different than the Windows host. It is NAT'd behind a virtual router also provided by Hyper-V (and with yet another separate address). You cannot use the Windows host's IP to connect to WSL2. You may be thinking of WSL1, where its pseudo-bridged network does work like that. Aug 2, 2021 at 20:00

2 Answers 2

0

The solution to not redo again everything with a new IP address is found in the post
WSL 2 NIC Bridge mode #4150.

Unfortunately, the solution is only to relegate the work to a script that is run automatically after each login.

Description of the problem:

The work around is to forward the TCP ports of WSL 2 services to the host OS.
The virtual adapter on WSL 2 machine changes its IP address during reboot which makes it tough to implement a run once solution.
Also a side note, windows firewall will block the redirected port.

The solution is to write a PowerShell script that does :

  • Get Ip Address of WSL 2 machine
  • Remove previous port forwarding rules
  • Add port Forwarding rules
  • Remove previously added firewall rules
  • Add new Firewall Rules

The script also removes unwanted firewall rules.
Here is the script as copied from that post:

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}

You need to schedule the script to run after login, as follows:

Go to search, search for task scheduler. In the actions menu on the right, click on create task.
Enter Name, go to triggers tab. Create a new trigger, with a begin task as you login, set delay to 10s.
Go to the actions and add the script. If you are using Laptop, go to settings and enable run on power.

6
  • Do you have to add the port 22 if you want to log via ssh using the ip 0.0.0.0?
    – Sayaman
    Aug 4, 2021 at 21:07
  • 1
    Yes, add the port to the ports list.
    – harrymc
    Aug 5, 2021 at 5:58
  • It's not working though.
    – Sayaman
    Aug 5, 2021 at 14:45
  • Do I need to make any change? It seems to connect to an outside ip when I enter 155.12.12.12 instead of connecting to 155.12.12.12 in the local network.
    – Sayaman
    Aug 5, 2021 at 20:15
  • It doesn't work. I did: ip addr show eth0 | awk '$1 == "inet" {gsub(/\/.*$/, "", $2); print $2}' and I get: 172.14.112.11
    – Sayaman
    Aug 5, 2021 at 21:03
0

If you just need an ssh connection/protocol for SecureCRT, which you indicated in the comments, then there's an alternative to port forwarding:

  • Install Windows OpenSSH server (directions running on Port 22.
  • Set your WSL2 sshd port to something else (e.g. 2222) in /etc/ssh/sshd_config
  • Use the Windows host as a jumphost, taking advantage of WSL2's localhost port-forwarding. I.e. ssh -o "ProxyCommand ssh -W %h:%p windowshost.local" -o "StrictHostKeyChecking=no -p 2222 localhost (substitute "windowshost" with your machine name).

This works because, by default, Windows will detect services running in WSL2 instances and forward localhost connections to them.

This assumes, of course, that SecureCRT can use a ProxyCommand.

1
  • here is information on ProxyCommand.
    – Ramhound
    Aug 2, 2021 at 21:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .