How to configure network on VM Ubuntu server with host changing networks?

5,614

Solution 1

Your basic problem (having a static IP) is unrelated to the VM. Instead of using a bridged network interface (i.e. guest system does its own networking), you could in theory use NAT and port redirection and configure the host system to have a static IP.

Unfortunately, it is not possible to configure a static IPv4 address. In an IPv4 network, every node has its own routing table. Typically, most nodes will send traffic to their local network (starting with 192.168., 10., or 172.16-31.* ) directly to the target PC. Unfortunately, the addresses used in the network can be any of the above, and your preferred address may already be taken.

That leaves us with a global IPv4 address. Routing for those is configured in the core internet, and that routing must be configured by the ISP and local gateway to the internet. This is not feasible, and does not allow dynamic readressing.

The Mobile IP standard that would allow such rerouting has not seen widespread implementation. Mobile IPv4 works by having a system manage your global IPv4 address and rerouting it to your current location.

You can achieve something like this today by using a VPN: Set up a VPN server with a static IP somewhere on the internet, and have your Ubuntu Server VM connect to it. Then, redirect the ports in question from the VPN server to the client.

If a constant name that maps to the current IP address is sufficient, you can use a dyndns service. These give you a domain name you can update with a client utility that runs in your VM. That way, you can point my-ubuntu-vm.dyndns-provider.net to the current local address in use.

Instead of registering a global DNS name, you can also use multicast DNS with avahi. After configuration, your VM will be reachable under a name like my-ubuntu-vm.local. Unfortunately, multicast DNS requires additional software. While many Linux and Mac OS X systems support it out of the box, Windows systems do not. You can install Apple's Bonjour (also offered together with Apple Safari) on these systems.

If dyndns doesn't work for you (you really need an IP, and not a name, or update times are too spotty), and you have almost constant internet connection, I'd recommend moving the VM system to a host that has a global IP address. This has the same downsides as the VPN solution (doesn't work when you're not connected to the internet, and you'll have to pay for the server and global address), but is comparatively easy to set up.

If you only want to access the VM from the host itself, you can simply use NAT and Port Forwarding (Now available in the GUI to, via Devices -> Network Adapters). Redirect the ports (for example, redirect the client's HTTP server from port 80 on the guest to port 2080 on the host), and then use the modified port. Step-By-step:

  1. Go to the network tab. If the VM is running, select Devices-> Network Adapters. If it is not, right-click, select Settings (or click the Settings button), and then go to the Network tab.
  2. Make sure there is only one adapter, and that it is set to NAT. (If you change anything here, you need to reconfigure the VM)
  3. Open up Advanced in the networking tab.
  4. Click Port Forwarding.
  5. Add a rule. Set the host port to a high port (like 2080), and the client port to the port you want to redirect (80 for HTTP, 22 for ssh).
  6. Enter http://localhost:2080 in your host system's browser to access the client HTTP server.

If you need the services to run on the same ports, you can also add an additional interface (apart from the NAT one) and set it to host-only. That will make a new interface appear on your host system, and you can use the private IPs VirtualBox reserves for this network to communicate with the guest. Step-by-step:

  1. Shut down the VM.
  2. Right click the VM in the Manager and select Settings.
  3. Go to the Network tab.
  4. Configure two interface: One as NAT (to allow your VM to access the internet), the other as Host-only (to allow your host to communicate with the guest).
  5. Boot the VM, and type /sbin/ifconfig to find out its address. You want the one starting with 192.168..
  6. Use that address on the host system.

Solution 2

You can't have a static IP address if you move your server around providers. Each provider will assign you whatever address it wants. As was already mentioned, you can use a dyndns service to point a domain name at whatever your IP address happens to be at a time. However, this will not work in general, because there may be a firewall or NAT blocking incoming connections.

A reliable but complex alternative is to make use of an existing server which already has a static IP address (or an updated dyndns domain). If nothing else, you can probably set one up at home (it better be Linux). Once you have such a server, set up a Virtual Private Network (VPN) between the static server and your roaming Ubuntu server. This will give your static server and your roaming server private IP addresses through which they can communicate, wherever your laptop is. In other words, it will appear as if the static server and the roaming server are part of the same LAN. Now you can set up NAT and routing on the static server to allow the roaming server to access the Internet through the VPN and the static server. You can add port forwarding rules on the static server to redirect specific ports from the static server to the roaming server, through the VPN. This will make it appear as if these services are located at the IP address of the static server, while they will really go through the VPN to be answered by the roaming server.

Share:
5,614

Related videos on Youtube

wise
Author by

wise

My background is as a software developer at Motorola in a variety of environments (Windows, Solaris, Linux) and languages (C/C++, perl, ruby, C#). Worked as a business analyst for a few years on large communications systems for Tier-1 customers, and also for government departments in Western Australia. Last few years working in Service Delivery, currently managing Application Support team supporting 50+ applications across a wide variety of technologies, some modern, some deprecated. Now working as a service management consultant in Australia/New Zealand for IBM.

Updated on September 18, 2022

Comments

  • wise
    wise over 1 year

    My host is a Windows 7 laptop, and I have a guest Ubuntu 11.04 server running in VMware. I'm using the guest server for Rails development.

    My laptop goes in and out of networks -- my home network, the work network, a 3G connection, etc.

    I'd like to set up the ubuntu server so that:

    • I can access it (Samba, HTTPD and SSH) at the same IP address all the time, regardless of the network my laptop is on. I only want to access the VM server from the laptop. It's a development server. A stable hostname and dynamic IP would be sufficient too -- so long as I can hit it via HTTP, SSH and Samba from the laptop, regardless of whether the laptop is connected to a network or not, and regardless of the network.
    • The server will be able to connect to the internet (assuming my laptop has connectivity) so I can apt-get.

    Is that possible? I've tried setting up a static IP in /etc/network/interfaces, but that only works for my home network, and if I go anywhere else I can't access the VM when my laptop isn't on my home network.

    • Ambroz Bizjak
      Ambroz Bizjak over 12 years
      "I can access it (SAMBA, HTTPD and SSH) at the same IP address all the time" do you mean that from just the laptop, or from any Internet host?
  • wise
    wise over 12 years
    Thanks for the answer. I've updated the question with edits to clarify based on @Ambroz Bizjak's comment -- does that affect your answer?
  • phihag
    phihag over 12 years
    @Stewart Johnson Yes, significantly. Added two paragraphs.
  • wise
    wise over 12 years
    Thanks for the clarification, but I don't quite follow what you mean by the bit you added. Should I set the VM to use the host's IP address? Where do I do the port forwarding configuration?
  • wise
    wise over 12 years
    Awesome - that's great. I went with the new host-only adapter and that works just like I need. Thanks for the detailed explanation!