Adding additional ip addresses in FreeBSD – Alternate methods

Traditional way of adding additional ip address in FreeBSD has been to add entries in /etc/rc.conf:

ifconfig_em0_alias0="inet 10.1.30.15 netmask 255.255.255.255"
ifconfig_em0_alias1="inet 10.5.30.16 netmask 255.255.255.255"
ifconfig_em0_alias2="inet 192.168.1.10 netmask 255.255.255.255"
ifconfig_em0_alias3="inet 10.1.30.245 netmask 255.255.255.255"

This works nicely, but the problem lies in the fact that if you miss the numbering in alias, subsequent aliases will not be read by the system. So a listing like

ifconfig_em0_alias0="inet 10.1.30.15 netmask 255.255.255.255"
ifconfig_em0_alias1="inet 10.5.30.16 netmask 255.255.255.255"
ifconfig_em0_alias3="inet 10.1.30.245 netmask 255.255.255.255"

will lead to the last alias ‘10.1.30.245’ to be not read/set by the OS. There exists an alternate way to set up additional ip address which does not include numbering of aliases. All you need to do is create a file /etc/start_if.<interface>, where <interface> is the name of the network interface you want to bind the aliases to. So for example you have an interface em0, the file that needs to be created is /etc/start_if.em0. This file is a shell script (bourne shell) file where you can call the ifconfig command to add the aliases to your nic. An example file could be:

#!/bin/sh
/sbin/ifconfig em0 alias 10.1.50.15 netmask 255.255.255.255
/sbin/ifconfig em0 alias 10.5.30.16 netmask 255.255.255.255
/sbin/ifconfig em0 alias 192.168.1.10 netmask 255.255.255.255
/sbin/ifconfig em0 alias 10.1.30.245 netmask 255.255.255.255

Since this is a standard shell script, you will be able to all sorts of scripting inside (Not tested though, apart from placing comments using # sign). This allows for better administration and scripting for aliases.

There exists another way to set aliases, though I haven’t tested them myself. This goes into /etc/rc.conf file and lets you easily add groups of ip address at a time:

ipv4_addrs_em0="192.168.1.10/32 10.1.30.0/27 10.1.30.245/32"
Advertisement