diff --git a/pkg/hostagent/hostagent.go b/pkg/hostagent/hostagent.go index 98d0922a0bd..a064f6dcfac 100644 --- a/pkg/hostagent/hostagent.go +++ b/pkg/hostagent/hostagent.go @@ -42,6 +42,7 @@ import ( "github.com/lima-vm/lima/v2/pkg/sshutil" "github.com/lima-vm/lima/v2/pkg/store" "github.com/lima-vm/lima/v2/pkg/store/filenames" + "github.com/lima-vm/lima/v2/pkg/version/versionutil" ) type HostAgent struct { @@ -110,8 +111,16 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt return nil, err } + var limaVersion string + limaVersionFile := filepath.Join(inst.Dir, filenames.LimaVersion) + if b, err := os.ReadFile(limaVersionFile); err == nil { + limaVersion = strings.TrimSpace(string(b)) + } else if !errors.Is(err, os.ErrNotExist) { + logrus.WithError(err).Warnf("Failed to read %q", limaVersionFile) + } + // inst.Config is loaded with FillDefault() already, so no need to care about nil pointers. - sshLocalPort, err := determineSSHLocalPort(*inst.Config.SSH.LocalPort, instName) + sshLocalPort, err := determineSSHLocalPort(*inst.Config.SSH.LocalPort, instName, limaVersion) if err != nil { return nil, err } @@ -237,14 +246,14 @@ func writeSSHConfigFile(sshPath, instName, instDir, instSSHAddress string, sshLo return os.WriteFile(fileName, b.Bytes(), 0o600) } -func determineSSHLocalPort(confLocalPort int, instName string) (int, error) { +func determineSSHLocalPort(confLocalPort int, instName, limaVersion string) (int, error) { if confLocalPort > 0 { return confLocalPort, nil } if confLocalPort < 0 { return 0, fmt.Errorf("invalid ssh local port %d", confLocalPort) } - if instName == "default" { + if versionutil.LessThan(limaVersion, "2.0.0") && instName == "default" { // use hard-coded value for "default" instance, for backward compatibility return 60022, nil } diff --git a/pkg/version/versionutil/versionutil.go b/pkg/version/versionutil/versionutil.go index 36efcebada9..323a8fcba38 100644 --- a/pkg/version/versionutil/versionutil.go +++ b/pkg/version/versionutil/versionutil.go @@ -51,3 +51,8 @@ func GreaterThan(limaVersion, oldVersion string) bool { func GreaterEqual(limaVersion, oldVersion string) bool { return compare(limaVersion, oldVersion) >= 0 } + +// LessThan returns true if limaVersion < oldVersion. +func LessThan(limaVersion, oldVersion string) bool { + return compare(limaVersion, oldVersion) < 0 +} diff --git a/templates/default.yaml b/templates/default.yaml index 8b27aa99374..2aa5c4077d1 100644 --- a/templates/default.yaml +++ b/templates/default.yaml @@ -122,8 +122,6 @@ additionalDisks: ssh: # A localhost port of the host. Forwarded to port 22 of the guest. # 🟢 Builtin default: 0 (automatically assigned to a free port) - # NOTE: when the instance name is "default", the builtin default value is set to - # 60022 for backward compatibility. localPort: null # Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub . # This option is useful when you want to use other SSH-based diff --git a/website/content/en/docs/config/_index.md b/website/content/en/docs/config/_index.md index 99469c1471a..097fca1176b 100644 --- a/website/content/en/docs/config/_index.md +++ b/website/content/en/docs/config/_index.md @@ -11,6 +11,6 @@ The current default spec: - Memory: 4 GiB - Disk: 100 GiB - Mounts: `~` (read-only), `/tmp/lima` (writable) -- SSH: 127.0.0.1:60022 +- SSH: 127.0.0.1: For environment variables, see [Environment Variables](./environment-variables/). diff --git a/website/content/en/docs/faq/_index.md b/website/content/en/docs/faq/_index.md index 824949514dc..0b02ca23289 100644 --- a/website/content/en/docs/faq/_index.md +++ b/website/content/en/docs/faq/_index.md @@ -50,7 +50,12 @@ weight: 6 Password is disabled and locked by default. You have to use `limactl shell bash` (or `lima bash`) to open a shell. -Alternatively, you may also directly ssh into the guest: `ssh -p 60022 -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes 127.0.0.1`. +Alternatively, you may also directly ssh into the guest: `ssh -p -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes 127.0.0.1`. +The port number can be inspected by running `limactl list`. +e.g., +```bash +limactl list --format '{{ .SSHLocalPort }}' default +``` #### "Does Lima work on ARM Mac?" Yes