Setup shared internet connection on virtualbox with fixed IP

2

2

I am a web developer and until recently I have been using ubuntu as my OS. For many reasons, I have switched back to windows. I still want to keep my server on linux platform, so I setup my local server as a virtual machine. Everything works great, but i have a little struggle with the networking. Since I am working in different places and going around clients, I connect to all sorts of network with different settings. That means the possible IP range is very dynamic which causes issues when I work on my local server. At the moment I have a dynamic IP on my host and static IP on my guest. That way I can access the server from my host (by adding record to hosts file). I also have internet connection on the guest. But once i change networks, it does not work (assuming the network has different configuration).

My question is, how to setup host-guest networking, so no matter what network I connect to, I can keep my static IP on guest, which is registered in hosts file on my host so I can access the webserver and also I will have internet connection on the guest?

Hope it make sense.

Thank you

Tomas

Posted 2014-06-02T23:07:47.853

Reputation: 53

What's your hypervisor? Hyper-V? VirtualBox? What is your network configuration: NAT, Bridge, host-only...? – MariusMatutiae – 2014-06-06T11:02:50.553

is the goal to access the web server from this static IP publicly with or without the host system? – mbb – 2014-06-09T18:46:52.573

Answers

2

Would require some testing/tweaking, but it sounds like setting up a second network adapter on your guest VM might be the answer.

This would allow you to use the primary adapter in a NAT configuration as rhubee recommended (which will provide your guest VM with internet access), while the second adapter has a static IP for an internal network that will not change between host and guest. The testing/tweaking portion would come in as you may possibly need to configure some static routes to ensure traffic for the services/ports route through to the correct interface.

Not quite the same goal, but I have run multiple guest VM's that share an internal only network between them, while having a separate adapter NAT'd so that they could both still hit the internet.

user331822

Posted 2014-06-02T23:07:47.853

Reputation: 36

Yes, I always come to the same thought that I will need 2 network interfaces, but I still wasn't able to successfully set it up... – Tomas – 2014-06-11T08:25:13.823

Ok I have accepted this answer because its closest to the way I have solved the issue. I have setup everything as its here http://askubuntu.com/questions/293816/in-virtualbox-how-do-i-set-up-host-only-virtual-machines-that-can-access-the-in and it works great. No port forwarding or anything needed. Bounty will go to rhubee though because he provided most of the information. Thanks!

– Tomas – 2014-06-11T09:06:45.627

7

It seems you're using bridged networking mode for your VM. This basically connects your guest to whatever network your host is connected to - and needs a matching configuration.

If your network configuration is changing often, you're better off with NAT networking instead. Your host acts as a "router" for your VM, so you don't need to constantly adapt the network configuration on the guest.

Your host also assigns (via DHCP) an IP address to your guest on a private network. You can just assign a static IP to your virtual machine on that same network and change the hosts file accordingly on your host.

For example: You setup NAT networking for your VM, your guest is assigned 192.168.15.3 via DHCP. You change this manually to 192.168.15.250, and on your host you add 192.168.15.250 myserver to your hosts file.

You can then even forward single ports (like 80 or 443 for http and https), so your virtual machine can be reached from other computers as well. This is described in chapter 6 of the VirtualBox manual in further detail, as are the various network modes available.

rhubee

Posted 2014-06-02T23:07:47.853

Reputation: 446

Hi, thanks, yes I tried this, but the problem with this approach is that thre is no internet connection on guest. – Tomas – 2014-06-09T14:29:14.343

1@Tom Are you using DHCP or a Static IP address on the Guest ? If you set it up to use DHCP, it should "just work" – Lawrence – 2014-06-10T08:29:24.263

Hi, I tried to use both, the problem with DHCP is that I always have to change my hosts file on host to access the server. Problem with static IP is that I have to change everything once I move to network with other settings, which is pretty often. – Tomas – 2014-06-11T08:23:51.077

0

No need to register your VM on the network as a separate machine. No need for a hosts file entry, just NAT your VM, you can access it as http://localhost:8000 if you use this procedure:

Go to Settings for your VM in VirtualBox => Network => Enable Network Adapter, Attached to NAT. Advanced => Port Forwarding => enter this: Guest port 80 Host port 8000.

bbaassssiiee

Posted 2014-06-02T23:07:47.853

Reputation: 1 225

I need to use virtual hosts on apache, so I have to be able to setup for example domain.local that points to the guest IP and is handled by apache Virtual host configuration – Tomas – 2014-06-09T14:31:03.783

Each vhost can have its own port number. – bbaassssiiee – 2014-06-09T17:58:40.727

0

This is very easy to do... In Windows, open your network settings (ncpa.cpl). Go to your IPv4 configuration and click on ADVANCED. Assign a fixed IP address in the range of your VM, so that both Windows and Linux are in the same subnet. Your main Windows configuration should remain on DHCP. This way, your network card identifies itself with both foreign and local network and you'll be able to contact everything on both networks.

The only problem is that you may cause an IP conflict if the visiting network is in the same range and already has this IP assigned to someone else. If you're a bit smart in your choice, you can really limit the possibility of this happening.

Jakke

Posted 2014-06-02T23:07:47.853

Reputation: 910

0

I would try with both addresses dynamically assigned by the network's DHCP server (whichever network you are on I presume will have a DHCP server). Then connect to the guest using the guests name...which you may need to edit hosts file for. But at least you are sure that from a Subnet point of view both will be on the same one. I realise that you seem to not want to have to edit the hosts file, but you will probably only need to edit it from time to time depending on the DNS resolution of the host network you have joined.

Michael

Posted 2014-06-02T23:07:47.853

Reputation: 101