Assigning multiple IP addresses to a network adapter on CentOS 7

Source: Internet
Author: User

Sometimes you might want to give a NIC multiple addresses. What are you going to do about it? Also buy a network card to assign the address? In a small network, you don't actually have to do this. We can now assign multiple IP addresses to one NIC in Centos/rhel 7. You want to know what to do? OK, follow me, it's not hard.

650) this.width=650; "Src=" http://img.linux.net.cn/data/attachment/album/201503/26/101151p4cwwf5fr9wf4rtv.jpg " height= "420" width= "669"/>

First, let's find the IP address of the NIC. In my CentOS 7 server, I used only one NIC.

Run the following command with root privileges:

IP addr

Example output:

1: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state  unknown     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00     inet 127.0.0.1/8 scope host lo        valid_lft forever preferred_lft forever    inet6 ::1/128  scope host        valid_lft forever preferred_lft  Forever2: enp0s3: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_ Fast state up qlen 1000    link/ether 08:00:27:80:63:19 brd  ff:ff:ff:ff:ff:ff    inet 192.168.1.150/24 brd 192.168.1.255  Scope global enp0s3       valid_lft forever preferred_ Lft forever

As seen above, my network card name is ENP0S3,IP address is 192.168.1.150.

As you know, the configuration file for the NIC is stored in the /etc/sysconfig/network-scripts/ directory. The details of each NIC will be stored in different names, such as ifcfg-enp0s3.

Let's look at the details of the ifcfg-enp0s3 .

Cat/etc/sysconfig/network-scripts/ifcfg-enp0s3

Example output:

Type= "Ethernet" bootproto= "None" defroute= "yes" ipv4_failure_fatal= "no" ipv6init= "yes" ipv6_autoconf= "yes" ipv6_ Defroute= "yes" ipv6_failure_fatal= "no" name= "Enp0s3" uuid= "e9f9caef-cb9e-4a19-aace-767c6ee6f849" ONBOOT= "yes" Hwaddr= "08:00:27:80:63:19" ipaddr0= "192.168.1.150" prefix0= "gateway0=" "192.168.1.1" dns1= "192.168.1.1" ipv6_ peerdns= "yes" ipv6_peerroutes= "yes"

OK, now we'll be assigning multiple addresses in the same subnet.

Edit File /etc/sysconfig/network-scripts/ifcfg-enp0s3:

Vi/etc/sysconfig/network-scripts/ifcfg-enp0s3

Add an additional IP address as follows.

Type= "Ethernet" bootproto= "None" defroute= "yes" ipv4_failure_fatal= "no" ipv6init= "yes" ipv6_autoconf= "yes" ipv6_ Defroute= "yes" ipv6_failure_fatal= "no" name= "Enp0s3" uuid= "933cdc9b-b383-4ddd-b219-5a72c69c9cf0" ONBOOT= "yes" Hwaddr= "08:00:27:3f:ab:68" ipaddr0= "192.168.1.150" ipaddr1= "192.168.1.151" ipaddr2= "192.168.1.152" prefix0= "24" gateway0= "192.168.1.1" dns1= "192.168.1.1" ipv6_peerdns= "yes" ipv6_peerroutes= "yes"

As you can see, I've added two IP addresses:ipaddr1= "192.168.1.151″& ipaddr2=" 192.168.1.152″

Similarly, you can add more IP addresses.

Finally, save and exit the file. Restart the network service for the changes to take effect.

Systemctl Restart Network

Now, let's check if the IP address has been added.

IP addr

Example output:

: lo: <loopback,up,lower_up> mtu 65536 qdisc noqueue state  unknown     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00     inet 127.0.0.1/8 scope host lo        valid_lft forever preferred_lft forever    inet6 ::1/128  scope host        valid_lft forever preferred_lft  Forever2: enp0s3: <broadcast,multicast,up,lower_up> mtu 1500 qdisc pfifo_ Fast state up qlen 1000    link/ether 08:00:27:3f:ab:68 brd  ff:ff:ff:ff:ff:ff    inet 192.168.1.150/24 brd 192.168.1.255  Scope global enp0s3       valid_lft forever preferred_ lft forever    inet 192.168.1.151/24 brd 192.168.1.255 scope global secondary  enp0s3       valid_lft forever preferred_lft forever     inet 192.168.1.152/24 brd 192.168.1.255 scope global secondary  enp0s3       valid_lft forever preferred_lft forever     inet6 fe80::a00:27ff:fe3f:ab68/64 scope link         valid_lft forever preferred_lft forever

