How to configure multiple IP addresses for the same network card

When developing embedded Linux devices, I wonder if you have ever encountered this requirement: the device itself has only one Ethernet port, but the requirement is that this network port should be used as both a debugging port and a business data communication port . Moreover, the two uses have different requirements for IP addresses: when used as a debugging port, the IP address is fixed; when used as a business communication port, the IP address is dynamically assigned. How to realize this requirement?

In fact, it is very simple. The Linux system supports network card alias configuration, which means setting different names for the same network card. For example, for eth0, we can set aliases: eth0:0, eth0:1, eth0:2, etc.

The method of configuring the alias of the network card is as follows:

  1. Configure via ifconfig

ifconfig can configure the IP address of any network card, such as Ethernet, wifi, 4G modem, etc.

ifconfig eth1 192.168.2.88 netmask 255.255.255.0
ifconfig eth1:0 192.168.0.66 netmask 255.255.255.0

Here, configure an alias for eth1, eth1:0 and an IP address. The effect is as shown in the figure: 2. Configure the interfaces file
Insert image description here
through /etc/network/interfaces , configure eth1 to obtain IP through dhcp, and eth1:0 as static IP.
Insert image description here

Guess you like

Origin blog.csdn.net/linux_embedded/article/details/128616407