linux下一块网卡设置多个ip地址
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux下一块网卡设置多个ip地址相关的知识,希望对你有一定的参考价值。
无论是在实际的生产坏境中,还是平时的练习中, 一个网卡设置多个ip地址是非常常见的,也是非常有用的。
我们以centos6.8系统为列, 在一块网卡上设置多个ip地址。
1>. 给一块网卡设置多个ip地址这个功能可能会受到NetworkManager服务的影响,首先关闭NetworkManager服务,并且开机自启也关闭。
[[email protected] network-scripts]# service NetworkManager stop Stopping NetworkManager daemon: [ OK ] [[email protected] network-scripts]# chkconfig NetworkManager off
2>. 查看当前系统的ip地址和网卡的设备名。
[[email protected] network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.138 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32cc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:3964 errors:0 dropped:0 overruns:0 frame:0 TX packets:1193 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:328092 (320.4 KiB) TX bytes:167348 (163.4 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:B3:32:D6 inet addr:192.168.203.131 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32d6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4083 errors:0 dropped:0 overruns:0 frame:0 TX packets:626 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:323667 (316.0 KiB) TX bytes:92256 (90.0 KiB)
很显然, 机子上有两块网卡, 一块命名为eth0, 另外一块命名为eth1。
3>.eth0网卡在原有基础之上,再去添加一个ip地址。
修改配置文件ifcfg-eth0:1
[[email protected] network-scripts]# vim ifcfg-eth0:1 [[email protected] network-scripts]# cat ifcfg-eth0:1 DEVICE=eth0:1 HWADDR=00:0C:29:B3:32:CC TYPE=Ethernet UUID=a7f94e4f-1f87-439f-a044-9888cc8ddc7c ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.203.11 NETMASK=255.255.255.0 GATEWAY=192.168.203.2 DNS1=192.168.203.2
4>. 重启network服务
[[email protected] network-scripts]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining IP information for eth0... done. Determining if ip address 192.168.203.11 is already in use for device eth0... [ OK ]
5>. 查看系统ip地址
[[email protected] network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.138 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32cc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5027 errors:0 dropped:0 overruns:0 frame:0 TX packets:1536 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:418869 (409.0 KiB) TX bytes:224202 (218.9 KiB) eth0:1 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.11 Bcast:192.168.203.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0第二个ip地址设置成功了。
如何给网卡绑定两个ip地址, 其中一个地址为自动分配的, 另外一个地址是静态分配的。
无论一块网卡上绑定多少个地址, 但是只能动态绑定一个动态地址,并且动态分配的地址必须是主网络配置文件,而不能是别名配置文件。 别名配置文件中必须是静态分配ip地址。
1>. 我们先将别名网络配置文件设置为动态(dhcp)分配ip地址,而主网络配置文件设置为一个静态的ip地址。
[[email protected] network-scripts]# cat ifcfg-eth0 DEVICE=eth0 HWADDR=00:0C:29:B3:32:CC TYPE=Ethernet UUID=a7f94e4f-1f87-439f-a044-9888cc8ddc7c ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.203.11 NETMASK=255.255.255.0 GATEWAY=192.168.203.2 DNS1=192.168.203.2 [[email protected] network-scripts]# cat ifcfg-eth0:1 DEVICE=eth0:1 HWADDR=00:0C:29:B3:32:CC TYPE=Ethernet UUID=a7f94e4f-1f87-439f-a044-9888cc8ddc7c ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp
2>. 重启网络服务
[[email protected] network-scripts]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining if ip address 192.168.203.11 is already in use for device eth0... error in ifcfg-eth0:1: didn‘t specify device or ipaddr [ OK ]
3>. 查看eth0的分配到的ip地址
[[email protected] network-scripts]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.11 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32cc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:6408 errors:0 dropped:0 overruns:0 frame:0 TX packets:2024 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:535222 (522.6 KiB) TX bytes:298666 (291.6 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:B3:32:D6 inet addr:192.168.203.131 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32d6/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5589 errors:0 dropped:0 overruns:0 frame:0 TX packets:656 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:438975 (428.6 KiB) TX bytes:97234 (94.9 KiB)
很显然eth0别名网络的配置文件并没有起效。
4>. 再将eth0主配置文件修改为动态获取ip地址,而别名配置文件修改为静态分配的ip地址
[[email protected] network-scripts]# cat ifcfg-eth0 DEVICE=eth0 HWADDR=00:0C:29:B3:32:CC TYPE=Ethernet UUID=a7f94e4f-1f87-439f-a044-9888cc8ddc7c ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=dhcp [[email protected] network-scripts]# cat ifcfg-eth0:1 DEVICE=eth0:1 HWADDR=00:0C:29:B3:32:CC TYPE=Ethernet UUID=a7f94e4f-1f87-439f-a044-9888cc8ddc7c ONBOOT=yes NM_CONTROLLED=yes BOOTPROTO=static IPADDR=192.168.203.10 PREFIX=24 GATEWAY=192.168.203.2
5>. 重启网络服务
[[email protected] network-scripts]# service network restart Shutting down interface eth0: [ OK ] Shutting down loopback interface: [ OK ] Bringing up loopback interface: [ OK ] Bringing up interface eth0: Determining IP information for eth0... done. Determining if ip address 192.168.203.10 is already in use for device eth0... [ OK ]
6>. 查看ip地址
[[email protected] network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.138 Bcast:192.168.203.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:feb3:32cc/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:7840 errors:0 dropped:0 overruns:0 frame:0 TX packets:2479 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:659719 (644.2 KiB) TX bytes:369320 (360.6 KiB) eth0:1 Link encap:Ethernet HWaddr 00:0C:29:B3:32:CC inet addr:192.168.203.10 Bcast:192.168.203.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0的第二个ip地址修改成功。
以上是关于linux下一块网卡设置多个ip地址的主要内容,如果未能解决你的问题,请参考以下文章