Fixed IP for a Linux (Ubuntu) server running inside a VM?

I’ve got an Ubuntu 14.04LTS instance running under Hyper-V on my laptop currently. It works fine when I’m on my home network, and I can access it direct via the IP that it’s being assigned (by DHCP presumably). If I take the laptop anywhere else though, the server becomes unavailable, presumably because it’s being allocated an IP I’m not aware of. I’m assuming there’s a way of allocating a fixed (known) IP address inside the VM server, that would give me access whether I’m at home or not, but how? Any suggestions?

I need some clarity. So you have Linux in a VM on a Laptop. Is the host operating system on the Laptop the one trying to access the Linux Server in the VM and are you using NAT for the type of adapter?

Or are you using a second device to try and access the Linux Server in the VM that isn’t connected to the laptop via any sort of network bridge?

For the former, you should be able to just setup a Port Forward, found in Settings > Network > Advanced > Port Forwarding.

Leave the Host IP and Guest IP blank, call it HTTP (for example), and give it a Host Port of 8080 and the guest port needs to match what the VM server expects, 80 for HTTP, 443 for HTTPS, 22 for SSH, you get the idea. The Host Port can be anything though.

Then you can access your VM server using localhost:8080 (so long as your laptop isn’t watching that port too).

For the latter, a device not connected to your laptop, you sort of have an uphill battle here. As you are on a network you have little control over, so your laptop’s IP is what is necessary to know, the VM’s is less. To solve this, you can use a service such as http://www.noip.com/, install their client on the laptop, so it can keep the IP it references up-to-date, then you can use mydnsname.no-ip.com (or whatever host name they assign you) to access your Laptop. Then it is still a matter of setting up Port Forwarding so you can use mydnsname.no-ip.com:8080 to access Apache in the VM.

It’s a Windows 10 laptop running Hyper-V that’s acting as host. I only need to access the server from that, though I know I’ve been able to reach it from other machines local to my home network, as it just picks up another IP in the same range as everywhere else. That said, the ability for other machines to connect, isn’t something I set out to achieve, just a by-product. As far as I know, I’m not doing any NAT’ing - when I created the VM in Hyper-V, there’s an option to set a virtual switch, and associate that with the laptop’s network adaptor, which is treated as an external network, and that was pretty much it.

Time to go do a bit of digging I think…

Ah, okay. Based on the following article, you probably want to switch it to use Internal instead of External for its network adapter.

The other option is to assign a static IP, but that becomes difficult if you move to a network that uses different subnets. Such as 192.168.0.1 at home but work uses 192.168.1.1 or something entirely different. Internal, should make it more of a bridge with the laptop and using localhost may still work.

This may also be of value
http://superuser.com/questions/719893/hyper-v-nat-port-forwarding

I’ve been looking at a number of sources on that this morning. The one important limitation of using the internal option, is that the Ubuntu server can’t then get to the internet; something I’d need for updates and adding new packages. I guess I could switch back and forth between Internal and External when I’m at home, but there’s nothing to say that’s the only time I’ll need to reach out to the internet from the VM.

Some of that’s a bit above my head right now, but I’ll keep reading. If I were to create a static IP on the server though, could adding a line in my hosts file allow me to find it, when I’m outside my home network?

So I think you still will want to try and setup NAT in Hyper-V. That way your VM still has a way to connect outside, but it isn’t exposed beyond your laptop (except through port forwarding via your router – I think).

http://blog.areflyen.no/2012/10/10/setting-up-internet-access-for-hyper-v-with-nat-in-windows-8/

That should permit your laptop to always communicate with it regardless of what IP it gets. Especially if you put it in the same subnet.

That looks more promising. I’ll give that a go and report back.

Well it ended up taking a lot longer than I’d anticipated, but I got there in the end. It would seem that it wanted the old external adaptor removing before it would accept an IP from the internal version. I though turning the old one off would have been enough, but no. I’m going though. I’ll try taking it somewhere else now and just confirm that I can get at things still. Thanks for the help.

1 Like

Let me know if it doesn’t. I think this will be a better workflow for you and fits more inline with what I’m accustom to with VirtualBox and VMWare Workstation.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.