How to get IP address assigned to VM running in background?

27

8

I run my Ubuntu instance out of Virtual Box in the background using the following command:

VBoxManage startvm ${VM_NAME} --type headless

The main difference between that and running it through the GUI is that, when I am in the GUI after it starts, I can check ifconfig to see what the IP address is so that I can ssh into it from my terminal on the host and obviously can't do that in the background.

Assuming a dynamic assignment of IP addresses on the network, is there a way to extract the assigned IP to the newly created instance for ssh purposes OR is there a way to ssh into it without knowing the IP (e.g. via a backdoor port on the host)?

amphibient

Posted 2013-08-20T13:50:11.620

Reputation: 1 613

Answers

16

You can use the VBoxManage command to extract the IP address, as shown in this forum post on virtualbox.org:

VBoxManage guestproperty enumerate <vmname>

Unless you have very good reasons, though, you'll want to strongly consider assigning a static IP address to your guest vm. This is possible even though you are probably using DHCP. Just pick an IP address outside the range that your DHCP server allocates.

ChrisInEdmonton

Posted 2013-08-20T13:50:11.620

Reputation: 8 110

Ok then, how do you do this when guest additions is not installed? – code_dredd – 2018-08-28T23:55:14.123

1Or: VBoxManage guestproperty get <vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP". – Todd Walton – 2018-10-08T17:12:33.037

9that command did not return the IP address – amphibient – 2013-08-20T14:01:09.870

3You need to have the guest additions installed for this to work. – heavyd – 2013-08-20T14:42:06.793

9

First at all make sure you have installed VirtualBox Extension Pack or install it if don't.

For example ( for Ubuntu )

> wget http://download.virtualbox.org/virtualbox/4.2.12/Oracle_VM_VirtualBox_Extension_Pack-4.2.12-84980.vbox-extpack
> sudo VBoxManage extpack install ./Oracle_VM_VirtualBox_Extension_Pack-4.2.12-84980.vbox-extpack

or from

https://www.virtualbox.org/wiki/Downloads

After that you can get IP of VM

VBoxManage guestproperty enumerate <_name_of_VM_> | grep IP | grep -o -w -P -e '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'

weivall

Posted 2013-08-20T13:50:11.620

Reputation: 191

5still doesnt display the IP for me – Josh Nankin – 2015-04-21T22:20:27.693

@JoshNankin Did you solve this? – user3933607 – 2017-09-07T21:06:18.513

@JoshNankin what VBoxManage guestproperty enumerate <name_of_VM> | grep IP gives you? – weivall – 2017-09-08T08:39:18.667

for OSX -

VBoxManage guestproperty enumerate 4ab1029f-23ff-4c5c-b4bd-3b6d68aa7cc0 | grep IP | grep -oE "\b([0-9]{1,3}.){3}[0-9]{1,3}\b"

Regex for IP I took https://stackoverflow.com/questions/11482951/extracting-ip-address-from-a-line-from-ifconfig-output-with-grep

– weivall – 2017-09-08T08:41:40.587

5

I know I'm late to this party, but this will work with VirtualBox 5.0.6.

This uses VBoxManage to grab a list of running virtual machines, queries their properties in a loop, and displays the IP addresses in a pretty way.

#!/bin/bash

for f in $(VBoxManage list runningvms | awk -F\" '{print $2}'); do
      echo "$f:"
      VBoxManage guestproperty enumerate "$f" | grep IP
    done

Dan Stynchula

Posted 2013-08-20T13:50:11.620

Reputation: 51

1

You can look at the DHCP table in the router, before startup and after and compare them. If the router displays the computer name, that makes it a lot easier of course. This is maybe not a sofisticated solution, but it works.

Most routers assign dynamic addresses, but they keep giving a machine or VM (router doesn't know the difference) the same IP for most of the time. If you copy a VM in Virtualbox you can choose to set a new MAC address, which is the way the router identifies the VM.

SPRBRN

Posted 2013-08-20T13:50:11.620

Reputation: 5 185

0

I had similar task in python. Probably it would be helpful.

pip install pyvbox

and then in python:

import virtualbox

vbox = virtualbox.VirtualBox()
vm = vbox.find_machine('running_vb_machine_name')
res = vm.enumerate_guest_properties('/VirtualBox/GuestInfo/Net/0/V4/IP')
ip = res[1][0]
print ip

Roman

Posted 2013-08-20T13:50:11.620

Reputation: 1

0

I used the controlvm option to start a packet trace on the active nic of the guest and opened the local file with wireshark. The guests' IP address will be in there.

VBoxManage controlvm <vm-name> nictracefile<1-N> /full/path/to/file
VBoxManage controlvm <vm-name> nictrace<1-N> on

wireshark /full/path/file

Don't forget to disable the trace when you're done.

VBoxManage controlvm <vm-name> nictrace<1-N> off

pp-paul

Posted 2013-08-20T13:50:11.620

Reputation: 1

-4

VMNAME="..."; # save ID of VM  in VMNAME variable
vboxmanage guestproperty enumerate $VM_NAME|grep IP|cut -f2 -d,|cut -f2 -d:

Abdennour TOUMI

Posted 2013-08-20T13:50:11.620

Reputation: 211

5We’re looking for substantial answers that provide some explanation and context.  Please don’t give just a two-line code answer; explain why your answer is right, ideally with citations.  Answers that don’t include explanations may be removed.  But also, your answer is equivalent to previous answers except for trivial formatting differences.  Please don’t post an answer unless you actually have something new to contribute. – G-Man Says 'Reinstate Monica' – 2015-12-26T11:48:42.423

:). u'r welcome .. not at all – Abdennour TOUMI – 2015-12-26T12:27:56.103

@G-Man : if you have thousand accounts , log-in to them , and vote-down.. Unfortunately , i will not delete the answer . – Abdennour TOUMI – 2015-12-26T22:29:57.127