43

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)?

9 Answers 9

24

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.

4
  • 19
    that command did not return the IP address
    – amphibient
    Aug 20, 2013 at 14:01
  • 4
    You need to have the guest additions installed for this to work.
    – heavyd
    Aug 20, 2013 at 14:42
  • Ok then, how do you do this when guest additions is not installed?
    – code_dredd
    Aug 28, 2018 at 23:55
  • 1
    Or: VBoxManage guestproperty get <vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP". Oct 8, 2018 at 17:12
11

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}'
4
  • 8
    still doesnt display the IP for me Apr 21, 2015 at 22:20
  • @JoshNankin Did you solve this? Sep 7, 2017 at 21:06
  • @JoshNankin what VBoxManage guestproperty enumerate <_name_of_VM_> | grep IP gives you?
    – weivall
    Sep 8, 2017 at 8:39
  • 1
    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 stackoverflow.com/questions/11482951/…
    – weivall
    Sep 8, 2017 at 8:41
8

You can do it without install Guest Addition

But you need to know your networks
for example, we have VM with two adapters
two network adapters

one for Internet communication
second for host only communication

you can see network with command VBoxManage list

nat network:

% VBoxManage list natnets
NetworkName:    NatNetwork
IP:             10.10.12.1
Network:        10.10.12.0/24
DHCP Enabled:   Yes
Enabled:        Yes

host only network

% VBoxManage list hostonlyifs
Name:            vboxnet0 
DHCP:            Disabled
IPAddress:       192.168.6.1
NetworkMask:     255.255.255.240

here is main part
show running VMs

% vboxmanage list runningvms
"centos7 node0 ip test" {46ec9075-f49b-4d16-9867-d56710b6a3da}

we need VM uuid
it is 46ec9075-f49b-4d16-9867-d56710b6a3da

now we can fetch MAC addresses

% vboxmanage showvminfo --details 46ec9075-f49b-4d16-9867-d56710b6a3da | fgrep MAC
NIC 1:                       MAC: 08002783E8BB, Attachment: NAT Network 'NatNetwork', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none
NIC 2:                       MAC: 080027D992F0, Attachment: Host-only Interface 'vboxnet0', Cable connected: on, Trace: off (file: none), Type: 82540EM, Reported speed: 0 Mbps, Boot priority: 0, Promisc Policy: deny, Bandwidth group: none

08002783E8BB for first adapter in 'NatNetwork' network
080027D992F0 for second adapter for 'vboxnet0' interface

now we can get IP from DHCP table for first adapter

% vboxmanage dhcpserver findlease --network NatNetwork --mac-address=08002783E8BB
IP Address:  10.10.12.4                     
MAC Address: 08:00:27:83:e8:bb
State:       acked
Issued:      2020-07-13T13:31:20Z (1594647080)
Expire:      2020-07-13T13:41:20Z (1594647680)
TTL:         600 sec, currently 586 sec left

and for second

% vboxmanage dhcpserver findlease --interface vboxnet0 --mac-address=080027D992F0
IP Address:  192.168.6.3
MAC Address: 08:00:27:d9:92:f0
State:       acked
Issued:      2020-07-13T13:29:06Z (1594646946)
Expire:      2020-07-13T13:39:06Z (1594647546)
TTL:         600 sec, currently 399 sec left

please note
for nat network we use option --network
for host only adapter we use option --interface

now we connect to this VM from host by ssh

% ssh root@192.168.6.3
root@192.168.6.3's password: 
Last login: Mon Jul 13 09:05:33 2020
[root@node0 ~]# 
2
  • VBoxManage: command not found
    – Alex
    Feb 20, 2021 at 16:07
  • This is what I wanted, thanks.
    – Marinaio
    Feb 2 at 13:19
6

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
0
2

This is how I do it, both by hand and in ansible. The "/Net/0/" is the first network interface, "/Net/1/" the second etc. The "/V4/" is for IPv4 and "/V6/" is IPv6.

IPv4 version

VBoxManage guestproperty get <uuid|vmname> "/VirtualBox/GuestInfo/Net/0/V4/IP" | sed -e 's/Value: //g'

IPV6 version

VBoxManage guestproperty get <uuid|vmname> "/VirtualBox/GuestInfo/Net/0/V6/IP" | sed -e 's/Value: //g'

Put into a simple script :

#!/bin/sh
for VM in $(VBoxManage list runningvms | awk -F\{ '{print $2}' | sed -e 's/}//g');
do {
  VMNAME="$(VBoxManage showvminfo ${VM} --machinereadable | awk -F\= '/^name/{print $2}')"
  VMIP=$(VBoxManage guestproperty get ${VM} "/VirtualBox/GuestInfo/Net/0/V4/IP" | sed -e 's/Value: //g')
  printf "VM-IP: %-16s VM-Name: %-50s\n" "${VMIP}" "${VMNAME}"
} done

Which provides an output similar to this of all running VM's:

VM-IP: 10.13.172.82     VM-Name: "Ansible Lab VM1 Master"
VM-IP: 10.13.172.84     VM-Name: "Ansible Lab VM2 Slave CentOS"
VM-IP: 10.13.172.83     VM-Name: "Ansible Lab VM3 Slave Win10"
...etc

Note: Requires Guest Additions to be installed.

1
  • You can validate that the guestproperties are available (guestadditions installed correctly and running) by executing VBoxManage guestproperty enumerate ${VM}
    – shadowbq
    Nov 22, 2021 at 14:40
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.

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
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
-4
VMNAME="..."; # save ID of VM  in VMNAME variable
vboxmanage guestproperty enumerate $VM_NAME|grep IP|cut -f2 -d,|cut -f2 -d:
3
  • 5
    We’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. Dec 26, 2015 at 11:48
  • :). u'r welcome .. not at all Dec 26, 2015 at 12:27
  • @G-Man : if you have thousand accounts , log-in to them , and vote-down.. Unfortunately , i will not delete the answer . Dec 26, 2015 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.