Skip to content

Stop using hard-coded port 60022 for "default" instance #3780

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/version/versionutil/versionutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions templates/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion website/content/en/docs/config/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<Random port>

For environment variables, see [Environment Variables](./environment-variables/).
7 changes: 6 additions & 1 deletion website/content/en/docs/faq/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <PORT> -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
Expand Down