As you can see, a single network card already has 3 IP addresses.

Let's ping the new IP address:

Ping-c 4 192.168.1.151

Example output:

  1. PING 192.168.1.151 (192.168.1.151) 56(84) bytes of data.

  2. 64 bytes from 192.168 1.151 : icmp_seq = 1 TTL = 64 time = 0.048 ms

  3. 64 bytes from 192.168 1.151 : icmp_seq = 2 TTL = 64 time = 0.075 ms

  4. 64 bytes from 192.168 1.151 : icmp_seq = 3 TTL = 64 time = 0.077 ms

  5. 64 bytes from 192.168.1.151: icmp_seq=4 ttl=64 time=0.077 ms


  6. --- 192.168.1.151 ping statistics ---

  7. 4 packets transmitted, 4 received, 0% packet loss, time 2999ms

  8. rtt min/avg/max/mdev = 0.048/0.069/0.077/0.013 ms

Ping-c 4 192.168.1.152

Example output:

  1. PING 192.168.1.152 (192.168.1.152) 56(84) bytes of data.

  2. 64 bytes from 192.168 1.152 : icmp_seq = 1 TTL = 64 time = 0.034 ms

  3. 64 bytes from 192.168 1.152 : icmp_seq = 2 TTL = 64 time = 0.075 ms

  4. 64 bytes from 192.168 1.152 : icmp_seq = 3 TTL = 64 time = 0.073 ms

  5. 64 bytes from 192.168.1.152: icmp_seq=4 ttl=64 time=0.075 ms


  6. --- 192.168.1.152 ping statistics ---

  7. 4 packets transmitted, 4 received, 0% packet loss, time 2999ms

  8. rtt min/avg/max/mdev = 0.034/0.064/0.075/0.018 ms

If you want to use a different subnet , you have to change prefix0=24 into different subnets, such as prefix1=16.

For example, I want to add a Class A address (* such as 10.0.0.1) to my network card.

Type= "Ethernet" bootproto= "None" defroute= "yes" ipv4_failure_fatal= "no" ipv6init= "yes" ipv6_autoconf= "yes" ipv6_ Defroute= "yes" ipv6_failure_fatal= "no" name= "Enp0s3" uuid= "933cdc9b-b383-4ddd-b219-5a72c69c9cf0" ONBOOT= "yes" Hwaddr= "08:00:27:3f:ab:68" ipaddr0= "192.168.1.150" ipaddr1= "192.168.1.151" ipaddr2= "192.168.1.152" ipaddr3= " 10.0.0.1 "prefix0=" "prefix1=16gateway0=" 192.168.1.1 "dns1=" 192.168.1.1 "ipv6_peerdns=" yes "ipv6_peerroutes=" yes "

You can see that I have added a Class A address (10.0.0.1) and the prefix is 16.

Save and exit the file. Restart the network service, and then ping the new address:

Ping-c 4 10.0.0.1

Example output:

  1. ping 10.0 0.1 ( 10.0 0.1 56 ( 84 ) bytes of data

  2. 64 bytes from 10.0 0.1 : icmp_seq = 1 TTL = 64 time = 0.097 ms

  3. 64 bytes from 10.0 0.1 : icmp_seq = 2 TTL = 64 time = 0.073 ms

  4. 64 bytes from 10.0 0.1 : icmp_seq = 3 TTL = 64 time = 0.074 ms

  5. 64 bytes from 10.0.0.1: icmp_seq=4 ttl=64 time=0.075 ms


  6. --- 10.0.0.1 ping statistics ---

  7. 4 packets transmitted, 4 received, 0% packet loss, time 3000ms

  8. rtt min/avg/max/mdev = 0.073/0.079/0.097/0.014 ms

In a similar way, you can add different gateways.

That's it.

via:http://www.unixmen.com/linux-basics-assign-multiple-ip-addresses-single-network-interface-card-centos-7/


Assigning multiple IP addresses to a network adapter on CentOS 7

Related Article

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.