linux下一塊網卡設置多個ip地址

不管是在實際的生產壞境中,仍是平時的練習中, 一個網卡設置多個ip地址是很是常見的,也是很是有用的。vim


咱們以centos6.8系統爲列, 在一塊網卡上設置多個ip地址。centos


1>. 給一塊網卡設置多個ip地址這個功能可能會受到NetworkManager服務的影響,首先關閉NetworkManager服務,而且開機自啓也關閉。bash

[root@centos6 network-scripts]# service NetworkManager stop
Stopping NetworkManager daemon:                            [  OK  ]
[root@centos6 network-scripts]# chkconfig NetworkManager off

2>. 查看當前系統的ip地址和網卡的設備名。網絡

[root@centos6 network-scripts]# ifconfigide

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。oop


3>.eth0網卡在原有基礎之上,再去添加一個ip地址。rest

修改配置文件ifcfg-eth0:1orm

[root@centos6 network-scripts]# vim ifcfg-eth0:1
[root@centos6 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服務ip

[root@centos6 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地址ci

[root@centos6 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地址。

[root@centos6 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
[root@centos6 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>. 重啓網絡服務

[root@centos6 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地址

[root@centos6 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地址

[root@centos6 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
[root@centos6 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>. 重啓網絡服務

[root@centos6 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地址

[root@centos6 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地址修改爲功。 

相關文章
相關標籤/搜索