How to have host dhcp server assign same ip to guest?

This is for discussing general topics about how to use VirtualBox.
Post Reply
leonbnu
Posts: 22
Joined: 24. Oct 2008, 04:13

How to have host dhcp server assign same ip to guest?

Post by leonbnu »

On a host-only network, I need to keep each guest a fixed ip address. Instead of assigning a static ip address in the guest, is there a way to have the host dhcp server assign a same ip to the guest every time? Thank you1
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: PUEL
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: How to have host dhcp server assign same ip to guest?

Post by Sasquatch »

Boot the VMs in the same order every time. The first VM that requests an IP will get the first IP. If you have a few VMs, let's call them vm1, vm2 and vm3, and you have a pool from .100 to .150, you get the following result:
Boot vm1 first > gets .100
Boot vm2 second > gets .101
Boot vm3 last > gets .102

Boot vm2 first > gets .100
Boot vm1 second > gets .101
Boot vm3 last > gets .102

Boot vm3 first > gets .100
and so on.

If you want each VM to have the same IP address, no matter in what order you boot them, just give them fixed addresses. The only information they get is IP and netmask. Nothing fancy.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
uli100
Posts: 59
Joined: 29. Oct 2009, 18:34
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Various Linux Distros
Location: Stuttgart, Germany

Re: How to have host dhcp server assign same ip to guest?

Post by uli100 »

You can do something like this:

* Deactivate the DHCP server which is integrated in VirtualBox
* Install/configure/activate a DHCP server on your host operating system, for example "dnsmasq" on linux
* Make sure the DHCP server listens on your host-only network interface (vboxnet0)

For details on how to use static ip addresses, you'll have to look at the man pages of the DHCP server you're using.

Hope this helps,
Uli.
HubTou
Posts: 93
Joined: 24. Nov 2009, 11:01
Primary OS: FreeBSD
VBox Version: OSE other
Guest OSses: FreeBSD, OpenSolaris, and several Linuxes

Re: How to have host dhcp server assign same ip to guest?

Post by HubTou »

Hello,

I would:
- assign a specific MAC address to each VM using "VBoxManage modifyvm <name> --macaddress<1-N> <mac>"
- use a DHCP server that can assign IP addresses depending on the MAC address of the requester

For example, with ISC DHCPD:

Code: Select all

host client {
    hardware ethernet 00:11:22:33:44:55;
    fixed-address 11.22.33.44;
}
I think it would also be very easy to modify the internal DHCP server of VirtualBox to use a mapping file between MAC and IP addresses.

I'm considering to do it, because I need this for bridged guests that I intend to keep independent of the network environment of their host.

Best regards,

Hubert
leonbnu
Posts: 22
Joined: 24. Oct 2008, 04:13

Re: How to have host dhcp server assign same ip to guest?

Post by leonbnu »

Hi HubTou,

Have you succeeded to modify the internal DHCP server of VBox? If this could be done easily, then this would be the easiest solution.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: How to have host dhcp server assign same ip to guest?

Post by Perryg »

I don't get it?
The whole purpose for DHCP is to automatically assign IP addresses. If you want the same IP address on the guests all the time then that is what static addressing is for.
Why do you need to modify anything? Even if you want some to have dynamic and some to have static it is the same thing. Assign static addresses to the guests that need to be constant.
What am I missing?
Sasquatch
Volunteer
Posts: 17798
Joined: 17. Mar 2008, 13:41
Primary OS: Debian other
VBox Version: PUEL
Guest OSses: Windows XP, Windows 7, Linux
Location: /dev/random

Re: How to have host dhcp server assign same ip to guest?

Post by Sasquatch »

