How to assign an ip address to a vm from the host with using vboxmanage?

This is for discussing general topics about how to use VirtualBox.
Post Reply
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

How to assign an ip address to a vm from the host with using vboxmanage?

Post by camaell »

I'm using virtualbox 5.1.18 and trying to assign an ip address to my virtual machine from the host. Vboxmanage, or a script, or basically anything related with command line on the host will do. I've been searching this for days and couldn't come up with anything.

My host is Windows 10 and vm is ubuntu 16.4.

Please help me.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by socratis »

Why do you "need" to do it from the host and you can't do it from within the guest by assigning a static IP?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by camaell »

It's part of a project. I'm supposed to contol vm from the host.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by noteirak »

This has nothing to do with the API.
Moving to "Using VirtualBox".
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by noteirak »

What you need is the vboxmanage guestcontrol commands set.
You'll be able to run any command in the guest, like ip or ifconfig that will allow you to set an IP.

More info in the User Manual, Chapter 8.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by camaell »

Thanks but there's nothing here to get me to the guest's command line from the host.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by mpack »

Sure there is, assuming the guest even has a command line. VBoxManage guestcontrol <vm> run <cmdlinetool> <cmdline args>, and that's as much of your homework as I'm willing to do.
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by camaell »

Got it. Thank you :)
camaell
Posts: 13
Joined: 24. Apr 2017, 10:22

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by camaell »

Hello again,

I can change the IP address using the guest's command line. But here's the thing, it's not working stable. Here's what I did:

Code: Select all

    vbox = VirtualBox.VirtualBox()
              vm = vbox.find_machine(vmname)
              session = vm.create_session()
              interface='eth0'
Then I'm giving those commands:

Code: Select all

    setxkbmap us
    sudo su
    <my root password>
    ifconfig <interface> <new_ip> up
    route add default gw 192.168.1.1 <interface>
Problems are:
  • Command line needs to be open. Otherwise, it's not working properly
  • With this method, I have only one shot to change the IP. Because I'm using "sudo" command and next time I've tried to be root, it doesn't require the password. So, this ruins the process.
  • I can't control the interface. I have multiple virtual machines and not all of them has the exact interface.
Is there any other way to change ip remotely in bridge mode?
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Windows, Linux

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by scottgus1 »

camaell wrote:change ip remotely in bridge mode
If you are using Virtualbox's Bridged networking, then your guest is acting just like another PC on your physical network, able to access all of the physical network's services.

Solve your problem as if you were using two physical PCs on the network. Virtualbox's commands may not come into the solution at all. How would you change the IP address of one PC from the other PC? Do the same with your host and guest.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by socratis »

camaell wrote:Command line needs to be open. Otherwise, it's not working properly

With this method, I have only one shot to change the IP. Because I'm using "sudo" command and next time I've tried to be root, it doesn't require the password. So, this ruins the process.

I can't control the interface. I have multiple virtual machines and not all of them has the exact interface.
All of the above, are not VirtualBox problems I'm afraid. Those are "scripting" problems for Linux. Maybe your should be looking in the related forums/websites for the solution? Remote-controlling a Linux system?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
mpack
Site Moderator
Posts: 39134
Joined: 4. Sep 2008, 17:09
Primary OS: MS Windows 10
VBox Version: PUEL
Guest OSses: Mostly XP

Re: How to assign an ip address to a vm from the host with using vboxmanage?

Post by mpack »

scottgus1 wrote: Solve your problem as if you were using two physical PCs on the network. Virtualbox's commands may not come into the solution at all. How would you change the IP address of one PC from the other PC? Do the same with your host and guest.
And I'm sure that is the point of the homework, not how to do it in a VirtualBox VM.
Post Reply