Bind multiple IP addresses to a single Nic for multi-network segment access, and bind multiple NICs to a single IP address for Load Balancing

Source: Internet
Author: User
Today, we have encountered the problem of implementing multi-network segments in Linux. In the past, we only had to pay attention to how to modify the Linux route. When using the 2003 system, we often access multiple network segments with a single Nic. The premise is to bind an IP address for each CIDR block first. Similarly, if we need to enable the Linux Nic to access multiple network segments, we must first bind multiple IP addresses to a single Nic. See the following implementation:
Here I will use my own Redhat Enterprise Linux as an example.
Assume that the NIC to be bound with multiple IP addresses is eth0. First, let's look at the configuration information and system route of the original NIC:
[Root @ test network-Scripts] # ifcfg-eth0 more
Device = eth0
Bootproto = static
Broadcast = 172.16.1.255
Hwaddr = 00: 0C: 29: A2: 0C: 32
Ipaddr = 172.16.1.55
Netmask = 255.255.255.0
Network = 172.16.1.0
Onboot = Yes
Type = Ethernet

[Root @ test network-Scripts] # route-n
Kernel IP routing table
Destination gateway genmask flags metric ref use
Iface
172.16.1.0 0.0.0.0 255.255.255.0 u 0 0 0
Eth0
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0
Eth0
0.0.0.0 172.16.1.1 0.0.0.0 ug 0 0 0
Eth0

Create a file named ifcfg-eth0: 0 in the/etc/sysconfig/network-scripts directory with the same content as the original ifcfg-eth0, but be sure to modify it for your own purposes, the following is the configuration information of the original NIC: We can also simply copy the original ifcfg-eth0 and rename the ifcfg-eth0: 1
[Root @ test network-Scripts] # cp ifcfg-eth0 ifcfg-eth0: 1
And modify the content as follows:
[Root @ test network-Scripts] # More ifcfg-eth0: 1
Device = eth0: 1
Bootproto = static
Broadcast = 192.168.140.255
Hwaddr = 00: 0C: 29: A2: 0C: 32
Ipaddr = 192.168.140.242
Netmask = 255.255.255.0
Network = 192.168.140.0
Onboot = Yes
Type = Ethernet
The device is the device name. If the device name is not modified after the copy, the NIC will be bound to the IP address in the eth0: 1 file.
Ipaddr is the IP address of this device. Broadcast is the broadcast address, netmask is the subnet mask, and onboot
Indicates that the network interface is automatically started when the system starts. If the IP address is the same, the network interface will be automatically down, that is, onboot = No.
[Root @ test network-Scripts] # service network restart
// After the configuration is complete, restart the network service to connect to the new network segment.
[Root @ test network-Scripts] # route-n
Kernel IP routing table
Destination gateway genmask flags metric ref use
Iface
172.16.1.0 0.0.0.0 255.255.255.0 u 0 0 0
Eth0
192.168.140.0 0.0.0.0 255.255.255.0 u 0 0 0
Eth0 // route automatically added after binding an IP address
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0
Eth0
0.0.0.0 172.16.1.1 0.0.0.0 ug 0 0 0
Eth0
[Root @ test network-Scripts] # Ping 192.168.140.1
// And the corresponding network segment can communicate normally.
Ping 192.168.140.1 (192.168.140.1) 56 (84) bytes of data.
64 bytes from 192.168.140.1: icmp_seq = 0 TTL = 255 time = 3.75 MS

To bind one more IP address, you only need to add the file name to the eth0: X in the device in the file. Linux supports up to 255 IP aliases.

Expand the above content: Here I am using the route command to add a static route so that the machine can access other network segments: Note: in my environment, 192.168.140.1 is the IP address of the VLAN on the layer-3 switch, and the routing is enabled on the layer-3 Switch. Before manually adding a static route, we cannot access the CIDR block other than 192.168.140.1. Here we add a route
[Root @ test network-Scripts] # route add-net 192.168.0.0/16 GW 192.168.140.1
[Root @ test network-Scripts] # route-n
Kernel IP routing table
Destination gateway genmask flags metric ref use
Iface
172.16.1.0 0.0.0.0 255.255.255.0 u 0 0 0
Eth0
192.168.140.0 0.0.0.0 255.255.255.0 u 0 0 0
Eth0
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0
Eth0
192.168.0.0 192.168.140.1 255.255.0.0 ug 0 0 0
Eth0
0.0.0.0 172.16.1.1 0.0.0.0 ug 0 0 0
Eth0
[Root @ test network-Scripts] # Ping 192.168.200.1
// Here we can see that after adding a static route, we can immediately access other network segments through layer-3 switching.
Ping 192.168.200.1 (192.168.200.1) 56 (84) bytes of data.
64 bytes from 192.168.200.1: icmp_seq = 0 TTL = 63 time = 3.77 MS

If the route command parameters are not well remembered, you can use Route
-- Help. For linuix users, this is the most direct and best help in any situation. Of course, we do not want to manually add a route after the computer is restarted. We can write this command to the system startup script/etc/rc. d/rc. Local.

It is widely used to bind multiple IP addresses to a single Nic.
Ifconfig eth0: 1 192.168.140.242 broadcast 192.168.140.255 netmask
255.255.255.0
You can also add the preceding command to the startup script/etc/rc. d/rc. Local.

How to share a single IP address with multiple NICs
 
You can use multiple NICs to create a network card with the same IP address. This technology already exists in Cisco and sun, called trunking and
Etherchannel technology. In Linux, this technology is called bonding. Because bonding is included in kernel 2.4.x, you only need to set
Bonding driver
Select support. Then, recompile the core, restart the computer, and execute the following command:
Ismod Bonding
Ifconfig eth0 down
Ifconfig eth1 down
Ifconfig bond0 IPaddress
Ifenslave bond0 eth0
Ifenslave bond0 eth1
Now the two NICs work like one, which can improve data transmission between cluster nodes.
You 'd better write these statements as a script, and then use/etc/rc. d/rc. Local to make the statement take effect immediately after it is started.
Bonding is a good choice for Servers. When there is no Gigabit Nic, use two or three 100 m NICs
Bonding can greatly increase the bandwidth between the server and the switch. However, you need to set the connection bonding on the vswitch.
The two interfaces of the network adapter are mapped to the same Virtual Interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.