Make IP address of WSL2 static
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping
--
Chapters
00:00 Make Ip Address Of Wsl2 Static
00:45 Accepted Answer Score 28
01:24 Answer 2 Score 2
02:05 Answer 3 Score 4
02:27 Answer 4 Score 11
02:51 Thank you
--
Full question
https://superuser.com/questions/1582234/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#windows10 #ssh #windowssubsystemforlinux #netsh
#avk47
ACCEPTED ANSWER
Score 28
The IP address of a WSL2 machine cannot be made static, however it can be determined using wsl hostname -I
Based on this I was able to create the following powershell script that will start sshd on my WSL machine and route traffic to it.
wsl.exe sudo /etc/init.d/ssh start
$wsl_ip = (wsl hostname -I).trim()
Write-Host "WSL Machine IP: ""$wsl_ip"""
netsh interface portproxy add v4tov4 listenport=22 connectport=22 connectaddress=$wsl_ip
I added the following to my sudoers file via visudo to avoid needing a password to start sshd
%sudo ALL=(ALL) NOPASSWD: /etc/init.d/ssh
Finally, from an administrative powershell terminal, I scheduled my script to run at startup
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:15
Register-ScheduledJob -Trigger $trigger -FilePath C:\route_ssh_to_wsl.ps1 -Name RouteSSHtoWSL
ANSWER 2
Score 11
This solution helped me to set up a static ip of my wsl, try:
Run this on your windows host machine:
netsh interface ip add address "vEthernet (WSL)" 192.168.99.1 255.255.255.0
And this on your wsl linux machine:
sudo ip addr add 192.168.99.2/24 broadcast 192.168.99.255 dev eth0 label eth0:1;
But to keep this IP after the rebooting your sytem you need to set up those commands in the startup scrip.
ANSWER 3
Score 4
No need to use scripts to get the ip, just use Openssh server for windows and change the default shell from c:/system32/cmd.exe to c:/system32/bash.exe:
https://docs.microsoft.com/en-US/windows-server/administration/openssh/openssh_server_configuration
ANSWER 4
Score 2
Using wsl and wsl2 at the same time caused problems for me. Could not get correct wsl hostname from the powershell command:
wsl hostname -I
Building on the answer from Nick, I needed to forward web 80 and 443, along with some other app ports.
ubuntu2004.exe -c "sudo /etc/init.d/ssh start"
$wsl_ip = (ubuntu2004.exe -c "ifconfig eth0 | grep 'inet '").trim().split()| where {$_}
$regex = [regex] "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
$ip_array = $regex.Matches($wsl_ip) | %{ $_.value }
$wsl_ip = $ip_array[0]
Write-Host "WSL Machine IP: ""$wsl_ip"""
netsh interface portproxy add v4tov4 listenport=443 listenaddress=0.0.0.0 connectport=443 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=8080 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=3001 listenaddress=0.0.0.0 connectport=3001 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2222 connectaddress=$wsl_ip
netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=$wsl_ip