Link to home
Start Free TrialLog in
Avatar of MikaelEriksson
MikaelEriksson

asked on

Add multiple alias Ethernet interfaces

I need to create 126 alias interfaces of eth1 (eth1:1 - eth1:126).
I know how to do this in the GUI and manually by creating /etc/sysconfig/network-scripts/ifcfg-eth1:1, ifcfg-eth1:2.....

Created a shell script, very simple because I'm not a good at scripting.
---
#!/bin/bach
for ((i = 2 ; i <=126; i++ ))
do
  cp /etc/sysconfig/network-scripts/ifcfg-eth1:1 /etc/sysconfig/network-scripts/ifcfg-eth1:$i
done
---
The script works fine and creates all the files I need. But, I dont want to manually edit all the files to set the Interface name and IP.

Is there a quicker way of creating alias interfaces or could someone help me to create a script that changes the interface and IP automatically?

Also, I tried the use "ifconfig eth1:2 192.168.0.100 netmask 255.255.255.128 up"
Works fine until I restart the server, then all alias interfaces are gone.

Need a quick answer to this one!
Avatar of liddler
liddler
Flag of Ireland image

create a template file somethink like
/tmp/template
containing the ip address info you need, put with the IP address 192.168.0.XXX
then instead of the copy, do something like:
sed 's/XXX/$i' /tmp/template > /etc/sysconfig/network-scripts/ifcfg-eth1:$i

haven't tested it, but that should do you
What Linux are you using in debian there is a file /etc/network/interfaces which contains a list of all interfaces and whether or not to start them at boot I assume other Linux vendors will have similar files.
Avatar of MikaelEriksson
MikaelEriksson

ASKER

Thanks for the input.
I can't get "sed 's/XXX/$i' /tmp/template > /etc/sysconfig/network-scripts/ifcfg-eth1:$i" to work from the script.
It will replace XXX but not with the number, it's replaced by $i instead.
ASKER CERTIFIED SOLUTION
Avatar of liddler
liddler
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm sure you are right. I have already done the job manually.
It was needed to be done really quick and instead of learning to script I was learning emacs :-)

Thanks anyway!

Ps. liddler, I'll use your tip next time and give you the credit for helping me.
try to add some script in your rc.local
$/sbin/ifconfig eth1:1 <ip address> netmask <your subnetmask> up
.
.
.
$/sbin.ifconfig eth1:n <ip address> netmask <your subnetmask> up

#then add route for each device syntax.
$route add -host <ip address> dev eth1:<youralias>
.
.
.