Skip to content

Commit 32e6420

Browse files
committed
Add SSH address for supplying external IP
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
1 parent 79cfe42 commit 32e6420

File tree

8 files changed

+35
-3
lines changed

8 files changed

+35
-3
lines changed

cmd/limactl/show_ssh.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9595
if err != nil {
9696
return err
9797
}
98-
opts = append(opts, "Hostname=127.0.0.1")
98+
opts = append(opts, fmt.Sprintf("Hostname=%s", inst.SSHAddress))
9999
opts = append(opts, fmt.Sprintf("Port=%d", inst.SSHLocalPort))
100100
return sshutil.Format(w, instName, format, opts)
101101
}

examples/default.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ additionalDisks:
117117
# fsType: "ext4"
118118

119119
ssh:
120+
# Address for the host.
121+
# 🟢 Builtin default: "127.0.0.1" (localhost)
122+
address: null
120123
# A localhost port of the host. Forwarded to port 22 of the guest.
121124
# 🟢 Builtin default: 0 (automatically assigned to a free port)
122125
# NOTE: when the instance name is "default", the builtin default value is set to

pkg/hostagent/hostagent.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ func determineSSHLocalPort(y *limayaml.LimaYAML, instName string) (int, error) {
207207
if *y.SSH.LocalPort < 0 {
208208
return 0, fmt.Errorf("invalid ssh local port %d", y.SSH.LocalPort)
209209
}
210+
if *y.SSH.LocalPort == 0 && *y.SSH.Address != "127.0.0.1" {
211+
return 22, nil
212+
}
210213
switch instName {
211214
case "default":
212215
// use hard-coded value for "default" instance, for backward compatibility

pkg/limayaml/defaults.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,16 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
283283
y.Firmware.LegacyBIOS = ptr.Of(false)
284284
}
285285

286+
if y.SSH.Address == nil {
287+
y.SSH.Address = d.SSH.Address
288+
}
289+
if o.SSH.Address != nil {
290+
y.SSH.Address = o.SSH.Address
291+
}
292+
if y.SSH.Address == nil {
293+
y.SSH.Address = ptr.Of("127.0.0.1")
294+
}
295+
286296
if y.SSH.LocalPort == nil {
287297
y.SSH.LocalPort = d.SSH.LocalPort
288298
}

pkg/limayaml/defaults_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func TestFillDefault(t *testing.T) {
7979
Archives: defaultContainerdArchives(),
8080
},
8181
SSH: SSH{
82+
Address: ptr.Of("127.0.0.1"),
8283
LocalPort: ptr.Of(0),
8384
LoadDotSSHPubKeys: ptr.Of(true),
8485
ForwardAgent: ptr.Of(false),
@@ -291,6 +292,7 @@ func TestFillDefault(t *testing.T) {
291292
},
292293
},
293294
SSH: SSH{
295+
Address: ptr.Of("0.0.0.0"),
294296
LocalPort: ptr.Of(888),
295297
LoadDotSSHPubKeys: ptr.Of(false),
296298
ForwardAgent: ptr.Of(true),
@@ -474,6 +476,7 @@ func TestFillDefault(t *testing.T) {
474476
},
475477
},
476478
SSH: SSH{
479+
Address: ptr.Of("127.0.1.1"),
477480
LocalPort: ptr.Of(4433),
478481
LoadDotSSHPubKeys: ptr.Of(true),
479482
ForwardAgent: ptr.Of(true),

pkg/limayaml/limayaml.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ type Virtiofs struct {
129129
}
130130

131131
type SSH struct {
132-
LocalPort *int `yaml:"localPort,omitempty" json:"localPort,omitempty"`
132+
Address *string `yaml:"address,omitempty" json:"address,omitempty"`
133+
LocalPort *int `yaml:"localPort,omitempty" json:"localPort,omitempty"`
133134

134135
// LoadDotSSHPubKeys loads ~/.ssh/*.pub in addition to $LIMA_HOME/_config/user.pub .
135136
LoadDotSSHPubKeys *bool `yaml:"loadDotSSHPubKeys,omitempty" json:"loadDotSSHPubKeys,omitempty"` // default: true

pkg/limayaml/validate.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ func Validate(y LimaYAML, warn bool) error {
153153
}
154154
}
155155

156+
if y.SSH.Address != nil {
157+
if err := validateHost("ssh.address", *y.SSH.Address); err != nil {
158+
return err
159+
}
160+
}
156161
if *y.SSH.LocalPort != 0 {
157162
if err := validatePort("ssh.localPort", *y.SSH.LocalPort); err != nil {
158163
return err
@@ -438,6 +443,13 @@ func validateNetwork(y LimaYAML, warn bool) error {
438443
return nil
439444
}
440445

446+
func validateHost(field string, host string) error {
447+
if net.ParseIP(host) == nil {
448+
return fmt.Errorf("field `%s` must be IP", field)
449+
}
450+
return nil
451+
}
452+
441453
func validatePort(field string, port int) error {
442454
switch {
443455
case port < 0:

pkg/store/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Inspect(instName string) (*Instance, error) {
9393
inst.Arch = *y.Arch
9494
inst.VMType = *y.VMType
9595
inst.CPUType = y.CPUType[*y.Arch]
96-
inst.SSHAddress = "127.0.0.1"
96+
inst.SSHAddress = *y.SSH.Address
9797
inst.SSHLocalPort = *y.SSH.LocalPort // maybe 0
9898
inst.SSHConfigFile = filepath.Join(instDir, filenames.SSHConfig)
9999
inst.HostAgentPID, err = ReadPIDFile(filepath.Join(instDir, filenames.HostAgentPID))

0 commit comments

Comments
 (0)