Today, let's review how to configure multiple IP addresses for a single Nic in linux. One is to use commands for direct configuration, and the other is to write the configuration file by yourself, now let's talk about it one by one.
1. Use commands for configuration
This is simple. First, check the IP address of the current machine. The command is as follows:
- ifconfig
- eth0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
- inet addr:192.168.1.55 Bcast:192.168.3.255 Mask:255.255.252.0
- inet6 addr: fe80::219:d1ff:fe24:2aec/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
We can see that the IP address of the current machine is 1.55, so we will configure a 1.56 (Be sure not to conflict with the address in the LAN), run the following command:
- ifconfig eth0:0 192.168.1.56 netmask 255.255.252.0
- ifconfig
- eth0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
- inet addr:192.168.1.55 Bcast:192.168.3.255 Mask:255.255.252.0
- inet6 addr: fe80::219:d1ff:fe24:2aec/64 Scope:Link
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
- eth0:0 Link encap:Ethernet HWaddr 00:19:D1:24:2A:EC
- inet addr:192.168.1.56 Bcast:192.168.3.255 Mask:255.255.252.0
- UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
The first line of command is to configure an IP address, and then use the ifconfig command to view the two IP addresses. Then ping the test to see if it is successful:
- ping 192.168.1.56
- PING 192.168.1.56 (192.168.1.56)56(84) bytes of data.
- 64 bytes from 192.168.1.56: icmp_seq=1ttl=64time=0.045 ms
- 64 bytes from 192.168.1.56: icmp_seq=2ttl=64time=0.045 ms
- 64 bytes from 192.168.1.56: icmp_seq=3ttl=64time=0.043 ms
- ping 192.168.1.55
- PING 192.168.1.55 (192.168.1.55)56(84) bytes of data.
- 64 bytes from 192.168.1.55: icmp_seq=1ttl=64time=0.030 ms
- 64 bytes from 192.168.1.55: icmp_seq=2ttl=64time=0.022 ms
- 64 bytes from 192.168.1.55: icmp_seq=3ttl=64time=0.022 ms
2. Use the configuration file for configuration
The nic ip configuration file is in/etc/sysconfig/network-scripts/, and the files are ehtx or ethx: x, respectively. Run the following command:
- cd/etc/sysconfig/network-scripts/
- cp ifcfg-eth0 ifcfg-eth0:1
In this way, we simply copy the configuration file of the current network. Then we open the copied file and change the IP address to the desired IP address, such as 192.168.1.57! Vi file, the content is as follows:
- DEVICE=eth0
- BOOTPROTO=none
- HWADDR=00:19:D1:24:2A:EC
- ONBOOT=yes
- DHCP_HOSTNAME=zhongqg.localdomain
- IPADDR=192.168.1.55
- NETMASK=255.255.252.0
- GATEWAY=192.168.0.1
- TYPE=Ethernet
- USERCTL=no
- IPV6INIT=no
- PEERDNS=yes
Then we only need to change the IPADDR to our IP address, and then save and exit and start the configuration file:
- ifup eth0:1
In either of these methods, we can easily configure multiple IP addresses in linux.