I agree with Perry here. You're all doing it the hard way, jumping through hoops and making turns that are not necessary. Want the same, set it that way. Don't try to get the same with some kind of DHCP option, it takes a lot more to get that going than to just set the IP in the Guest OS.
It may be 'easy' on a Linux Host, but think about Mac and Windows. The TS doesn't specify it's host, so everyone can say "do this and use that", but you all assume he's using one particular OS as Host. Very good advise there! :roll:
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
HubTou
Posts: 93
Joined: 24. Nov 2009, 11:01
Primary OS: FreeBSD
VBox Version: OSE other
Guest OSses: FreeBSD, OpenSolaris, and several Linuxes

Re: How to have host dhcp server assign same ip to guest?

Post by HubTou »

Hello,

Yes, our use case is not obvious :-)

Let me try to explain it.

We have hosts (nodes of a private cloud) that are generic stripped down-optimized-VirtualBox servers, located at several dedicated servers hosting providers.
We have virtual machines (guests) which are designed to be host network independent, in order to be relocated at will, without any change.

Guests are known to the outside world only by their name(s).

We have name servers that make the matching between guests names and guest IP addresses, with short time-to-live caching delays, in order to be able to quickly point a guest name to a new guest IP address.

With NATed guests, we have no problem:
- they don't need to know their external IP address and are happy with their 10.0.2.15 internal address.
- they are configured to use DHCP, and get their internal IP address, network mask and DNS servers that way (nothing is hardwired).
- the host assigns them a specific (failover) IP address, makes some port forwarding (the ports we want to be visible to the outside world), sets some firewall rules (the ports we want to restrict to specific networks or IP addresses) and VPN tunnels (with specific networks).

