Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Anyone know of a script or command to add all the needed information to /etc/interfaces to add a new virtual interface?

It would need to add a new:

iface eth0:4 inet static
address x.y.x.y
netmask 255.255.240.0

and update primary network interfaces.

auto eth0 eth0:1 eth0:2 eth0:3 eth0:4
share|improve this question

2 Answers 2

#!/bin/sh
# Usage: addif alias_nic address netmask
cat >>/etc/network/interfaces <<EOF
auto $1
iface $1 inet static
address $2
netmask $3
EOF

ifup $1
share|improve this answer

I usually do something like this in my interfaces file to bind additional addresses with an interface. I use the up/down to execute the external ip command that is part of the iproute package.

auto eth0
iface eth0 inet static
        address 192.168.32.10
        netmask 255.255.255.0
        network 192.168.32.0
        broadcast 192.168.32.255
        gateway 192.168.32.1
        # bind .25 for bar
        up ip addr add 192.168.32.25/24 brd + dev eth0
        down ip addr del 192.168.32.25/24 brd + dev eth0
        # bind .47 for foo
        up ip addr add 192.168.32.47/24 brd + dev eth0
        down ip addr del 192.168.32.47/24 brd + dev eth0

Of course it is also legal to put something like this in your interfaces file, even though I kinda like the former better.

auto eth0
iface eth0 inet static
        address 192.168.32.10
        netmask 255.255.255.0
        network 192.168.32.0
        broadcast 192.168.32.255
        gateway 192.168.32.1

auto eth0:1
iface eth0:1 inet static
        address 192.168.32.25
        netmask 255.255.255.0

auto eth0:2
iface eth0:2 inet static
        address 192.168.32.47
        netmask 255.255.255.0
share|improve this answer
    
Does this leave that interface as down? I am a bit unfamiliar with the syntax. –  Brian G Aug 21 '09 at 18:21
    
Sorry? I don't understand the question. –  Zoredache Aug 21 '09 at 18:48
    
Did not understand the up on one line and the down on the other. I will RTFM. :D –  Brian G Aug 21 '09 at 18:52
1  
@Brian G: The first example uses the "up" and "down" lines to execute extra commands when the interface is bought up and down, respectively. In this case, when it's bought up extra IPs are added, and when it's bought down they are removed –  Daniel Lawson Aug 22 '09 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.