Skip to content

Commit d1b3766

Browse files
authored
Merge pull request #3780 from AkihiroSuda/remove-hardcoded-60022
Stop using hard-coded port 60022 for "default" instance
2 parents 6afea79 + 9c4132a commit d1b3766

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

pkg/hostagent/hostagent.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/lima-vm/lima/v2/pkg/sshutil"
4343
"github.com/lima-vm/lima/v2/pkg/store"
4444
"github.com/lima-vm/lima/v2/pkg/store/filenames"
45+
"github.com/lima-vm/lima/v2/pkg/version/versionutil"
4546
)
4647

4748
type HostAgent struct {
@@ -110,8 +111,16 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
110111
return nil, err
111112
}
112113

114+
var limaVersion string
115+
limaVersionFile := filepath.Join(inst.Dir, filenames.LimaVersion)
116+
if b, err := os.ReadFile(limaVersionFile); err == nil {
117+
limaVersion = strings.TrimSpace(string(b))
118+
} else if !errors.Is(err, os.ErrNotExist) {
119+
logrus.WithError(err).Warnf("Failed to read %q", limaVersionFile)
120+
}
121+
113122
// inst.Config is loaded with FillDefault() already, so no need to care about nil pointers.
114-
sshLocalPort, err := determineSSHLocalPort(*inst.Config.SSH.LocalPort, instName)
123+
sshLocalPort, err := determineSSHLocalPort(*inst.Config.SSH.LocalPort, instName, limaVersion)
115124
if err != nil {
116125
return nil, err
117126
}
@@ -237,14 +246,14 @@ func writeSSHConfigFile(sshPath, instName, instDir, instSSHAddress string, sshLo
237246
return os.WriteFile(fileName, b.Bytes(), 0o600)
238247
}
239248

240-
func determineSSHLocalPort(confLocalPort int, instName string) (int, error) {
249+
func determineSSHLocalPort(confLocalPort int, instName, limaVersion string) (int, error) {
241250
if confLocalPort > 0 {
242251
return confLocalPort, nil
243252
}
244253
if confLocalPort < 0 {
245254
return 0, fmt.Errorf("invalid ssh local port %d", confLocalPort)
246255
}
247-
if instName == "default" {
256+
if versionutil.LessThan(limaVersion, "2.0.0") && instName == "default" {
248257
// use hard-coded value for "default" instance, for backward compatibility
249258
return 60022, nil
250259
}

pkg/version/versionutil/versionutil.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ func GreaterThan(limaVersion, oldVersion string) bool {
5656
func GreaterEqual(limaVersion, oldVersion string) bool {
5757
return compare(limaVersion, oldVersion) >= 0
5858
}
59+
60+
// LessThan returns true if limaVersion < oldVersion.
61+
func LessThan(limaVersion, oldVersion string) bool {
62+
return !GreaterEqual(limaVersion, oldVersion)
63+
}

templates/default.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ additionalDisks:
122122
ssh:
123123
# A localhost port of the host. Forwarded to port 22 of the guest.
124124
# 🟢 Builtin default: 0 (automatically assigned to a free port)
125-
# NOTE: when the instance name is "default", the builtin default value is set to
126-
# 60022 for backward compatibility.
127125
localPort: null
128126
# Load ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub .
129127
# This option is useful when you want to use other SSH-based

website/content/en/docs/config/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ The current default spec:
1111
- Memory: 4 GiB
1212
- Disk: 100 GiB
1313
- Mounts: `~` (read-only), `/tmp/lima` (writable)
14-
- SSH: 127.0.0.1:60022
14+
- SSH: 127.0.0.1:<Random port>
1515

1616
For environment variables, see [Environment Variables](./environment-variables/).

website/content/en/docs/faq/_index.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ weight: 6
5050
Password is disabled and locked by default.
5151
You have to use `limactl shell bash` (or `lima bash`) to open a shell.
5252

53-
Alternatively, you may also directly ssh into the guest: `ssh -p 60022 -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes 127.0.0.1`.
53+
Alternatively, you may also directly ssh into the guest: `ssh -p <PORT> -i ~/.lima/_config/user -o NoHostAuthenticationForLocalhost=yes 127.0.0.1`.
54+
The port number can be inspected by running `limactl list`.
55+
e.g.,
56+
```bash
57+
limactl list --format '{{ .SSHLocalPort }}' default
58+
```
5459

5560
#### "Does Lima work on ARM Mac?"
5661
Yes

0 commit comments

Comments
 (0)