Operating System - Linux
1829420 Members
2387 Online
109991 Solutions
New Discussion

One interface two ipaddress

 
Praveen Bezawada
Respected Contributor

One interface two ipaddress

We are running Linux Redhat Advanced Server 2.1 on HP DL380. We want to setup two ipaddresses on one of the network interface of the linux server so that traffic from the machine can be sent to different networks based on the destination address. It looks like this is possible with "ifaliases". Can someone provide some more details on this, like where to loook for information, what are the issues involved etc..
Any help will be appreciated.

BPK
6 REPLIES 6
Karthik S S
Honored Contributor

Re: One interface two ipaddress

ifconfig eth0:1 x.x.x.x netmask y.y.y.y up

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Steven E. Protter
Exalted Contributor

Re: One interface two ipaddress

note that these two ip addresses need to be compatible.

Its probably a good idea that they be on the same subnet and network though you can experiment prior to going production.

Example

eth0 is 192.168.0.40 mask 255.255.255.0

ifconfig eth0:1 192.168.0.41 netmask 255.255.255.0 up

I believe the up is optional.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Ian Hillier
Frequent Advisor

Re: One interface two ipaddress

Don't forget to add the ifcfg-eth0:1 file in /etc/sysconfig/network-scripts so this interface comes up when you reboot. You can make a copy of the ifcfg-eth0 and change it. Like SEP said, use addresses in the same subnet. I use this on my production web servers and it works like a charm.

Regards,

John
Ameet_HP
Frequent Advisor

Re: One interface two ipaddress

You can not give another Network IP to same Ethernet. For that, you will need to add another card. You can assign different Virtual IP Addresses to one card as shown above, only those should be of same subnet, Mr. Godrej, Vikhroli (E), Mumbai.

-Ameet
Stuart Browne
Honored Contributor

Re: One interface two ipaddress

Actually Ameet, you are totally incorrect.

In modern Linux kernels (anything in the 2.2 range or newer), the interface is never tied to an ip address, and 'IP Aliasing' is not an option, it's a given.

All IP's are 'virtual', only loosely tied to a given interface.

As for mixing and matching subnets, there is absolutely no problem. Linux automatically determines which IP should be used for a given interface, if it is the originating system, otherwise it just forwards it from one interface to the other (given netfilter and /proc/sys/net/ipv4/ip_forward of course).

An example perhaps is best. This is a machine under my desk:

[root@linux: /root]# ip addr list
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 100
link/ether 00:08:02:a4:03:05 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.2/24 brd 192.168.3.255 scope global eth0
inet 192.168.1.2/24 brd 192.168.1.255 scope global eth0:0
inet 192.0.0.1/24 brd 192.0.0.255 scope global eth0:1
2463: eth1: mtu 1500 qdisc pfifo_fast qlen 100
link/ether 00:40:f4:58:97:61 brd ff:ff:ff:ff:ff:ff
inet 203.xx.xxx.22/30 brd 203.xx.xxx.23 scope global eth1
inet 203.yyy.yy.66/30 brd 203.yyy.yy.67 scope global eth1:0
3554: ipsec0: mtu 16260 qdisc pfifo_fast qlen 10
link/ether 00:40:f4:58:97:61 brd ff:ff:ff:ff:ff:ff
inet 203.xx.xxx.22/30 brd 203.xx.xxx.23 scope global ipsec0
3600: ppp0: mtu 1400 qdisc pfifo_fast qlen 3
link/ppp
inet 192.168.8.200 peer 192.168.8.244/32 scope global ppp0

As can be seen from 2/2463 (eth0/eth1), both have at least two IP addresses. Both on different subnet masks (eth1 being my external interfaces, slightly commented out). Neither of which is 'predominant'. It's only tools like 'ifconfig' (which are there for compatability, and 'cause everybody remembers how to use them) that the distinction of aliases etc. still exists in Linux.
One long-haired git at your service...
Jeroen Peereboom
Honored Contributor

Re: One interface two ipaddress

I'd like to add a trivial thing to Stuart's reply. Of course you can have 2 'logical' subnets on one physical cable, but you must define the addresses not only on your own server but also on some other network elements, i.e. another server-, switch- or router-interface such that indeed you have 2 logical subnets on one physical cable.

Maybe that's why Ameet thinks (thought?!) that it was not possible...

JP.