20

We can use multiple IP addresses for a single interface in Linux (eg. eth0:0, eth0:1). But can we use DHCP and static IP at the same time? eth0:1 should have a static IP address and eth0:0 should be configured using DHCP.

How can we accomplish both?

Fabby
  • 35,017
shantanu
  • 8,835

6 Answers6

16

Yes, this can be done. However, you would usually use eth0 and eth0:0 for this instead of eth0:0 and eth0:1. You could set up your /etc/network/interfaces file like this:

auto lo eth0 eth0:0
iface lo inet loopback

iface eth0 inet dhcp

iface eth0:0 inet static
    address ...
    netmask ...

You probably don't want to set a gateway on the eth0:0 interface because that could conflict with the gateway settings you get from DHCP. If you enter manual settings in /etc/network/interfaces you will want to make sure network-manager is not trying to manage your settings. This is covered here.

jkt123
  • 3,600
10

After some intensive searching for an answer to the same problem, I worked out a solution that lets Network Manager continue to manage your connections. First, create a DHCP connection normally in Network Manager using Edit Connections > Add. This will create a file located in /etc/NetworkManager/system-connections/. Open this file in your text editor with Super User permissions. Example: sudo nano /etc/NetworkManager/system-connections/DHCPEthernet

The code will look something like this:

[802-3-ethernet]
duplex=full
mac-address=00:0E:C6:88:31:43

[connection]
id=DHCPEthernet
uuid=26af83f1-c48c-4454-9038-bbb4bec3e3a3
type=802-3-ethernet
timestamp=1405008541

[ipv6]
method=auto

[ipv4]
method=auto

Add a line under the ipv4 section for the static ip address you would like to add. I am using 192.168.10.1 with no gateway for this example

address1=192.168.10.1/24,0.0.0.0

Save the file, and use Network Manager to disconnect and then reconnect to the network. Pinging will confirm both IP addresses function properly. The output from ip addr confirms success.

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 14:10:9f:d7:22:0b brd ff:ff:ff:ff:ff:ff
    inet 172.20.1.71/24 brd 172.20.1.255 scope global wlan0
       valid_lft forever preferred_lft forever
    inet6 fe80::1610:9fff:fed7:220b/64 scope link 
       valid_lft forever preferred_lft forever
3: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0e:c6:88:31:43 brd ff:ff:ff:ff:ff:ff
    inet 172.20.1.60/24 brd 172.20.1.255 scope global eth2
       valid_lft forever preferred_lft forever
    inet 192.168.10.1/24 brd 192.168.10.255 scope global eth2
       valid_lft forever preferred_lft forever
    inet6 fe80::20e:c6ff:fe88:3143/64 scope link 
       valid_lft forever preferred_lft forever
Andrew Vian
  • 537
  • 4
  • 6
7

I checked around a bit and found out, that nowadays it is possible to create this configuration using the nmcli tool. In fact, it allows for complete control of NetworkManager. The manual pages for nmcli are very thorough and pretty understandable. See man nmcli and man nm-settings.

To set up the configuration in this question, the easiest way is to edit your current connection profile. Find out the name of the profile from your GUI NetworkManager tool and edit it using the CLI editor (here the name is Ethernet connection):

$ sudo nmcli c edit 'Ethernet connection'

===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'Ethernet connection 1'

Type 'help' or '?' for available commands.
Type 'describe [.]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, ipv4, ipv6
nmcli> goto ipv4
You may edit the following properties: method, dns, dns-search, dns-options, dns-priority, addresses, gateway, routes, route-metric, ignore-auto-routes, ignore-auto-dns, dhcp-hostname, dhcp-send-hostname, never-default, may-fail, dad-timeout, dhcp-timeout, dhcp-client-id, dhcp-fqdn
nmcli ipv4> set ipv4.addresses {your_address_here}/{your_network_prefix_here}
Do you also want to set 'ipv4.method' to 'manual'? [yes]: no
nmcli ipv4> save
Connection 'Ethernet connection' (87fa8e41-7fe3-435a-a2f2-29a9c8084d2d) successfully updated.
nmcli ipv4> quit

Bolded portions are your inputs, replace things in curly braces with your settings. Answering no to the question about ipv4.method preserves DHCP configuration. Afterwords you need to disable and re-enable the configuration, which can be done using GUI-tools or nmcli.

If you want to, it is also possible to create a new connection from the command line:

sudo nmcli -p connection add type ethernet ifname {your_interface_name} con-name MyConnection -- ipv4.addresses {your_address}/{your_prefix_length} ipv4.method auto

Again, replace things in curly braces with your settings.

Finally, if you still wish to edit the configuration files, remember to reload the file after editing:

sudo nmcli connection reload

Do note, that configurations created this way are likely to confuse the GUI tools. At best, they won't show you the static addresses.

4

Debian Buster/10 NetworkManager nm-connection-editor can now do this from the graphical user interface.

Edit the connection, open "IPv4 Settings" tab, then:

  1. Set Method to "Automatic (DHCP)".
  2. Add "Additional static addresses" below.

This will write the following settings in /etc/NetworkManager/system-connections/connection.nmconnection:

[ipv4]
address1=10.0.0.1/24
method=auto
user1338062
  • 1,018
2

To update the accepted answer, for ubuntu 20.04 (with netplan), the official documentation is not super clear about this. However, it's as simple as adding the interface as DHCP, and appending an ip address to it

cat /etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp5s0:
      dhcp4: true
      addresses: [192.168.10.4/24]

After applying the netplan (sudo netplan apply), the result is that DHCP yields me an address in the 10.x range, but the interface also gets a static IP.

2: enp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 04:7c:16:b9:b7:d9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.4/24 brd 192.168.10.255 scope global noprefixroute enp5s0
       valid_lft forever preferred_lft forever
    inet 10.0.0.150/24 brd 10.0.0.255 scope global dynamic noprefixroute enp5s0
       valid_lft 86254sec preferred_lft 86254sec

Credits go to https://support.us.ovhcloud.com/hc/en-us/articles/360002987280-How-to-Configure-an-Additional-IP-as-an-Alias-in-Ubuntu

Arnout
  • 121
0

It is 100% possible to have both multiple static Ip addresses and/or a dynamic IP address for a single interface on a single computer on Ubuntu Server 24.04 LTS using netplan.

I have done so my self.

It almost certainly ias also possible for Ubuntu Desktop and for earlier versions.

(So long as netplan is supported and configured)

However, if your static address is in a different subnet, your router/other computer may not recognise that address.

i.e. A static IP of 10.13.133.9 won't work on a network that is 192.168.2.0/24.

Langdon
  • 81