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?
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:
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.
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:
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
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.
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. Seeman nmcli
andman 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
):Bolded portions are your inputs, replace things in curly braces with your settings. Answering
no
to the question aboutipv4.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:
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:
Do note, that configurations created this way are likely to confuse the GUI tools. At best, they won't show you the static addresses.
Debian Buster/10 NetworkManager
nm-connection-editor
can now do this from the graphical user interface.Edit the connection, open "IPv4 Settings" tab, then:
This will write the following settings in
/etc/NetworkManager/system-connections/connection.nmconnection
: