How to setup a static IP for network-manager in Virtual Box on Ubuntu Server

Solution 1:

You need to use Network Manger from the command line, this is nmcli.

First, you can list the available connections Network Manager knows about with the following, this is important to find the name, as the device id isn't used:

# nmcli con show

This will give you something like:

NAME                UUID                                  TYPE            DEVICE 
Wired connection 1  7a3b674a-f346-3cfb-8b30-ff70e6db1b60  802-3-ethernet  enp0s3

You can then modify the connection with the following:

nmcli con mod "Wired connection 1"
  ipv4.addresses "HOST_IP_ADDRESS/IP_NETMASK_BIT_COUNT"
  ipv4.gateway "IP_GATEWAY"
  ipv4.dns "PRIMARY_IP_DNS,SECONDARY_IP_DNS"
  ipv4.dns-search "DOMAIN_NAME"
  ipv4.method "manual"

When you enter the above use one line, I've just split it into separate lines to make it more clear.

If you want to set the connection to use DHCP, you can use the following:

nmcli con mod "Wired connection 1"
  ipv4.addresses ""
  ipv4.gateway ""
  ipv4.dns ""
  ipv4.dns-search ""
  ipv4.method "auto"

You need all the empty quotes as they remove any settings they previously have.

To add a network, use:

nmcli con add ...

With similar parameters.

To make the settings active, reboot. (I tried re-starting Network Manager, but that didn't seem to activate the changes, but a reboot did.)

Solution 2:

For those who want the NetworkManager approach, I just went through this, taking the tack mss suggested. There's a touch of information on the Debian wiki and full documentation of the options at the GNOME developer site. (From the RHEL7 docs, it does look like their version of nmcli has add support, so hopefully that'll make it in.)

The dynamic IP is pretty simple (just doing the network config, mind, not the VBox side):

[802-3-ethernet]
auto-negotiate=true
mac-address=XX:XX:XX:XX:XX:XX

[connection]
id=Wired connection 1
uuid=xxx-xxxxxx-xxxxxx-xxxxxx-xxx
type=802-3-ethernet
timestamp=0

[ipv6]
method=disabled

[ipv4]
method=auto

Use uuidgen (package uuid-runtime) to make the uuid, and of course fill the MAC address properly. (Usually better to do that than to specify a device name.)

For the static IP (note the semicolon on the DNS array!):

[802-3-ethernet]
auto-negotiate=true
mac-address=XX:XX:XX:XX:XX:XX

[connection]
id=Wired connection 2
uuid=xxx-xxxxxx-xxxxxx-xxxxxx-xxx
type=802-3-ethernet
timestamp=0

[ipv6]
method=ignore

[ipv4]
method=manual
dns=8.8.8.8;8.8.4.4;
address1=192.168.56.101/24,192.168.1.1

Solution 3:

Normally I edit the file /etc/network/interfaces and mod the info to something like this:

iface eth0 inet static
address 192.168.56.101
netmask 255.255.255.0
gateway 192.168.56.1 (u had 102.168.1.1. So, I guessed it was a mistake)