@@ -137,7 +137,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
137137 }
138138
139139 // inst.Config is loaded with FillDefault() already, so no need to care about nil pointers.
140- sshLocalPort , err := determineSSHLocalPort (* inst .Config .SSH .LocalPort , instName , limaVersion )
140+ sshLocalPort , err := determineSSHLocalPort (* inst .Config .SSH .Address , * inst . Config . SSH . LocalPort , instName , limaVersion )
141141 if err != nil {
142142 return nil , err
143143 }
@@ -189,6 +189,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
189189 inst .Dir ,
190190 * inst .Config .User .Name ,
191191 * inst .Config .SSH .LoadDotSSHPubKeys ,
192+ * inst .Config .SSH .Address ,
192193 * inst .Config .SSH .ForwardAgent ,
193194 * inst .Config .SSH .ForwardX11 ,
194195 * inst .Config .SSH .ForwardX11Trusted )
@@ -244,7 +245,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
244245 instName : instName ,
245246 instSSHAddress : inst .SSHAddress ,
246247 sshConfig : sshConfig ,
247- portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , ignoreTCP , inst .VMType ),
248+ portForwarder : newPortForwarder (sshConfig , inst . SSHAddress , sshLocalPort , rules , ignoreTCP , inst .VMType ),
248249 grpcPortForwarder : portfwd .NewPortForwarder (rules , ignoreTCP , ignoreUDP ),
249250 driver : limaDriver ,
250251 signalCh : signalCh ,
@@ -290,13 +291,16 @@ func writeSSHConfigFile(sshPath, instName, instDir, instSSHAddress string, sshLo
290291 return os .WriteFile (fileName , b .Bytes (), 0o600 )
291292}
292293
293- func determineSSHLocalPort (confLocalPort int , instName , limaVersion string ) (int , error ) {
294+ func determineSSHLocalPort (confSSHAddress string , confLocalPort int , instName , limaVersion string ) (int , error ) {
294295 if confLocalPort > 0 {
295296 return confLocalPort , nil
296297 }
297298 if confLocalPort < 0 {
298299 return 0 , fmt .Errorf ("invalid ssh local port %d" , confLocalPort )
299300 }
301+ if confLocalPort == 0 && confSSHAddress != "127.0.0.1" {
302+ return 22 , nil
303+ }
300304 if versionutil .LessThan (limaVersion , "2.0.0" ) && instName == "default" {
301305 // use hard-coded value for "default" instance, for backward compatibility
302306 return 60022 , nil
@@ -445,8 +449,22 @@ func (a *HostAgent) Run(ctx context.Context) error {
445449 return a .startRoutinesAndWait (ctx , errCh )
446450}
447451
452+ func getIP (address string ) string {
453+ ip := net .ParseIP (address )
454+ if ip != nil {
455+ return address
456+ }
457+ ctx := context .Background ()
458+ ips , err := net .DefaultResolver .LookupIP (ctx , "ip" , address )
459+ if err == nil && len (ips ) > 0 {
460+ return ips [0 ].String ()
461+ }
462+ return address
463+ }
464+
448465func (a * HostAgent ) startRoutinesAndWait (ctx context.Context , errCh <- chan error ) error {
449466 stBase := events.Status {
467+ SSHIPAddress : getIP (a .instSSHAddress ),
450468 SSHLocalPort : a .sshLocalPort ,
451469 }
452470 stBooting := stBase
@@ -639,7 +657,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
639657 for _ , rule := range a .instConfig .PortForwards {
640658 if rule .GuestSocket != "" {
641659 local := hostAddress (rule , & guestagentapi.IPPort {})
642- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
660+ _ = forwardSSH (ctx , a .sshConfig , a .instSSHAddress , a . sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
643661 }
644662 }
645663 }
@@ -654,13 +672,13 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
654672 if rule .GuestSocket != "" {
655673 local := hostAddress (rule , & guestagentapi.IPPort {})
656674 // using ctx.Background() because ctx has already been cancelled
657- if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
675+ if err := forwardSSH (context .Background (), a .sshConfig , a .instSSHAddress , a . sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
658676 errs = append (errs , err )
659677 }
660678 }
661679 }
662680 if a .driver .ForwardGuestAgent () {
663- if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
681+ if err := forwardSSH (context .Background (), a .sshConfig , a .instSSHAddress , a . sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
664682 errs = append (errs , err )
665683 }
666684 }
@@ -671,7 +689,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
671689 if a .instConfig .MountInotify != nil && * a .instConfig .MountInotify {
672690 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
673691 if a .driver .ForwardGuestAgent () {
674- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
692+ _ = forwardSSH (ctx , a .sshConfig , a .instSSHAddress , a . sshLocalPort , localUnix , remoteUnix , verbForward , false )
675693 }
676694 }
677695 err := a .startInotify (ctx )
@@ -687,7 +705,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
687705 for {
688706 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
689707 if a .driver .ForwardGuestAgent () {
690- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
708+ _ = forwardSSH (ctx , a .sshConfig , a .instSSHAddress , a . sshLocalPort , localUnix , remoteUnix , verbForward , false )
691709 }
692710 }
693711 client , err := a .getOrCreateClient (ctx )
@@ -721,7 +739,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
721739 local , remote := a .portForwarder .forwardingAddresses (guest )
722740 if local != "" {
723741 logrus .Infof ("Setting up static TCP forwarding from %s to %s" , remote , local )
724- if err := forwardTCP (ctx , a .sshConfig , a .sshLocalPort , local , remote , verbForward ); err != nil {
742+ if err := forwardTCP (ctx , a .sshConfig , a .instSSHAddress , a . sshLocalPort , local , remote , verbForward ); err != nil {
725743 logrus .WithError (err ).Warnf ("failed to set up static TCP forwarding %s -> %s" , remote , local )
726744 }
727745 }
@@ -831,11 +849,11 @@ const (
831849 verbCancel = "cancel"
832850)
833851
834- func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
852+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , addr string , port int , command ... string ) error {
835853 args := sshConfig .Args ()
836854 args = append (args ,
837855 "-p" , strconv .Itoa (port ),
838- "127.0.0.1" ,
856+ addr ,
839857 "--" ,
840858 )
841859 args = append (args , command ... )
@@ -846,7 +864,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
846864 return nil
847865}
848866
849- func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote , verb string , reverse bool ) error {
867+ func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , addr string , port int , local , remote , verb string , reverse bool ) error {
850868 args := sshConfig .Args ()
851869 args = append (args ,
852870 "-T" ,
@@ -865,15 +883,15 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
865883 "-N" ,
866884 "-f" ,
867885 "-p" , strconv .Itoa (port ),
868- "127.0.0.1" ,
886+ addr ,
869887 "--" ,
870888 )
871889 if strings .HasPrefix (local , "/" ) {
872890 switch verb {
873891 case verbForward :
874892 if reverse {
875893 logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
876- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
894+ if err := executeSSH (ctx , sshConfig , addr , port , "rm" , "-f" , remote ); err != nil {
877895 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
878896 }
879897 } else {
@@ -888,7 +906,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
888906 case verbCancel :
889907 if reverse {
890908 logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
891- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
909+ if err := executeSSH (ctx , sshConfig , addr , port , "rm" , "-f" , remote ); err != nil {
892910 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
893911 }
894912 } else {
@@ -909,7 +927,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
909927 if verb == verbForward && strings .HasPrefix (local , "/" ) {
910928 if reverse {
911929 logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
912- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
930+ if err := executeSSH (ctx , sshConfig , addr , port , "rm" , "-f" , remote ); err != nil {
913931 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
914932 }
915933 } else {
0 commit comments