With Bridged guests, it's more complicated:
- at dedicated servers hosting providers, we usually can't directly use virtual MAC addresses or our servers get banned.
- if we previously declare that a failover IP address will be using a virtual MAC address, it's allowed at some hosting providers
(for examples: see http://guides.ovh.com/DedieMac / http://guides.ovh.com/BridgeClient / http://documentation.online.net/fr/serv ... p-failover / http://www.online.net/serveur-dedie/adr ... elle.xhtml / http://forum.online.net/index.php?/topi ... irtuelles/ ).
- but the solution involves knowing in the guest the external (failover) IP address, external IP address of the gateway and name servers, as well a setting some static routes.
- we don't want to hardwire these information because we still want to be able to relocate these guests at will.
[I've not yet tried what follows, but I will in the next weeks]
- the solution would be to provide all these information through DHCP. According to RFC2132 and RFC3442 (http://tools.ietf.org/html/rfc2132 /http://tools.ietf.org/html/rfc3442) it should be possible to provide all the information above to a guest (including static routes).
- I think I could do that with ISC DHCPD and some others, but I would prefer to use the internal DHCP server even if it's not sophisticated enough yet to use all the needed DHCP options (a mapping file would be sufficient for what I intend to do, and would avoid launching another process just for that).
- of course, the DHCP requests and offers would have to be filtered by the host firewall in order to stay inside that server.
- from the host, a virtual MAC address would be assigned to the guest.
- the failover IP address that we want for that guest would be associated to that virtual MAC address.
- thus, we wouldn't have to change the DNS information at each guest launch because of an unwanted IP address change, and the guest would stay completely unaware of its network environment and of the fact that it's used through NAT or bridge (whatever the OS installed inside it).

So, it's not that I want a guest to have or be aware of a specific IP address, but I'm currently obliged to do it because of the way hosting providers are handling VPS inside the dedicated servers that they provide. And the whole point of our private cloud is to be able to relocate guests at will between our different providers and/or our internal nodes...

Best regards,

Hubert
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: How to have host dhcp server assign same ip to guest?

Post by Perryg »

@Hubert,
Thank you for explaining your situation.
AFAIK the VirtualBox DHCP server only provides addressing for host-only and NAT but not Bridged. So any kind of DHCP would need to be external.
Probably why you are using an external DHCP server.

But getting back to the OP and their question, they more than likely will not even know what we are talking about. Most are using this on a home system to test different aspects of one thing or another, and for them this is overkill and simply not needed.

I would however like to continue this discussion so when you have time and if you want to let's start a new topic with advanced DHPC configuration in the title so the users that want/need it can benefit and not tag it to general users posts.
leonbnu
Posts: 22
Joined: 24. Oct 2008, 04:13

Re: How to have host dhcp server assign same ip to guest?

Post by leonbnu »

Cited from Wikipedia:
Depending on implementation, the DHCP server may have three methods of allocating IP-addresses:

(1)dynamic allocation: A network administrator assigns a range of IP addresses to DHCP, and each client computer on the LAN is configured to request an IP address from the DHCP server during network initialization. The request-and-grant process uses a lease concept with a controllable time period, allowing the DHCP server to reclaim (and then reallocate) IP addresses that are not renewed.

(2)automatic allocation: The DHCP server permanently assigns a free IP address to a requesting client from the range defined by the administrator. This is like dynamic allocation, but the DHCP server keeps a table of past IP address assignments, so that it can preferentially assign to a client the same IP address that the client previously had.

(3)static allocation: The DHCP server allocates an IP address based on a table with MAC address/IP address pairs, which are manually filled in (perhaps by a network administrator). Only requesting clients with a MAC address listed in this table will be allocated an IP address. This feature (which is not supported by all DHCP servers) is variously called Static DHCP Assignment (by DD-WRT), fixed-address (by the dhcpd documentation), Address Reservation (by Netgear), DHCP reservation or Static DHCP (by Cisco/Linksys), and IP reservation or MAC/IP binding (by various other router manufacturers).
I believe the dhcp server in vbox only implemented the dynamic allocation. But the other two methods do exist and are widely implemented.
If you know that it's not implemented right now, a simple answer saying so would be much helpful.
I really don't understand why the moderators here always presume the users do not know what they are talking about.
HubTou
Posts: 93
Joined: 24. Nov 2009, 11:01
Primary OS: FreeBSD
VBox Version: OSE other
Guest OSses: FreeBSD, OpenSolaris, and several Linuxes

Re: How to have host dhcp server assign same ip to guest?

Post by HubTou »

Hello leonbnu,

Looking quickly at the product source code (/src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp), it seems that "static allocation" is already available!

If I understand correctly, the DHCP IP ranges are associated with MAC addresses.

If you gave a VM a specific MAC address with "VBoxManage modifyvm <name> --macaddress<1-N> <mac without ':' characters>",
you could use the "VBoxNetDHCP" command with that MAC address in the "--mac-address" option,
and the IP you want associated to that MAC address with the "--lower-ip" *and* "--upper-ip" options (they must be equal).

Can you try this?

Best regards,

Hubert

Code: Select all

# VBoxNetDHCP --help
VBoxNetDHCP Version 3.2.12_OSE
(C) 2009-2011 Oracle Corporation
All rights reserved.

Usage: VBoxNetDHCP <options>

Options:
    -N, --name
    -n, --network
    -t, --trunk-name
    -T, --trunk-type
    -a, --mac-address
    -i, --ip-address
    -D, --lease-db
    -v, --verbose
    -b, --begin-config
    -g, --gateway
    -l, --lower-ip
    -u, --upper-ip
    -m, --netmask

Code: Select all

#find . -type f -name "*DHCP*"
./src/VBox/Main/include/DHCPServerRunner.h					VirtualBox Main - interface for VBox DHCP server
./src/VBox/Main/include/DHCPServerImpl.h					VirtualBox COM class implementation
./src/VBox/Main/DHCPServerImpl.cpp						VirtualBox COM class implementation
./src/VBox/Main/DHCPServerRunner.cpp						VirtualBox Main - interface for VBox DHCP server
./src/VBox/Frontends/VBoxManage/VBoxManageDHCPServer.cpp	VBoxManage - Implementation of dhcpserver command
./src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp				VBoxNetDHCP - DHCP Service for connecting to IntNet
./src/VBox/NetworkServices/DHCP/VBoxNetDHCPHardened.cpp		VBoxNetDHCP - Hardened main().

# find . -type f -name "*dhcp*"
./src/VBox/Devices/Network/lwip/src/include/lwip/dhcp.h
./src/VBox/Devices/Network/lwip/src/core/dhcp.c			Dynamic Host Configuration Protocol client / This is a DHCP client for the lwIP TCP/IP stack. It aims to conform with RFC 2131 and RFC 2132

# find . -type f -exec grep -il dhcp {} \;
./src/VBox/Installer/linux/Makefile.kmk
./src/VBox/Installer/freebsd/Makefile.kmk
./src/VBox/Installer/freebsd/pkg_plist
./src/VBox/Installer/solaris/vboxconfig.sh
./src/VBox/Installer/solaris/Makefile.kmk
./src/VBox/Installer/solaris/checkinstall.sh
./src/VBox/Installer/solaris/makepackage.sh
./src/VBox/VMM/VM.cpp
./src/VBox/Main/Makefile.kmk
./src/VBox/Main/VirtualBoxImpl.cpp
./src/VBox/Main/win/svchlp.cpp
./src/VBox/Main/win/svchlp.h
./src/VBox/Main/win/NetIf-win.cpp
./src/VBox/Main/xpcom/server.cpp
./src/VBox/Main/idl/VirtualBox.xidl
./src/VBox/Main/os2/NetIf-os2.cpp
./src/VBox/Main/ConsoleImpl2.cpp
./src/VBox/Main/include/DHCPServerRunner.h
./src/VBox/Main/include/VirtualBoxImpl.h
./src/VBox/Main/include/HostNetworkInterfaceImpl.h
./src/VBox/Main/include/netif.h
./src/VBox/Main/include/DHCPServerImpl.h
./src/VBox/Main/generic/NetIf-generic.cpp
./src/VBox/Main/DHCPServerImpl.cpp
./src/VBox/Main/HostNetworkInterfaceImpl.cpp
./src/VBox/Main/cbinding/VBoxCAPI_v2_2.h
./src/VBox/Main/cbinding/VBoxCAPI_v3_0.h
./src/VBox/Main/cbinding/VBoxCAPI_v3_1.h
./src/VBox/Main/xml/Settings.cpp
./src/VBox/Main/xml/VirtualBox-settings-common.xsd
./src/VBox/Main/xml/SettingsConverter.xsl
./src/VBox/Main/DHCPServerRunner.cpp
./src/VBox/Additions/WINNT/Graphics/Wine/include/iptypes.h
./src/VBox/Additions/WINNT/Graphics/Wine/include/winerror.h
./src/VBox/Frontends/VBoxShell/vboxshell.py
./src/VBox/Frontends/VBoxManage/VBoxManageDHCPServer.cpp
./src/VBox/Frontends/VBoxManage/VBoxManage.h
./src/VBox/Frontends/VBoxManage/Makefile.kmk
./src/VBox/Frontends/VBoxManage/VBoxManage.cpp
./src/VBox/Frontends/VBoxManage/VBoxManageList.cpp
./src/VBox/Frontends/VBoxManage/VBoxManageHostonly.cpp
./src/VBox/Frontends/VBoxManage/VBoxManageHelp.cpp
./src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsNetwork.cpp
./src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsNetworkDetails.ui
./src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsNetworkDetails.h
./src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsNetwork.h
./src/VBox/Frontends/VirtualBox/src/settings/global/VBoxGLSettingsNetworkDetails.cpp
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_cs.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_xx_YY.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ca_VA.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_sk.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_gl_ES.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ko.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_pt.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_fi.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_lt.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_de.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_da.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ca.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_zh_TW.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_tr.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_el.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_uk.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_it.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_sr.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_eu.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_fr.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_nl.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_bg.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_zh_CN.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ro.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_es.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_pl.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ru.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ar.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_km_KH.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_hu.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_ja.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_id.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_pt_BR.ts
./src/VBox/Frontends/VirtualBox/nls/VirtualBox_sv.ts
./src/VBox/HostDrivers/Support/SUPDrv.c
./src/VBox/HostDrivers/Support/SUPR3HardenedVerify.cpp
./src/VBox/HostDrivers/Support/SUPR0.def
./src/VBox/HostDrivers/VBoxNetFlt/win/VBoxNetFlt-win.c
./src/VBox/HostDrivers/VBoxNetFlt/win/WinNetConfig.cpp
./src/VBox/Runtime/common/checksum/ipv4.cpp
./src/VBox/NetworkServices/Makefile.kmk
./src/VBox/NetworkServices/DHCP/Makefile.kmk
./src/VBox/NetworkServices/DHCP/VBoxNetDHCP.cpp
./src/VBox/NetworkServices/DHCP/VBoxNetDHCPHardened.cpp
./src/VBox/NetworkServices/NAT/VBoxNetNAT.cpp
./src/VBox/NetworkServices/NetLib/VBoxNetUDP.cpp
./src/VBox/NetworkServices/NetLib/VBoxNetBaseService.cpp
./src/VBox/Devices/Makefile.kmk
./src/VBox/Devices/Network/SrvIntNetR0.cpp
./src/VBox/Devices/Network/DevVirtioNet.cpp
./src/VBox/Devices/Network/DevPCNet.cpp
./src/VBox/Devices/Network/lwip/CHANGELOG
./src/VBox/Devices/Network/lwip/src/netif/etharp.c
./src/VBox/Devices/Network/lwip/src/include/lwip/netif.h
./src/VBox/Devices/Network/lwip/src/include/lwip/opt.h
./src/VBox/Devices/Network/lwip/src/include/lwip/dhcp.h
./src/VBox/Devices/Network/lwip/src/core/ipv4/ip.c
./src/VBox/Devices/Network/lwip/src/core/dhcp.c
./src/VBox/Devices/Network/lwip/src/core/netif.c
./src/VBox/Devices/Network/lwip/doc/rawapi.txt
./src/VBox/Devices/Network/lwip/vbox/include/lwipopts.h
./src/VBox/Devices/Network/testcase/tstIntNet-1.cpp
./src/VBox/Devices/Network/DrvNAT.cpp
./src/VBox/Devices/Network/slirp/udp.c
./src/VBox/Devices/Network/slirp/libslirp.h
./src/VBox/Devices/Network/slirp/slirp.c
./src/VBox/Devices/Network/slirp/bootp.c
./src/VBox/Devices/Network/slirp/slirp_state.h
./src/VBox/Devices/Network/slirp/bootp.h
./src/VBox/Devices/PC/Etherboot-src/arch/i386/core/wince_loader.c
./src/VBox/Devices/PC/Etherboot-src/arch/i386/core/multiboot_loader.c
./src/VBox/Devices/PC/Etherboot-src/include/etherboot.h
./src/VBox/Devices/PC/Etherboot-src/include/dev.h
./src/VBox/Devices/PC/Etherboot-src/include/pxe.h
./src/VBox/Devices/PC/Etherboot-src/include/bootp.h
./src/VBox/Devices/PC/Etherboot-src/Config
./src/VBox/Devices/PC/Etherboot-src/pcnet32.zrom
./src/VBox/Devices/PC/Etherboot-src/core/pxe_export.c
./src/VBox/Devices/PC/Etherboot-src/core/nic.c
./src/VBox/Devices/PC/Etherboot-src/core/main.c
./src/VBox/Devices/PC/Etherboot-src/core/nfs.c
./src/VBox/Devices/PC/Etherboot-src/core/config.c
./src/libs/libxml2-2.6.30/doc/tutorial/xmltutorial.pdf
./src/libs/libxml2-2.6.30/macos/libxml2.mcp.xml.sit.hqx
./include/iprt/net.h
./include/VBox/WinNetConfig.h
./include/VBox/settings.h
./kBuild/bin/win.amd64/cygwin1.dll
./kBuild/bin/win.x86/cygwin1.dll
./debian/lintian-override.in
./debian/rules
./debian/postinst.in
Post Reply