Static IP – How to Setup Static IP for Network-Manager in VirtualBox on Ubuntu Server

command linenetwork-managernetworkingvirtualbox

Hi there is anyone familiar with the non-gui version of ubuntus network Manager?

I'd like to set up a static IP with it in virtualbox without touching the file /etc/network/interfaces.

  • IP: 192.168.56.101
  • Gateway: 102.168.1.1
  • Netmask: 255.255.255.0

Additionally I'd like to set up a second network interface with a dynamic IP to get connected to the internet with the Vbox.

Best Answer

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.)

Related Question