Two IP Address on a single network card on FreeBSD

I needed to get one of my servers to respond to another IP Address. I have mostly used Linux and not FreeBSD. Linux is quite easy, but I have never done anything like this on FreeBSD, so here is how I got it work properly.

First of all you need to figure out what your IP Addresses are. If you are using private address (ie.,, etc) you can pretty much use whatever you want. If you are like me and have statically assigned IP addreses that are world viewable, then you will need to plan this out a little more. In my instance, I have a block of 16 address. They are thru I have already assigned my webserver to be, is assigned to another server so I wanted to assign as the secondary IP address.

To do this, it is quite simple, simply login as root to the server, you will need to edid the /etc/rc.conf file using your favorite editor. I personally use vim (or vi depending on which machine I am using) so we enter:

vi /etc/rc.conf

This command gets us into the file and allows us to make changes to it. The next thing that you will need to do is find the line that is similar to ifconfig_em0=”inet netmask″ this line will differ on each machine simply because not all machines have the same ethernet cards and the IP subnet mask. Below this line you will need to add the following line:

ifconfig_em0_alias0=" netmask"

This tells the networking system on FreeBSD to add a secondary IP address to the network interface. The subnet mask MUST mask out everything. Why? Well, in order for there to not be any conflicts between the two IP addresses one of them needs to only reference the single address. (this is because they are on the same subnet) This can turn into a big mess, so trust me, you need it this way.

Now, save your work on the file. (:wq if you are using vi or vim) Finally you are ready to restart the networking and routing systems to allow the IP address to actually function. We can do this by using the following command:

/etc/rc.d/netif restart && /etc/rc.d/routing restart

This forces the network interface to restart and pickup the additional IP address, it also restarts the local routing service, so that the system knows how to send packets out onto the network.

Now you can run the ifconfig command. You should get something that looks like the following:

sirius# ifconfig
em0: flags=8843 metric 0 mtu 1500
ether 00:30:48:93:11:11
inet netmask 0xfffffff0 broadcast
inet netmask 0xffffffff broadcast
media: Ethernet autoselect (100baseTX )
status: active
lo0: flags=8049 metric 0 mtu 16384
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
inet6 ::1 prefixlen 128
inet netmask 0xff000000

One thing to be careful about though, FreeBSD starts at zero and counts up, so you can add as many as you want, be remember that if you skip a number, any of the aliases above that will be ignored. Also make sure that you are careful and don’t put in the wrong information or the wrong interface, this will cause the networking to stop functioning. If this happens, and you machine is on the other side of the world in some datacenter, you will be in big trouble because the networking won’t return.

Good luck!

Leave a Reply

Switch to our mobile site