GuestbookSign our guestbook ContactGet in touch with the authors ArchiveAll unixwerk articles since 2003
July 28, 2008

Setting IP Aliases under BSD

Contents

FreeBSD

  1. Find the Interface
  2. Set a Temporary IP Alias
  3. Set a Permanent IP Alias
  4. Remove an IP Alias

OpenBSD

  1. Find the Interface
  2. Set an IP Alias
  3. Make the IP Alias Permanent
  4. Remove an IP Alias

FreeBSD

Find the Interface

If you want to put an IP alias on an interface under FreeBSD, first find the interface:

 FreeBSD# ifconfig 
 em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
         options=19b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4>
         ether 00:1c:25:74:af:69
         inet 192.168.1.200 netmask 0xffffff00 broadcast 192.168.1.255
         media: Ethernet autoselect (100baseTX <full-duplex>)
         status: active
 lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 
         inet6 ::1 prefixlen 128 
         inet 127.0.0.1 netmask 0xff000000 

In our case the interface is called em0. We will use this name in all the examples. Replace it with the interface name you find on your system!

Set a Temporary IP Alias

Then set the alias with ifconfig

 FreeBSD# ifconfig em0 192.168.100.200 netmask 255.255.255.0 alias

This sets an IP address 192.168.100.200 to em0. Check again with ifconfig:

 FreeBSD# ifconfig
 em0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
         options=19b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,TSO4>
         ether 00:1c:25:74:af:69
         inet 192.168.1.200 netmask 0xffffff00 broadcast 192.168.1.255
         inet 192.168.100.200 netmask 0xffffff00 broadcast 192.168.100.255
         media: Ethernet autoselect (100baseTX <full-duplex>)
         status: active
 lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2 
         inet6 ::1 prefixlen 128 
         inet 127.0.0.1 netmask 0xff000000 

Now we see two IP addresses on em0. If you want to set an IP address within the same network you would have to set a fake netmask of 255.255.255.255.

Set a Permanent IP Alias

The above alias will be lost after the next reboot. If you need a permanent alias, add the following line to /etc/rc.conf:

 ifconfig_em0_alias0="192.168.100.200 netmask 255.255.255.0"

and restart the network:

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

Remove an IP Alias

To remove the alias (until next reboot, if you made it permanent), just remove the IP address from the interface you put on it earlier:

 FreeBSD# ifconfig em0 192.168.100.200 delete


OpenBSD

Setting an IP alias to an interface with OpenBSD is straight forward:

Find the Interface

First find the interface you want to put an IP alias with ifconfig:

 OpenBSD# ifconfig
 lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 33208
         groups: lo
         inet 127.0.0.1 netmask 0xff000000
         inet6 ::1 prefixlen 128
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
 vr0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
         lladdr 00:40:45:28:89:37
         groups: egress
         media: Ethernet autoselect (100baseTX full-duplex)
         status: active
         inet 192.168.1.199 netmask 0xffffff00 broadcast 192.168.1.255
         inet6 fe80::240:45ff:fe28:8937%vr0 prefixlen 64 scopeid 0x2

From the above output you see that your current IP address is put on vr0. We will use this name in all the examples. Replace it with the interface name you find on your system!

Set an IP Alias

To put a second IP address to the same interface, just enter on the commandline as root:

 OpenBSD# ifconfig vr0 inet alias 192.168.100.199 netmask 255.255.255.0

This sets an IP address 192.168.100.199 to vr0. Check again with ifconfig:

 OpenBSD# ifconfig -A
 lo0: flags=8049 mtu 33208
         groups: lo
         inet 127.0.0.1 netmask 0xff000000
         inet6 ::1 prefixlen 128
         inet6 fe80::1%lo0 prefixlen 64 scopeid 0x4
 vr0: flags=8843 mtu 1500
         lladdr 00:40:45:28:89:37
         groups: egress
         media: Ethernet autoselect (100baseTX full-duplex)
         status: active
         inet 192.168.1.199 netmask 0xffffff00 broadcast 192.168.1.255
         inet6 fe80::240:45ff:fe28:8937%vr0 prefixlen 64 scopeid 0x2
         inet 192.168.100.199 netmask 0xffffff00 broadcast 192.168.100.255

Now we see two IP addresses on vr0. If you want to set an IP address within the same network you would have to set a fake netmask of 255.255.255.255.

Make the IP Alias Permanent

If you use ifconfig to set an IP alias, the alias won't be present after the next reboot. To make the setting permanent, add a line to /etc/hostname.<INTERFACE>:

 OpenBSD# vi /etc/hostname.vr0
 inet 192.168.1.199 255.255.255.0 NONE 
         inet alias 192.168.100.199 255.255.255.0

Remove an IP Alias

You can remove an IP alias with a comand like this:

 OpenBSD# ifconfig vr0 192.168.100.199 delete

This deletes the second IP address from the interface keeping the first.