FreeBSD IP Alias: Setup 2 or More IP address on One NIC

IP aliasing is the process of assigning more than one IP address to a network interface. This is useful for Apache web server virtual hosting or other network servers such as ftp server.

ADVERTISEMENTS

This tutorial explains how to assign one or more IP address to a single network interface under FreeBSD operating system.

It is possible to create network alias or assign 2 ip address to a single NIC under FreeBSD operating system.

Our Sample Setup

  • Interface name: lnc0 – IP : 192.168.1.1/255.255.255.0
  • Interface name: lnc0 alias – IP : 192.168.1.5/255.255.255.255

Netmask must be different otherwise you will get an error ifconfig: ioctl (SIOCAIFADDR): File exists.

Set ip alias using ifconfig command line utility

Login as the root user.

Now, use ifconfig command as follows to set alias:
# ifconfig lnc0 192.168.1.5 netmask 255.255.255.255 alias

Persistent IP Alias Configuration

Edit file /etc/rc.conf to setup persistent ip aliasing option by appending following configuration, so that next time FreeBSD comes up (after restart/shutdown) it will create alias for you:
ifconfig_lnc0_alias0="192.168.1.5 netmask 255.255.255.255"

Finally, Restart FreeBSD network service using network restart script:
# /etc/rc.d/netif restart && /etc/rc.d/routing restart
Display alias and real ip using ifconfig lnc0 command:
# ifconfig lnc0
Sample output:

lnc0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 1500
inet6 fe80::20c:29ff:fe01:ddbd%lnc0 prefixlen 64 scopeid 0x1
inet 192.168.1.2 netmask 0xffffff00 broadcast 192.168.1.255
inet 192.168.1.5 netmask 0xffff

Sample /etc/rc.conf file with IP aliasing

Here is my /etc/rc.conf file with em0 and em1 interface with 5 aliases:

hostname="sun.nixcraft.in"
# default vsnl router interface
defaultrouter="202.54.1.200"
# Force 10Mbps for both public and private LAN interace
ifconfig_em0="inet 10.20.110.2 netmask 255.255.255.192 media 10baseT/UTP mediaopt full-duplex"
ifconfig_em1="inet 202.52.1.2 netmask 255.255.255.248 media 10baseT/UTP mediaopt full-duplex"
ifconfig_em0_alias0="inet 10.20.1.226 netmask 255.255.255.0"
ifconfig_em0_alias1="inet 10.20.1.227 netmask 255.255.255.0"
ifconfig_em0_alias2="inet 10.20.1.228 netmask 255.255.255.0"
ifconfig_em0_alias3="inet 10.20.1.229 netmask 255.255.255.0"
ifconfig_em0_alias4="inet 10.20.1.230 netmask 255.255.255.0"
# static routing configuration
static_routes="lan0"
route_lan0="-net 10.0.0.0/8 10.20.110.1"

Further readings:

  • Read ifconfig man page.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
18 comments… add one
  • girish Dec 26, 2007 @ 8:23

    i want to configure in one networkcard 2 ip adress

  • Raju May 3, 2008 @ 11:02

    Hi,1st 0f all u r explaining 2 ip alias nd this proccess is very lenthy .i create 2 multiple NIC alaises like this;:-go t 1st network-scripts nd type dis command “cp ifcfg-eth0 ifcfg-eth0:1”.
    nd put in ur ip nd netmask as u like.very easy process.u can tell me where m i wrong nd ur daily blog reader.
    Regards
    Raju Dutta Tiwari

  • 🐧 nixCraft May 3, 2008 @ 11:33

    Raju,

    This is tutorial about FreeBSD and not about Red Hat Linux. Your instructions only applies to Linux and not to FreeBSD.

  • Mark Jan 2, 2009 @ 23:12

    Great Article, thanks, really helped me out!

  • andrew caluyo Jan 29, 2009 @ 8:08

    Hi, i was just wondering… i know that it is possible to have i.p. address in one nic but the question is, is it possible to have two i.p address but two different subnet(s) in one nic? i’d like to setup an alias outside the range of the subnet of the parent i.p. I’ve read in other forums that you should have the subnet for both i.p’s but is there any other way to do this? hope you can help me with the problem. tnx!

  • bob Feb 3, 2009 @ 23:27

    You can definitely have two IP’s from multiple subnet’s on one NIC as long as they are both within the same layer 3 network..

  • schursin May 21, 2009 @ 13:50

    Tnx, really helped me out!

  • samuel May 25, 2009 @ 14:09

    Folks,

    I’ve a question regarding the IP alias. One of customer uses a Server with many IP aliases. When they intitiate a ping or other, TCP test applicatiom this server, @IP source is not the same all the time. It means it can be one of these IP Aliases!

    To fix this issue, they’ve added a static IP route to a specific IP Public IP host and an arp static between this remote host and the route’s local mac. To recap:

    Server1—-RTR—Internet

    IPs ser1: 1.1.1.1,.1.1.1.2,.1.1.1.3 (/24)
    Rtr IP: 1.1.1.4/24

    Internet Host: x.x.x..x/32 (any Public IP)

    We need to open up a connection to this host using the same IP source (on the rtr, we just have a specific nat rule for this IP source, let;s say 1.1.1.2)

    But it doesn’t work cause sometime, Source IP is 1.1.1.1 or 1.1.1.3…

    so My customer added the following settings to reach x.x.x.x/32

    arp -s x.x.x.x/32 rtr’s mac
    and
    static route x.x.x.x/32 trough 1.1.1.2 (local IP)

    I really need some explanation. When we remove the arp, it does’nt work anymore.

    IP Routing works welll.

    tks a lot
    I appreciate
    Sam

    • jonny Sep 21, 2015 @ 16:51

      install tcpxd or similar

  • samuel May 25, 2009 @ 14:14

    the ip route, i apparently find out the reason, just to force the traffic to use this specific IP source to reach this public host. But what about the remote arp static? the only time i can see this kind of arp, it’s when we have proxy arp enabled on the router, and we want to play with the same IP subnet public or private through this one.

  • Rooger Jul 14, 2009 @ 13:59

    Good, informative article,
    Poor RAJU. hehehehehe

    rooger

  • nabi Jul 16, 2009 @ 11:02

    ihave working same time two ip in one nic card

  • Jim Sep 6, 2009 @ 17:09

    Thank you for the excellent instructions.

  • shadowbq Nov 23, 2010 @ 15:23

    “ifconfig_lnc0_alias0=”192.168.1.5 netmask 255.255.255.255″”

    should read

    ifconfig_lnc0_alias0=”inet 192.168.1.5 netmask 255.255.255.255″

  • Gregory Feb 8, 2011 @ 22:05

    Thank you very much for posting these instructions! Highly appreciated
    Gregory, ART by LENA

  • Bill Jan 18, 2012 @ 18:49

    Thanks for the helpful tutorial. One addition: the alias numbers must start at 0 and be consecutive numbers. If you skip a number FreeBSD will stop at the last consecutive number.

  • anchous Aug 30, 2012 @ 15:16

    Strange thing coz i use both mask for up aliases: 255.255.255.248 & 255.255.255.255 and both work properly

  • Nikhil Aug 20, 2014 @ 15:54

    hi,
    great info.
    I am trying to configure two network interfaces (two cards) on IPv6. I can see two interfaces up and active in ifconfig. Problem is that I can ping only singel ipv6 address (i.e for onboard card). I am not able to ping the other interface’s IP address (2’nd nic/external card)
    I played with rc.conf mentioned both interfaces with ip address and prefix length
    Any idea/pointers about the issue
    Thakns

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.