Linux 多IP設置 多vlan設置

Linux 設置 多ip,多vlan

 

引:在Linux下有時候需要給一個網卡綁定多個IP,本文介紹在Redhat系列(redhat,Fedora Core,Centos)中的實現方法和一種在Gentoo等其他Linux中普遍適用的方法。

 

在Linux下有時候需要給一個網卡綁定多個IP,本文介紹在Redhat系列(redhat,Fedora Core,Centos)中的實現方法和一種在Gentoo等其他Linux中普遍適用的方法。

 

1、單網卡綁定多IP在Redhat系列中的實現方法

 

假設需要綁定多IP的網卡是eth0,請在/etc/sysconfig/network-scripts目錄裏面創建一個名爲ifcfg-eth0:0的文件,

 

內容樣例爲:

 

DEVICE=”eth0:0″

IPADDR=”192.168.0.2″

BROADCAST=”172.168.0.255″

NETMASK=”255.255.255.0″

ONBOOT=”yes”

其中的DEVICE爲設備的名稱,

IPADDR爲此設備的IP地址,

BROADCAST是廣播地址,

NETMASK爲子網掩碼,

ONBOOT 表示在系統啓動時自動啓動。

如果需要再綁定多一個IP地址,

只需要把文件名和文件內的DEVICE中的eth0:x加一即可。

LINUX最多可以支持255個IP別名

 

2、普遍適用的單網卡綁定多IP實現方法

 

ifconfig eth0:1 192.168.0.3 broadcast 192.168.0.255 netmask 255.255.255.0

可以把上述命令加在啓動自運行文件裏面,在Gentoo下是/etc/conf.d/local.start,而某些版本的Linux是/etc/rc.d/rc.local。

 

3、多網卡共用單IP的實現方法

 

使用多塊網卡虛擬成爲一塊網卡,具有相同的IP地址。這項技術其實在sun和cisco中已經存在,分別稱爲 Trunking和 etherchannel技術,在linux中,這種技術稱爲bonding。因爲bonding在內核2.4.x中已經包含了,只需要在編譯的時候把網 絡設備選項中的 Bonding driver support選中就可以了。

 

然後,重新編譯核心,重新起動計算機,執行如下命令:

 

modprobe bonding miimon=100 mode=0

 ifconfig bond0 202.100.1.2 netmask 255.255.255.0 up

ifenslave bond0 eth0 eth1

現在兩塊網卡已經象一塊一樣工作了,這樣可以提高集羣節點間的數據傳輸。

 

你最好把這幾句寫成一個腳本,再由/etc/rc.d/rc.local或者/etc/conf.d/local.start調用,以便一開機就生效。

 

bonding對於服務器來是個比較好的選擇,在沒有千兆網卡時,用兩三塊100兆網卡作 bonding,可大大提高服務器到交換機之間的帶寬。但是需要在交換機上設置連接bonding 網卡的兩個口子映射爲同一個虛擬接口。

 

永久性的捆綁口配置

eth0  eth2捆綁成bond0使用 配置一相IP地址

1、加載bonding 模塊

    modprobe bonding

2、修改網卡配置文件

 vi /etc/sysconfig/network-scripts/ifcfg-bond0

DEVICE=bond0

TYPE=Ethernet

ONBOOT=yes

IPV6INIT=no

USERCTL=no

 

 vi /etc/sysconfig/network-scripts/ifcfg-eth0

DEVICE=eth0

BOOTPROTO=none

ONBOOT=yes

TYPE=Ethernet

MASTER=bond0

SLAVE=yes

 

vi /etc/sysconfig/network-scripts/ifcfg-eth2

DEVICE=eth2

BOOTPROTO=none

ONBOOT=yes

TYPE=Ethernet

MASTER=bond0

SLAVE=yes

 

3、加載模塊配置文件

 vi /etc/modprobe.d/bonding.conf

alias bond0 bonding

alias bond1 bonding

options bond0 mode=0 miimon=200

options bond1 mode=0 miimon=200

 

