VirtualBox: figure out host IP from NAT mode Linux guest

2

I need to SSH from guest VM (Linux/Debian 9) to its host running VirtualBox on a Windows 10 machine. Network configuration: NAT.

Which Linux command can I use to figure out the IP of the host from within the guest?

user1876484

Posted 2018-04-11T14:03:26.127

Reputation: 123

Answers

2

Show VirtualBox host's internal IP address

$ ip route show default
default via 10.0.2.2 dev eth0 proto static
10.0.2.0/24 dev etho proto kernel scope link src 10.0.2.15

In the above, 10.0.2.2 is the address of the host which is routing packets from the guest's IP (10.0.2.15 above for reference)

If you prefer getting the IP itself, you could use grep and cut (or just awk), eg:

$ ip route show default | grep default | cut -d ' ' -f3
10.0.2.2

bertieb

Posted 2018-04-11T14:03:26.127

Reputation: 6 181

Isn't 10.0.2.2 just the ip-address used by the host for the host-side of the virtual network between hosts and guest(s) in stead of the real network facing address of the host ? Would the SSH service of the host be listening to that ip for incoming requests ? That may be highly dependent of the specific OS and software used on the host . – Tonny – 2018-04-11T14:20:30.643

@Tonny - host OS - Windows 10. SSH server - either the built-in SSH server, or built-in OpenSSH server. Ideally it should work for both. Thank you! – user1876484 – 2018-04-11T14:27:14.170

@Tonny Depends on setup (ie a specific ListenAddress is configured) as you say. Presumably OP is able to configure host, but needs access from guest. – bertieb – 2018-04-11T14:28:57.720

@bertieb, what happens in the case of default SSH configuration, when no specific ListenAddress was specified? – user1876484 – 2018-04-11T14:33:49.913

1@user1876484 That is my situation, and I am able to connect from guest to host using the internal IP address. Might vary depending on sshd flavour though. – bertieb – 2018-04-11T14:34:50.400

@bertrieb If he can (re-)configure the host he can lookup the ip-address/hostname himself, can't he ? But if the guest is e.g. a Docker image deployed to multiple hosts that might not be an option if you need something in teh Docker image that works all the time regardless of which host is used. – Tonny – 2018-04-11T14:35:03.307

2@user1876484 I just tested this with a debian guest and a Win10 host (using RDP as the test-conenction) and that works, because Windows 10 by default binds the RDP service to all ip-addresses. I don't have a SSH-server around on Windows 10 at the moment to test with that. As long as the SSH service binds to * it should work (and I guess that the Microsoft supplied one will bind to all ip-addresses.) – Tonny – 2018-04-11T14:36:49.393

@Tonny Yes but automatic beats manual lookup every time :P AFAICT the 10.0.2.2 host 10.0.2.15 guest seems to be default for NAT mode. I also had a look at shared properties (VBoxControl guestproperty enumerate) and the host IP isn't there- it could be set from the host side -- again, configuration -- and shared that way. – bertieb – 2018-04-11T14:37:42.543

@bertieb Those ip-addresses are the same on my setup. Host is normally 10.0.2..2. But of course you can change this when playing around with the settings. – Tonny – 2018-04-11T14:39:08.573

(Determining a 'public' IP address is problematic)

– bertieb – 2018-04-11T14:39:09.590

@Tonny, Windows 10 has a built-in SSH server and also OpenSSH one (see there). Would be nice if you could confirm it working also with SSH.

– user1876484 – 2018-04-11T14:40:05.027

1

For sake of completeness: there is an API available which might expose a host IP; but I can't downlaoded and read through the SDK to find out. Going way beyond a quick determination but might be handy for those who need that.

– bertieb – 2018-04-11T14:42:36.420

@user1876484 I know that, but I'm working in a corporate environment. Can't put my Windows 10 box in Developer mode or install OpenSHH here, so I can't test. – Tonny – 2018-04-11T14:43:28.840