Page 1 of 1

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

Posted: 27. Apr 2017, 10:13
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.

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

Posted: 27. Apr 2017, 10:37
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?

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

Posted: 27. Apr 2017, 10:44
by camaell
It's part of a project. I'm supposed to contol vm from the host.

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

Posted: 27. Apr 2017, 16:16
by noteirak
This has nothing to do with the API.
Moving to "Using VirtualBox".

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

Posted: 27. Apr 2017, 16:17
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.

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

Posted: 2. May 2017, 08:16
by camaell
Thanks but there's nothing here to get me to the guest's command line from the host.

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

Posted: 2. May 2017, 09:40
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.

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

Posted: 2. May 2017, 10:44
by camaell
Got it. Thank you :)

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

Posted: 31. Jul 2017, 10:10
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?

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

Posted: 31. Jul 2017, 13:18
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.

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

Posted: 31. Jul 2017, 15:25
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?

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

Posted: 31. Jul 2017, 17:21
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.