mode指定了bond0的工作模式,常用的是0和1,0表示負載均衡方式,1表示主從方式,可根據需要自行配置。常用的爲0,1兩種。mode=0表示 load balancing (round-robin)爲負載均衡方式,兩塊網卡都工作。

                mode=1表示fault-tolerance (active-backup)提供冗餘功能,工作方式是主備的工作方式,也就是說默認情況下只有一塊網卡工作,另一塊做備份。bonding只能提供鏈路監測,即從主機到交換機的鏈路是否接通。

                如果只是交換機對外的鏈路down掉了,而交換機本身並沒有故障,那麼bonding會認爲鏈路沒有問題而繼續使用。 miimon是用來進行鏈路監測的。比如:miimon=100,那麼系統每100ms監測一次鏈路連接狀態,

                如果有一條線路不通就轉入另一條線路。

                    bonding mode=1 miimon=100.miimon是用來進行鏈路監測的。 比如:miimon=100,那麼系統每100ms監測一次鏈路連接狀態,如果有一條線路不通就轉入另一條線路;mode的值表示工作模式,

Bonding的模式一共有7種:

#defineBOND_MODE_ROUNDROBIN       0   balance-rr模式)網卡的負載均衡模式

#defineBOND_MODE_ACTIVEBACKUP     1   active-backup模式)網卡的容錯模式

#defineBOND_MODE_XOR              2   balance-xor模式)需要交換機支持

#defineBOND_MODE_BROADCAST        3    broadcast模式)

#defineBOND_MODE_8023AD           4   IEEE 802.3ad動態鏈路聚合模式)需要交換機支持

#defineBOND_MODE_TLB              5   自適應傳輸負載均衡模式

#defineBOND_MODE_ALB              6   網卡虛擬化方式

 

mode=0:平衡負載模式,有自動備援,但需要”Switch”支及設定。

mode=1:自動備援模式,其中一條線若斷線,其他線路將會自動備援。

 mode=6:平衡負載模式,有自動備援,不需要”Switch”支及設定。

 

 

 

4、查看網卡綁定信息

cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)

 

Bonding Mode: load balancing (round-robin)

MII Status: up

MII Polling Interval (ms): 200

Up Delay (ms): 0

Down Delay (ms): 0

 

Slave Interface: eth0

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 3

Permanent HW addr: 00:1b:cd:04:17:70

Slave queue ID: 0

 

Slave Interface: eth2

MII Status: up

Speed: 1000 Mbps

Duplex: full

Link Failure Count: 2

Permanent HW addr: 00:1b:cd:04:17:72

Slave queue ID: 0

 

 

-----

 

設置linux的vlan腳本

 

下面是腳本vlan.sh

#!/bin/bash

 

echo 1 > /proc/sys/net/ipv4/ip_forward

vconfig add eth0 51

ip address add 217.221.181.1/24 dev eth0.51

ip link set dev eth0.51 up

 

vconfig add eth0 52

ip address add 217.221.182.1/24 dev eth0.52

ip link set dev eth0.52 up

 

vconfig add eth0 53

ip address add 217.221.183.1/24 dev eth0.53

ip link set dev eth0.53 up

 

在rc.local中啓動

--------------

 

透過 VLan 的功能能將一個實體的 switch 將他切割成多個 logical broadcast domain (所有的廣播封包 broadcast package 只會在此 doamin 中傳送).他的做法也很簡單就是在原先的訊框 (Frame) 指定 tag 而這 tag 的數字即是 VLAN ID. VLAN 已經被 IEEE 列為標準協定的一員 802.1q一開始沒設定任何 VLAN 的 switch 其所有的網路埠的 VLAN ID 皆為 1.

 

 

Linux 的核心也是有支援 VLAN 功能,主要就是在網路埠上的訊框加上指定的 VLAN Tag .設定使用上很簡單.只要 2 步驟即可.

 

步驟一

目前 RedHat 是將 VLAN 編譯成模組的方式來支援,所以先看看你的 Linux 是否已經掛載了 802.1q(VALN) 的模組.

 

[root@benjr ~]# modprobe 8021q

[root@benjr ~]# lsmod |grep -i 8021q

步驟二

開始設定 VLAN ID .我們透過指令 #vconfig add [interface-name] [vlan_id] 這邊要注意的一點是要設定網路裝置 [interface-name] 必須是 active 的,可以透過 #ifconfig 查詢到的.[vlan_id] 可以為 0-4095.

 

[root@benjr ~]# vconfig add eth0 10

