Network card alias settings

1. What is the network card alias

IP alias is to configure multiple IPs on one physical network card to realize functions like sub-interfaces. From the perspective of network protocol, the lower layer always provides services for the upper layer. As long as the MAC address of a network card corresponds to an IP address of the upper layer, and this logical relationship is reasonable, it is established, and the upper layer does not care what the lower layer is. For example, an IP address can correspond to multiple application layer ports, isn't it the same reason? When linux is used as a DHCP server to assign different IPs to multiple network segments or linux is used as a router, it may be necessary to configure multiple IP addresses on one physical interface.

LINUX can support up to 255 IP subnet cards

2. Working principle

insert image description here

It should be noted here that the network card alias does not provide load balancing and redundancy functions, and its terminal is only worked by a network card.

3. Settings

3.1 Temporary addition, restart invalid

3.1.1 Use the ipconfig command to set the network card alias

Syntax for specifying an alias:

ifconfig eth0:0 192.168.1.100/24 up

或  

ifconfig eth0:0 192.168.1.100  netmask 255.255.255.0 up

You can also not specify an alias. When you add an ip to the same network card multiple times, an alias in the form of :0 and :1 will be automatically added behind it

If there is already an eth5, and the ip is 194.20.20.24, add 2 more ip:

ifconfig eth5  add 194.20.20.5
ifconfig eth5  add 194.20.20.6

Check again with ifconfig and find that eth5:0 and eth5:1 are added

eth5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 194.20.20.24  netmask 255.255.255.0  broadcast 194.20.20.255
        ether xxxxx  txqueuelen 1000  (Ethernet)
        RX packets 614  bytes 50944 (49.7 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 230  bytes 23584 (23.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth5:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 194.20.20.5  netmask 255.255.255.0  broadcast 194.20.20.255
        ether xxxxx  txqueuelen 1000  (Ethernet)

eth5:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 194.20.20.6  netmask 255.255.255.0  broadcast 194.20.20.255
        ether xxxxx  txqueuelen 1000  (Ethernet)

ifconfig deletes the alias, when specifying the alias, use down (note that the down command for the alias of the network card is to delete, and the corresponding network card body is disabled):

ifconfig eth5:0 down

At this time, check through ifconfig -a and find that eth5:0 does not exist

You can also use ifconfig eth5 del 194.20.20.6to delete, that is, when specifying the real body of the network card, carry the target ip, it will automatically find the alias of the network card and the item whose ip is the target value to delete

3.1.2 Use the ip addr command to set the network card alias

ip addr add 194.20.20.1/24 dev eth0                //给网卡网卡新增一个ip, ip 命令在不指定别名时,不会自动添加别名
ip addr add 194.20.20.2/24 dev eth0 label eth0:0   //新增网卡别名
ip addr del 194.20.20.2/24 dev eth0 label eth0:0   //删除网卡别名
ip addr flush dev eth0 label eth0:0

As shown above, use ip addr to add multiple IPs to a network card. If you do not specify an alias, it will be displayed as the same network card name, which will be similar to the following effect:

#ip addr 查看
 inet 194.20.20.132/24 scope global secondary eth0    //原来初次设置的ip,接口是eth0
       valid_lft forever preferred_lft forever
 inet 194.20.20.13/24  scope global secondary eth0    //新增一个ip,接口仍然是eth0
       valid_lft forever preferred_lft forever       
 inet 194.20.20.2/24 brd 194.20.20.255 scope global secondary eth0:0   //新增一个网卡别名eth0:0,方便差查看
       valid_lft forever preferred_lft forever
 

However, this setting is temporary and will not exist after the computer is restarted.

3.2 Permanent addition

Set a permanent device alias

Generate separate interface configuration files for each device alias

1. Close the NetworkManager service (otherwise the result will be wrong)

2. ifcfg-ethX:xxx (command method, remember to use: to separate)

3. Static networking must be used

DEVICE=eth0:0
IPADDR=10.10.10.10
NETMASK=255.0.0.0
ONPARENT=yes

Note: service network restart takes effect

Reference /usr/share/doc/initscripts-*/sysconfig.txt

3.3 view

Use ifconfig to view

And use ping ip to see if it works

reference

Network configuration command
network card alias setting
linux eth0 network card configuration details

Guess you like

Origin blog.csdn.net/m0_45406092/article/details/130067978