Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    How to create a virtual NIC

    Hello. I'm a beginer in linux. i have a Suse 9 Enterprice Linux mounted on a server with 2 phisical nics. I want to create some virtual nics on one of those. i wanna know how to do it and how many i'm able to create maximum on one of the nic's. Thx and i'm waiting 4 an answer.

  2. #2
    Just Joined!
    Join Date
    Jun 2008
    Memphis, TN
    I've never done this, but here is something I found:

    Say hello to howie, a Linux box at home on the (private, or non-routing) 172.16.0.xx network. That's the address associated with his interface eth0, of course. One day I needed to add a new virtual interface on a different network. It turns out that it's quite easy. One example of a virtual interface is eth0:1 and here's how to create it:

    howie# ifconfig eth0:1 netmask

    That's all it takes! Let's check to make sure it took:

    howie# ifconfig -a

    lo Link encap:Local Loopback
    inet addr: Bcast: Mask:
    RX packets:7961 errors:0 dropped:0 overruns:0
    TX packets:7961 errors:0 dropped:0 overruns:0

    eth0 Link encap:Ethernet HWaddr 00:80:AD:A8:35:A1
    inet addr: Bcast: Mask:
    RX packets:268557 errors:0 dropped:0 overruns:0
    TX packets:18870 errors:0 dropped:0 overruns:0
    Interrupt:5 Base address:0x300

    eth0:1 Link encap:Ethernet HWaddr 00:80:AD:A8:35:A1
    inet addr: Bcast: Mask:
    UP RUNNING MTU:1500 Metric:1
    RX packets:2762 errors:0 dropped:0 overruns:0
    TX packets:4761 errors:0 dropped:0 overruns:0

    Cute, huh? You can (and should!) also give this new address its own name in the /etc/hosts file: howie howitzer

    To make this permanent in RedHat or Mandriva, look in the directory /etc/sysconfig/network-scripts -- you'll see a file called ifcfg-eth0. Copy that file and edit it to create a new ifcfg-eth0:1 (Be sure to edit the contents of the file to give it the right address and netmask, of course.) It will look something like this:

    # cat /etc/sysconfig/network-scripts/ifcfg-eth0:1

    Now your eth0:1 interface will start automatically at reboot, just like eth0 itself. You can have as many of these "ifcfg-" files as you like, within reason.

    In Debian, the file is called "/etc/network/interfaces" and is somewhat simpler:

    auto eth0
    iface eth0 inet static

    auto eth0:1
    iface eth0:1 inet static

    You still have to tell the machine about the new network of which it is now a member. That's the job of the route command:

    howie# route add -net netmask
    howie# route

    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface * U 0 0 21 eth0 * U 0 0 1 eth0:1 * U 0 0 10 lo
    default bazooka UG 0 0 4 eth0

    Note that Linux was smart enough to figure out that the network goes with the new virtual interface. That's a good sign! Because this is a /24 subnet, the network address ends in .0 and the broadcast address ends in .255. The -net parameter on the route command ensures that route will understand that "" is a "network address," i.e. that it refers to all 255 addresses in the 192.168.30.* network.

    We can now talk to any other machine that is physically connected to us and has a 192.168.30.* address. Let's say howie is on a LAN with my notebook, stinger. Assign the address to stinger, and howie and stinger should be able to ping each other.

    So far, so good -- except when I plugged in my notebook, I couldn't ping any machine except howie. Well, why should it? I haven't done anything special to tell Linux that I want it to move packets between the 192.168.30 network and the 172.16.0 network! It would be rather dangerous if it routed packets by default.

    Turning a Linux machine into a router has a lot in common with turning it into a firewall -- in both cases you tell it to forward packets from one interface to another. See the linked article for a better example than the old version that used to appear here -- the one based on ipfwadm.

    ipfwadm was just another way to talk to the kernel's networking code, and tell it that you want it handle some packets differently. iptables does the same task better: it approaches the job more logically, and the kernel module it talks to has been extensively rewritten for stability and performance.
    More virtual interfaces

    You can also use this trick on a machine that has multiple "real" IP addresses assigned to it -- one that is co-located at an Internet Service Provider, for example. Just be sure that when your networking starts up, the script that runs ifconfig for your main interface also brings up the virtual interfaces as well. In a reasonably current RedHat install, creating "ifcfg-eth0:1" will do the right thing.

    What if it's not RedHat? There's still the old-timer's approach: Using 'grep' to find the string 'ifconfig' in some file somewhere in the /etc/rc.d/init.d (or /etc/init.d) directory and then messing with that script. Better to find that script and read it to see where it gets the values it uses, rather than rewriting it.
    Another approach -- Solaris

    To put this in perspective, let's look at a completely different way of doing the same thing.

    You can bring up virtual interfaces on a Solaris box by simply running the ifconfig command, but that has some consequences. For instance, with no entry in /etc/hosts, sendmail will log gethostbyname failed errors in your syslog. Doing things "the Solaris way" avoids that.

    What is the Solaris way? If you grep around as suggested above, eventually you'll find that the networking startup script looks for multiple files named after the network interface cards -- there may be several such cards installed. Typically you'll find ones named le0, ne0, hme0, or qfe0. A small Ultra would have an le0 interface, while a Sun 250 would have an hme0, a 100Mbit Ethernet port. ("hme" stands for "hundred megabit ethernet", sometimes humorously called the "Happy Meal" card.)

    /etc/hostname.hme0 holds the information associated with interface hme0, while /etc/hostname.qfe0 is for qfe0 (the first, or zeroth, interface on a quad fast ethernet card), and so forth.

    The Solaris "hostname" files are simpler than the RedHat ifcfg files: They contain either an IP address or a domain name, and that's all! If it's a domain name, it must also appear in /etc/hosts.

    To set up a virtual interface to be called hme0:2 on a Solaris box using address and the /24 netmask, you would enter these commands on the console as root:

    # ifconfig hme0:2 plumb
    # ifconfig hme0:2 netmask
    # ifconfig hme0:2 up

    But to make that interface permanent, i.e. to make it come up automatically when the system is rebooted, you would want to also create a /etc/hostname.hme0:2 file. Have to call it something, but the exact value doesn't matter, much:

    wombat.localdomain long as we add this matching entry to /etc/hosts -- wombat.localdomain

    To finish, check to see if you need to add an entry to /etc/netmasks --

    The /etc/netmasks file is a little tricky; you don't put in the address of the virtual interface; you create one entry for each network, identified by its network address. This is fine for the usual case, where you would want one server to respond to four or five different addresses all within the same small subnet. They all have the same network address, and one entry in the netmasks file covers all of them.*

    Do I hear a question there in the back of the room? Yes, you can create multiple virtual interfaces on one physical NIC even if they are using different network addresses, but it would be a bottleneck between those networks and all would suffer to some degree because of it. If you're trying to build a bridge or router between two networks, you really want two physical interfaces. That's a bit outside the scope of this article, but you're welcome to ask about it at our webboard (see below).

    *See LAN101 for explanations of the terms network address, gateway address, and broadcast address.

    The point is, don't get locked in to doing things just one way -- if Linux ever changes the way it assigns addresses to a NIC, it won't be the end of the world. If someone offered me an SGI Indigo for free, I wouldn't turn it down just because I might have to turn to Google for help setting it up!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts