Skip to content

Use loopback for SSH connection in WSL2 #3299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 2 additions & 19 deletions pkg/store/instance_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package store

import (
"fmt"
"os/exec"
"regexp"
"strings"

Expand All @@ -26,7 +25,7 @@ func inspectStatus(instDir string, inst *Instance, y *limayaml.LimaYAML) {
inst.SSHLocalPort = 22

if inst.Status == StatusRunning {
sshAddr, err := getWslSSHAddress(inst.Name)
sshAddr, err := GetSSHAddress(inst.Name)
if err == nil {
inst.SSHAddress = sshAddr
} else {
Expand Down Expand Up @@ -118,21 +117,5 @@ func GetWslStatus(instName string) (string, error) {
}

func GetSSHAddress(instName string) (string, error) {
return getWslSSHAddress(instName)
}

// GetWslSSHAddress runs a hostname command to get the IP from inside of a wsl2 VM.
//
// Expected output (whitespace preserved, [] for optional):
// PS > wsl -d <distroName> bash -c hostname -I | cut -d' ' -f1
// 168.1.1.1 [10.0.0.1]
func getWslSSHAddress(instName string) (string, error) {
distroName := "lima-" + instName
cmd := exec.Command("wsl.exe", "-d", distroName, "bash", "-c", `hostname -I | cut -d ' ' -f1`)
out, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("failed to get hostname for instance %q, err: %w (out=%q)", instName, err, string(out))
}

return strings.TrimSpace(string(out)), nil
return "127.0.0.1", nil
}
Loading