Simultaneous dynamic and static IPs for eth0.

A single network interface can be accessed allocated more than one IP address on GNU/Linux system.

The bulk of this article was originally written in response to a newsgroup posting where somebody wanted to use a laptop in two different locations. In one location the laptop needed to connect using DHCP and in another location it was expected to have static IP address. The situation described below actually allocates three IP addresses to a single network interface card. One address is allocated dynamically and the other two are static.
Content revision history:
Article first written: 10th October 2004
Updated:
Corrected:

Requirements & intermediate result

The requirement was to have a machine with three IP addresses; two IP addresses were to be static and the other was to be allocated dynamically by a DHCP server.

The situation at the end of the initial experiments was not perfect; the scheme seemed to work although the rcnetwork script reported that it had failed when it was told to restart the network. I haven't yet investigated any further to see what exactly it was objecting to.

Actions taken

This experiment was performed on a computer on which SuSE Linux professional version 9.0 had recently been installed.

The files I created were:

  • /etc/sysconfig/network/ifcfg-eth0
  • /etc/sysconfig/network/ifcfg-eth0:1
  • /etc/sysconfig/network/ifcfg-eth0:2

I also modified the file: /etc/sysconfig/network/dhcp

Previously the above file was in whatever state the SuSE 9.0 professional installation had left it since I do not normally use DHCP on my network.

The contents of each file are shown below exactly as they are on my computer except that I changed a couple of critical numbers to protect the innocent:

Contents of file "ifcfg-eth0"

BOOTPROTO='dhcp'
STARTMODE='onboot'
UNIQUE='<Your own computer will generate a number>'

Contents of file "ifcfg-eth0:1"

BOOTPROTO='static' 
BROADCAST='192.168.0.255'
IPADDR='1.2.3.4'
MTU=''
NETMASK='255.255.255.255'
NETWORK='1.2.3.4'
REMOTE_IPADDR=''
STARTMODE='onboot'
UNIQUE='<Your own computer will generate a number>'

Contents of file "ifcfg-eth0:2"

BOOTPROTO='static'
BROADCAST='192.168.0.255'
IPADDR='192.168.0.34'
MTU=''
NETMASK='255.255.255.0'
NETWORK='192.168.0.0'
REMOTE_IPADDR=''
STARTMODE='onboot'
UNIQUE='<Your own computer will generate a number>'

The file dhcp was modified in respect of the parameters listed below:

DHCLIENT_CLIENT_ID="YellowBananaSansFreckles"
DHCLIENT_MODIFY_RESOLV_CONF="yes"
DHCLIENT_SET_DEFAULT_ROUTE="yes"

The text shown above for DHCLIENT_CLIENT_ID is not significant; you can put whatever you like so long as it is unique for your network and so long as it isn't the network interface's hardware address. This parameter is one of the few that can be set from YaST but I think the other two have to be changed by editing the file. Also, if I understood correctly, these parameters could have been placed directly into the ifcfg-eth0 file to make them apply only to that one configuration.

With these configuration options the computer is accessible locally at 192.168.0.34 and also at whatever address the DHCP server allocates, generally 192.168.0.2. It is also accessible globally on the internet.

Notice that the UNIQUE parameter will be determined by your own computer for each network interface. All the configuration files for one particular interface will all receive the same value for UNIQUE.

Explanations:

The file ifcfg-eth0 allows the computer to acquire an IP address from a DHCP server. The file ifcfg-eth0:1 gives the computer its external IP address by which it is known to the rest of the world. Of course, 1.2.3.4 isn't actually its real IP address. The file ifcfg-eth0:2 gives the computer its ordinary IP address by which it is usually known internally. The DHCLIENT_CLIENT_ID="xyz" must be set. Without it the client machine will send its network hardware address as an ID but this doesn't succeed because the same hardware address is, of course, in use by the other two configurations for the same network device and therefore isn't unique on the network. The client and DHCP server both report that the DHCP negotiation was successful. The computer can be pinged at all three IP addresses and also it can ping other local machines and computers in the wider world. DNS seems to work. I didn't try getting clever with network time protocol since the computer happens to be a time server for the local network and I didn't dare do anything that might mess that up.

Caveats

  1. The computer in question didn't actually move anywhere during this experiment. It is possible therefore that what appears to be a successful configuration might not work quite so well if the computer could be physically relocated to another DHCP server. What I mean is that there might be something I overlooked that means the configuration is working more or less by coincidence.
  2. There is still that pesky “failed” message that is shown when I restart the network using rcnetwork restart.  I just haven't got time to investigate that further right now; it all seems to work but clearly something, somewhere has taken offence at something.
  3. I was working with SuSE professional edition, not the personal edition. I don't have any personal edition to compare with but it occurs to me that the two editions might have different default values set in some of the relevant configuration files.
  4. You might want to do this on a laptop that moves between, say, home and office. I wasn't working on a laptop but judging from information that I have read, laptop computers can have their own personal peculiarities.
  5. Your DHCP server might require that you change some more options in the dhcp file (or set them in the appropriate ifcfg-eth0 file).

Links

SuSE Linux
Creators and purveyors of several distributions of GNU/Linux, including those referred to in this article.
Navigation: (site map) learn linux home pagetechnical articles