首頁‎ > ‎電子期刊‎ > ‎2009年2月號‎ > ‎

Linux路由應用介紹


介紹

在網路概念裡,路由是一個很重要的觀念,他負責封包的傳遞及方向,若網域有太多的廣播,這時候就必須要用子網路遮罩,若要連接不同網域,這時候就必須要使用路由器來幫忙了,在這裡我們介紹進階路由

進階路由:

  1. Policy route
  2. Tunnel (GRE)
  3. Virtual Routing: 可由以下幾種方法來完成
  • VRRP: Virtual Routing Redundency Protocol 路由器上使用的協定
  • IPVS: IP Virtual Server
  • Bonding


Policy route

設定靜態路由檔,這個方法的位階順序是最低的 ex : vi /etc/sysconfig/network-scripts/route-eth0, 使用rule(policy)來設定靜態路由,此方法的位階順序比較高,policy總共有255條,設定檔在/etc/iproute2/rt_tables, 預設 0,253,254,255 這四條不能使用, 0 -> unspec , 253 -> default , 254 -> main , 255 -> local

ip rule list // show main, default, local 三種

ip route list table main // show main talbe 中所有的路由路徑

ip rule add from 原來網段 to 目的網段 table <num>

ip rule add from 192.168.4.0/24 to 192.168.15.0/24 table 100 // 這是暫時的, 必須寫在 script 使開機時啟動執行

ip rule add from 192.168.4.0/24 to 192.168.15.0/24 table test // 在 /etc/iproute2/rt_tables 裡面有加就可以這麼寫

使用 iptables 來紀錄 log

iptables -A FORWARD -j LOG -p icmp -s 192.168.4.100 -d 192.168.15.15 --log-level 6 --log-prefix "icmp_pks "

iptables -A FORWARD -j LOG -p http -s 來源_IP -d 目的_IP --log-level <0-7> --log-prefix "標記符號" // log-prefix 將紀錄到的封包,給予一個標頭 icmp_pks (並且空一格, 才不會跟後面的訊息連接在一起),syslog.conf內, kern.=info /var/log/iptalbes // 因為 iptables 的 log 是由 kernel 產生,只紀錄階級為 info 的訊息,view /usr/include/sys/syslog.h 紀錄有關 facility.priority 的資料

設定某種封包的路由走法

標記封包:

iptables -t mangle -A PREROUTE -j MARK -p tcp --dport 80 -d 192.168.4.100 --set-mark 1 // 主要是這行的設定 (用 iptables -t mangle -L 來顯示 mangle 清單),符合標記(標記為 1)封包的 policy, 使用 table 為 test: ip rule add fwmark 1 table test // 封包標頭有 1 的, 使用 tables 為 test 的規則 設定 table 為 test 的 路由ip route add 192.168.4.0/24 via 192.168.47.250 table test


Tunnel (GRE)

建立 Tunnel 來架路由

<第一個 Tunnel> modprobe ip_gre // 可用 lsmod | grep ip_gre 檢視是否載入成功

ip tunnel add tun0 mode gre local <local_IP> remote <remote_IP> // 建立一個 tunnel 裝置 tun0 ,ex : ip tunnel add tun0 mode gre local 192.168.115.100 remote 192.168.104.100

ip address add IP/mask dev tun0 // 綁一個 IP 在 tun0 介面上 ,ex : ip a a 192.168.115.1/24 dev tun0

ip link set tun0 up // 將 tun0 介面啟動

ip route add <remote_內部_網段> dev tun0 // 建立雙方 tunnel 內部的路由 ,ex : ip route add 192.168.4.0/24 dev tun0

以上設定完成後,需要等待幾分鐘時間來溝通

<第二個 Tunnel>

ip tunnel add tun1 mode gre local <local_IP> remote <remote_IP> // 建立一個 tunnel 裝置 tun1 ,ex : ip tunnel add tun0 mode gre local 192.168.115.100 remote 192.168.102.100 ip address add IP/mask dev tun1 // 綁一個 IP 在 tun1 介面上 ,ex : ip a a 192.168.115.2/24 dev tun1

ip link set tun1 up // 將 tun1 介面啟動

ip route add <remote_內部_網段> dev tun1     // 建立雙方 tunnel 內部的路由 ,ex : ip route add 192.168.2.0/24 dev tun1


Virtual Routing

VRRP


在兩台 Router(A & B) 上面做以下的設定:

  1. http://sourceforge.net 網站下載 vrrpd-1.0.tar.gz 套件
  2. tar vrrpd-1.0.tar.gz
  3. cd vrrpd
  4. gzip vrrpd.8. // 解壓縮說明檔
  5. cp vrrpd.8.gz /usr/share/man/man8/ // 將說明檔複製到可用 man 查詢的目錄下
  6. cp vrrpd /usr/sbin // 將執行檔, 複製到 /usr/sbin 目錄下
  7. make // 正式步驟開始
  8. vrrpd -i eth1 -v 100 192.168.101.1 // 設定虛擬的 IP Gateway 位址, -v 的設定, 在兩台 Route 不能設定一樣


IP Virtual Server


  1. rpm -aq | grep ipvsadm
  2. yum -y install ipvsadm
  3. iptables -t nat -A POSTROUTING -j SNAT -s 192.168.101.0/24 --to-source 192.168.100.200// 在 FireWall Server 上設定,可讓內部主機連外的 nat 設定
  4. man ipvsamd
  5. ipvsadm -A -t 192.168.100.200:80 -s wlc // -A 增加一個虛擬的介面 -s 設定如何輪替的方法: 有以下的方法,rr: Round Robin: 一個一個輪流,lc: Least Connection: 使用負載最輕的主機,wlc: Weighted Least Connection 使用加權數值,決定被使用的依據,數值越大權值越大
  6. ipvsadm -a -t 192.168.101.200:80 -r 192.168.101.101:80 -w 100 -m // -a 增加一個真實的介面 to 虛擬主機
  7. ipvsadm -a -t 192.168.101.200:80 -r 192.168.101.102:80 -w 10000 -m // -r Real Server;-w weitht 權值;-m Masquerading 偽裝,透過 NAT 轉換;在真實主機上設定,用來觀察通過的封包數:iptables -A INPUT -j ACCEPT -p tcp --dport 80;iptables -L -n -v -Z // 計數歸零;iptables -L -n -v // 看計數量


Bonding


  1. modprobe bonding // modinfo bonding 查看 bonding 模組的資訊
  2. miimon=100 mode=3
  3. vi modprobe.conf ; alias bond0 bonding
  4. ip link set bond0 up // ip l s bond0 up
  5. ifenslave bond0 eth1 eth2 LAN // 將 eth1 & eth2 兩張網卡綁在 bond0 介面下
  6. ip add address <IP/mask> dev bond0 // ip a a <IP/mask> dev bond0
  7. vi /etc/sysconfig/network-script/ifcfg-eth1 // 如果要寫死的話, 需要寫這些檔案(MASTER=bond0 ; SLAVE=yes)
  8. vi /etc/sysconfig/network-script/ifcfg-bond0 //新增網卡的資料進來


以上為各位介紹的一些進階路由使用的方法,當然一定還會有更多的方式可以運用,希望各位能秉持研究的熱誠專研,讓自己的技術能更上一層樓

Comments