Add a secondary ip address in linux

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-346158-id-2131136.html

 

In linux, apart from primary, there are two types of IP addresses:
1. ip alias (sub-interface)
2. secondary ip (secondary ip)

Can be added to a physical network card. alias is added by ifconfig. ifconfig is displayed as a sub-interface such as eth0: 1. secondary ip is supported by ip commands.

Add sub-interface:
# Ifconfig eth0: 1 10.10.56.33/24 up

# Add to memory and take effect immediately. You can view ifconfig and ip addr sh.Create a corresponding sub-interface configuration file under the/etc/sysconfig/network-scripts directory:
Cd
/etc/sysconfig/network-scripts ;
cp ifcfg-eth0 ifcfg-eth0:0 ;
Edit:ifcfg-eth0:0,Modify the device name and IP address: 
DEVICE=eth0:0
IPADDR=10.10.56.33
Linux can contain up to 255 ip addresses alias.



Add secondary ip:
# Ip a add dev eth1 172.16.18.18/24 brd 172.16.18.255
# Ip a sh dev eth1
......
Inet 172.16.18.118/24 brd 172.16.18.255 scope global secondary eth1
# If the ip address of the same network segment is added, the secondary ip address is displayed. If the ip address is not of the same network segment, it is the primary. however, for physical NICs, except for the first configured IP address, the added IP address can be regarded as secondary. the ip ifconfig command added here is invisible.
As far as I know, adding a secondary ip address to the ip address is only added to the memory. I don't know if it can be implemented in the configuration file.

-------------------------

 

Understanding of secondary ip address

 

Set an IP address for the port.
Ip address this port ip address Subnet Mask


In addition, you can set two or more IP addresses of different CIDR blocks on the same port to achieve communication between different CIDR blocks connected to the same LAN. This method is generally used because a CIDR block is not enough for users.


In the port setting status

Ip address this port ip address subnet mask secondary



Note: To implement communication between different network segments connected to the same vro port

Ip redirect


Generally, the Cisco Router does not allow the ip packets that come in from the same port to be sent back to the original port. ip redirect indicates that the ip packets that enter the same end of the router are allowed to be sent back from the original port.


2. If the network contains 0 IP addresses such as 138.0.0.1 or 192.1.0.2, we strongly recommend that you do not use this IP address. To use this IP address, you must set it in global setting mode.


Ip subnet-zero

 

-------------------------------

 

Ip alias and secondary ip address different?

 

I was ignorant. I used to think that ip alias (ip aliase) and secondary ip address (secondary ip address) are a code. Or I don't know whether there is secondary.
Ip address.

Today, when I accidentally browsed linux enterprise cluster, I mentioned the following sentence:
Reference
As of this
Writing, the Linux kernel supports both IP aliases and secondary IP addresses,
Though IP aliases are deprecated in favor of secondary IP
Addresses.

That means that secondary ip address and ip aliase are different concepts. First, Google didn't give me a good answer. I had to explore it myself, so now I can only get the following concepts:

Ip
Alias and secondary ip address are two different implementation methods. They are used to add multiple ip addresses to the same physical Nic in Linux.

Ip
Alias is created and maintained by the ifconfig program, while secondary ip address is created and maintained by the ip program. Ip addr add
The created scondary ip address cannot be seen in ifconfig-a. In turn, the ethX: Y created by ifconfig can be viewed in the ip addr
Show.

Ip alias is familiar to everyone, so let's talk about secondary ip
How to Create address


[Root @ mlsx autostart] # ifconfig
Eth1 Link
Encap: Ethernet HWaddr 00: 13: 77: 00: 7C: C6
Inet addr: 192.168.0.100
Bcast: 192.168.0.255 Mask: 255.255.255.0
UP BROADCAST RUNNING MULTICAST
MTU: 1500 Metric: 1
RX packets: 163438 errors: 2515 dropped: 0 overruns: 0
Frame: 2515
TX packets: 165217 errors: 0 dropped: 0 overruns: 0
Carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 96792997 (92.3 MiB) TX
Bytes: 16356841 (15.5 MiB)

Lo Link encap: Local Loopback
Inet
Addr: 127.0.0.1 Mask: 255.0.0.0
Up loopback running mtu: 16436 Metric: 1
RX
Packets: 561 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 561 errors: 0
Dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX bytes: 119798
(116.9 KiB) TX bytes: 119798 (116.9 KiB)

Eth0 is used by me ifconfig eth0
Down.

[Root @ mlsx autostart] # ip addr sh
2: lo:
<LOOPBACK, UP, 10000> 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 scope host lo
4:
Eth0: <BROADCAST, MULTICAST> mtu 1500 qdisc pfifo_fast qlen
1000
Link/ether 00: 12: f0: 1d: e8: c5 brd ff: ff
Inet
192.168.1.101/24 brd 192.168.1.255 scope global eth0
6: eth1:
<BROADCAST, MULTICAST, UP, 10000> mtu 1500 qdisc pfifo_fast qlen
1000
Link/ether 00: 13: 77: 00: 7c: c6 brd ff: ff
Inet
192.168.0.100/24 brd 192.168.0.255 scope global
Eth1

The down eth0 can be reflected here.

[Root @ mlsx autostart] # ip addr add
192.168.0.102/24 dev eth1
[Root @ mlsx autostart] # ip addr sh eth1
6: eth1:
<BROADCAST, MULTICAST, UP, 10000> mtu 1500 qdisc pfifo_fast qlen
1000
Link/ether 00: 13: 77: 00: 7c: c6 brd ff: ff
Inet
192.168.0.100/24 brd 192.168.0.255 scope global eth1
Inet 192.168.0.102/24
Scope global secondary eth1
[Root @ mlsx autostart] # ping 192.168.0.102
PING
192.168.0.102 (192.168.0.102) 56 (84) bytes of data.
64 bytes from
192.168.0.102: icmp_seq = 1 ttl = 64 time = 0.049 MS
64 bytes from 192.168.0.102:
Icmp_seq = 2 ttl = 64 time = 0.043 MS

Add a secondar ip address through the ip program, and use the ip addr
Show can also be seen, and can ping, but ifconfig can not see.

[Root @ mlsx autostart] # ifconfig
-
Eth1 Link encap: Ethernet HWaddr 00: 13: 77: 00: 7C: C6
Inet
Addr: 192.168.0.100 Bcast: 192.168.0.255 Mask: 255.255.255.0
UP BROADCAST
Running multicast mtu: 1500 Metric: 1
RX packets: 163888 errors: 2515 dropped: 0
Overruns: 0 frame: 2515
TX packets: 165696 errors: 0 dropped: 0 overruns: 0
Carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 96837439 (92.3 MiB) TX
Bytes: 16426734 (15.6 MiB)

Lo Link encap: Local Loopback
Inet
Addr: 127.0.0.1 Mask: 255.0.0.0
Up loopback running mtu: 16436 Metric: 1
RX
Packets: 565 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 565 errors: 0
Dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX bytes: 120134
(117.3 KiB) TX bytes: 120134 (117.3 KiB)

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.