這樣系統就會多出一組 eth0.10 的裝置出來,這裝置將只接受含有 VLAN ID =10 的訊框.所有設定好的 VLAN 網路埠都可以透過 /proc/net/vlan/config 來查詢到.不過這樣的設定下次重新開機就會失效了,還需要將他寫在 /etc/rc.local,讓他在開機時候就會生效.如果是要立即移除時可使用 #vconfig rem [vlan-name] 來移除.

 

不過這並不符合我當初的構想是將 Linux 下多個網路埠把他當成可以使用 VLAN 的 switch.

 

參考文件 http://linux.vbird.org/somepaper/20060622-linux_vlan.pdf

 

不過這樣的應用我不是很懂為什麼要將一個網路埠切割成多個並將子埠指派不同的 VLAN ID.上網查了一下似乎有一種應用叫做單臂路由器 one-armed-router,是用來在多個虛擬網路中傳送封包.而一個單臂路由器上所連接網路埠都是再同一個實體網路埠上.看起來不是很懂還是有其 他的應用方式.

 

 

 

 

linux VLAN配置(vconfig)

 

安裝vlan(vconfig)和加載8021q模塊

[root@test0001~]#yum install vconfig

[root@test0001~]#modprobe 8021q

[root@test0001~]#lsmod |grep -i 8021q

 

使用linux vconfig命令配置vlan

[root@test0001~]#vconfig add eth0 100

Added VLAN with VID == 100 to IF -:eth0:-

[root@test0001~]#vconfig add eth0 200

Added VLAN with VID == 200 to IF -:eth0:-

#在eth0接口上配置兩個VLAN

[root@test0001~]#vconfig set_flag eth0.100 1 1

Set flag on device -:eth0.100:- Should be visible in/proc/net/vlan/eth0.100

[root@test0001~]#vconfig set_flag eth0.200 1 1

Set flag on device -:eth0.200:- Should be visible in/proc/net/vlan/eth0.200

#設置VLAN的REORDER_HDR參數,默認就行了。

#可以使用cat /proc/net/vlan/eth0.100查看eth0.100參數

[root@test0001~]#ifconfig eth0 0.0.0.0

[root@test0001~]#ifconfig eth0.100 172.16.1.8 netmask 255.255.255.0up

[root@test0001~]#ifconfig eth0.200 172.16.2.8 netmask 255.255.255.0up

#配置網絡信息

[root@test0001~]#vconfig rem eth0.100

Removed VLAN -:eth0.100:-

[root@test0001~]#vconfig rem eth0.200

Removed VLAN -:eth0.200:-

#刪除VLAN命令

#將VLAN信息寫入配置文件(/etc/rc.local)中,下次隨機啓動。

 

添加虛擬設備

 

vconfig add eth0 192

cd /etc/sysconfig/network-scripts

cp ifcfg-eth0 ifcfg-eth0.192

編輯ifcfg-eth0.192 將eth0改成eth0.192

編輯ETH0將關於ip的刪除

 

開機啓動802.1q

 

在/etc/sysconfig/network鍵入VLAN=yes

開機保留vlan配置:編輯/etc/rc.local

添加如下內容:

modprobe 8021q

vconfig add eth0 192

ifconfig eth0.192 103.246.132.108/24 up

 

檢查vlan信息

tail /proc/net/vlan/bond0.1010

          total bytes received      8645890

      Broadcast/Multicast Rcvd            0

 

      total frames transmitted          160

       total bytes transmitted         7236

            total headroom inc            0

           total encap on xmit            0

Device: bond0

INGRESS priority mappings: 0:0  1:0  2:0  3:0  4:0  5:0  6:0 7:0

 EGRESS priority mappings:

 

cat /proc/net/vlan/p2p2.20

p2p2.20  VID: 20         REORDER_HDR: 1  dev->priv_flags: 1

         total frames received  46538248681

          total bytes received 38589078808915

      Broadcast/Multicast Rcvd        31239

 

      total frames transmitted  46432764217

       total bytes transmitted 14521448636338

            total headroom inc            0

           total encap on xmit            0

Device: p2p2

INGRESS priority mappings: 0:0  1:0  2:0  3:0  4:0  5:0  6:0 7:0

 EGRESS priority mappings:

 

檢查vlan配置

cat /proc/net/vlan/config

VLAN Dev name    | VLAN ID

Name-Type: VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD

p2p2.20        | 20  | p2p2

em1.26         | 26  | em1

em2.25         | 25  | em2

em2.18         | 18  | em2

p2p1.19        | 19  | p2p1

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章