http://hi.baidu.com/imyerik/item/3642d1acc5ce04208819d343
一、ip命令基础
# rpm -qf $(which ip)
iproute-2.6.18-9.el5
帮助:ip help
<1>链路层:
# ip link
# ip link help
Usage: ip link set DEVICE { up | down | #启用停用网卡
arp { on | off } | #打开关闭arp广播
dynamic { on | off } |
multicast { on | off } |
allmulticast { on | off } |
promisc { on | off } |
trailers { on | off } |
txqueuelen PACKETS | #发送缓冲队列长度(默认1000)
name NEWNAME | #网卡命名(lan,wan)
address LLADDR | broadcast LLADDR |#修改MAC地址
mtu MTU } #最大传输单元(默认1500)
ip link show [ DEVICE ]
网卡命名举例:
# ip link set lan down
# ip link set wlan down
# ip link set eth0 name wlan
# ip link set eth1 name lan
# ip link set lan up
# ip link set wlan up
<2>ip addr
# ip addr add 192.168.2.77/24 dev lan #添加ip
# ip addr show dev lan
# ip addr flush dev lan #清空网卡的所有ip地址
<3>ip neigh
# ip neigh show #显示ip与mac对应关系
# ip neigh flush dev lan
<4>ip route (需要开启ip_forward)
# ip route #相当于route -n
# ip route add 192.168.2.0/24 via 192.168.1.1 dev lan #添加一条路由:去往2.0网段,通过lan网卡,下一跳IP为192.168.1.1
# ip route add 0.0.0.0/0 via 10.10.10.30 dev wan #添加默认路由
# ip route add default via 10.10.10.30 dev wan #添加默认路由
# ip route del 192.168.2.0/24
# traceroute www.google.com #路由追踪
# tracepath www.google.com #..
# mtr www.google.com #动态刷新追踪
------------------------------------------------------------------------------------------------------------
二、ECMP(Equal Cost Multipath等值多路径路由)
当路由上有多个链路接入时,并且同时使用,可以考虑ECMP技术
添加两个默认路由,使用rr调度算法根据会话轮询
# ip route help #查看帮助
# ip route add default mpath rr nexthop via 192.168.96.254 dev vmnet1 nexthop via 192.168.203.254 dev vmnet8
# ip route #这样就会有两条默认路由了
设置权重
# ip route add default mpath rr nexthop via 192.168.96.254 dev vmnet1 weight 100 nexthop via 192.168.203.254 dev vmnet8 weight 10 #权重最大256
默认RHEL5U3内核会cache路由,对于已经建立好的连接,不会轮询,而直接读取cache路由,从而破坏rr轮询,所以需要将“在cache情况下仍然支持ECMP的模块“编译出来安装
Networking --->
--- Networking support
Networking options --->
IP: equal cost multipath with caching support (EXPERIMENTAL)
ip rule
# ip route list table local #最先查的表
# ip route list table main #平时操作的表
# ip route list table default
------------------------------------------------------------------------------------------------------------
三、策略路由
1、基于源地址策略路由
根据源客户端的子网网段走向相应的路由路径
例如:销售部门和办公部门,让销售部门享受10M带宽线路,让办公部门享受1M带宽线路;
配置举例:
需求:让公司192.168.0.0/24网段的客户走网通路由,192.168.1.0/24网段的走电信路由;
IP及拓扑说明:
C1--\ /-----CNC\
(GW) > ---->Client3C2--/ \------TEL/
Client1:192.168.0.1
Client2:192.168.1.2
Client3:20.0.0.1
公司网关GW
对内:
I1:192.168.0.254 eth0
I2:192.168.1.254 eth0
对外:
E1:202.106.0.21 eth1
E2:202.106.46.152 eth1
网通CNC网关
C1:202.106.0.20 eth0
C2:20.0.0.2 eth1
电信TEL网关
T1:202.106.46.151 eth0
T2:20.0.0.3 eth1
GW配置:
<1>增加自定义的路由表
# vi /etc/iproute2/rt_tables
100 tab1
101 tab2
# ip rule
<2>添加路由规则
# ip route add default via 202.106.0.20 dev eth1 table tab1 #添加默认路由到表tab1
# ip route add default via 202.106.46.151 dev eth1 table tab2 #添加默认路由到表tab2
# ip rule add from 192.168.1.0/24 table tab1 #来自192.168.1.0/24网段的请求走tab1表
# ip rule add from 192.168.0.0/24 table tab2
# ip rule
# ip route add 192.168.1.0/24 dev eth0 table tab1 #添加直连路由,若不添加则Client1 ping GW后,数据包不能返回,因为tab1表中只有一个default路由
# ip route add 192.168.0.0/24 dev eth0 table tab2
TEL配置:
添加去往Client1/2的路由
# ip route add 192.168.0.0/24 via 202.106.46.152 dev eth0
# ip route add 192.168.1.0/24 via 202.106.46.152 dev eth0
CNC配置:
添加去往Client1/2的路由
# ip route add 192.168.0.0/24 via 202.106.0.21 dev eth0
# ip route add 192.168.1.0/24 via 202.106.0.21 dev eth0
Client配置:
Client1\2:
# ip route add default via 192.168.0.254 dev eth0
# ip route add default via 192.168.1.254 dev eth0
Client3:
# ip route add default via 20.0.0.3 dev eth0 #测试仅从TEL路径返回也可以双路返回
# ip route add default mpath rr nexthop via 20.0.0.2 dev eth0 nexthop via 20.0.0.3 dev eth0
Client1\2测试:
# traceroute 20.0.0.1
------------------------------------------------------------
2、基于防火墙标记的路由(firemark)
将来自不同ip段的数据包打上不同的防火墙标记,根据防火墙的标记路由到相应的地址
GW配置:
1-100的IP标记为1,101-253的IP标记为10
# iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.1-192.168.1.100 -j MARK --set-mark 1
# iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.101-192.168.1.253 -j MARK --set-mark 10
先清空之前的rule
# ip rule del from 192.168.1.0/24
# ip rule del from 192.168.0.0/24
# ip rule add fwmark 1 pref 1000 table tab1 #将标记为1的包送至tab1,pref为优先级
# ip rule add fwmark 10 pref 2000 table tab2
# ip rule
0: from all lookup 255
1000: .... tab1
2000: .... tab2
32766: from all lookup main
32767: from all lookup default
剩余基本同上
# ip route add default via 202.106.0.20 dev eth1 table tab1 #添加默认路由到表tab1
# ip route add default via 202.106.46.151 dev eth1 table tab2
# ip route add 192.168.1.0/24 dev eth0 table tab1 #添加直连路由
# ip route add 192.168.0.0/24 dev eth0 table tab2
........
------------------------------------------------------------
3、基于目的地址策略路由
根据访问的目标地址返回,访问网通的IP段走CNC,访问电信的走TEL
GW配置:
删除规则
# iptables -F -t mangle
# ip rule del fwmark 1 pref 1000 table tab1
# ip rule del fwmark 10 pref 2000 table tab2
# ip rule add to 20.0.0.1 table tab2
# ip rule add to 20.0.0.4 table tab1
# ip rule
其余配置同上
Client1\2测试:
# traceroute 20.0.0.1
# traceroute 20.0.0.4
假如访问教育网或国外网络,可以在main表中添加一条默认的路由
# rpm -qf $(which ip)
iproute-2.6.18-9.el5
帮助:ip help
<1>链路层:
# ip link
# ip link help
Usage: ip link set DEVICE { up | down | #启用停用网卡
arp { on | off } | #打开关闭arp广播
dynamic { on | off } |
multicast { on | off } |
allmulticast { on | off } |
promisc { on | off } |
trailers { on | off } |
txqueuelen PACKETS | #发送缓冲队列长度(默认1000)
name NEWNAME | #网卡命名(lan,wan)
address LLADDR | broadcast LLADDR |#修改MAC地址
mtu MTU } #最大传输单元(默认1500)
ip link show [ DEVICE ]
网卡命名举例:
# ip link set lan down
# ip link set wlan down
# ip link set eth0 name wlan
# ip link set eth1 name lan
# ip link set lan up
# ip link set wlan up
<2>ip addr
# ip addr add 192.168.2.77/24 dev lan #添加ip
# ip addr show dev lan
# ip addr flush dev lan #清空网卡的所有ip地址
<3>ip neigh
# ip neigh show #显示ip与mac对应关系
# ip neigh flush dev lan
<4>ip route (需要开启ip_forward)
# ip route #相当于route -n
# ip route add 192.168.2.0/24 via 192.168.1.1 dev lan #添加一条路由:去往2.0网段,通过lan网卡,下一跳IP为192.168.1.1
# ip route add 0.0.0.0/0 via 10.10.10.30 dev wan #添加默认路由
# ip route add default via 10.10.10.30 dev wan #添加默认路由
# ip route del 192.168.2.0/24
# traceroute www.google.com #路由追踪
# tracepath www.google.com #..
# mtr www.google.com #动态刷新追踪
------------------------------------------------------------------------------------------------------------
二、ECMP(Equal Cost Multipath等值多路径路由)
当路由上有多个链路接入时,并且同时使用,可以考虑ECMP技术
添加两个默认路由,使用rr调度算法根据会话轮询
# ip route help #查看帮助
# ip route add default mpath rr nexthop via 192.168.96.254 dev vmnet1 nexthop via 192.168.203.254 dev vmnet8
# ip route #这样就会有两条默认路由了
设置权重
# ip route add default mpath rr nexthop via 192.168.96.254 dev vmnet1 weight 100 nexthop via 192.168.203.254 dev vmnet8 weight 10 #权重最大256
默认RHEL5U3内核会cache路由,对于已经建立好的连接,不会轮询,而直接读取cache路由,从而破坏rr轮询,所以需要将“在cache情况下仍然支持ECMP的模块“编译出来安装
Networking --->
--- Networking support
Networking options --->
IP: equal cost multipath with caching support (EXPERIMENTAL)
ip rule
# ip route list table local #最先查的表
# ip route list table main #平时操作的表
# ip route list table default
------------------------------------------------------------------------------------------------------------
三、策略路由
1、基于源地址策略路由
根据源客户端的子网网段走向相应的路由路径
例如:销售部门和办公部门,让销售部门享受10M带宽线路,让办公部门享受1M带宽线路;
配置举例:
需求:让公司192.168.0.0/24网段的客户走网通路由,192.168.1.0/24网段的走电信路由;
IP及拓扑说明:
C1--\ /-----CNC\
(GW) > ---->Client3C2--/ \------TEL/
Client1:192.168.0.1
Client2:192.168.1.2
Client3:20.0.0.1
公司网关GW
对内:
I1:192.168.0.254 eth0
I2:192.168.1.254 eth0
对外:
E1:202.106.0.21 eth1
E2:202.106.46.152 eth1
网通CNC网关
C1:202.106.0.20 eth0
C2:20.0.0.2 eth1
电信TEL网关
T1:202.106.46.151 eth0
T2:20.0.0.3 eth1
GW配置:
<1>增加自定义的路由表
# vi /etc/iproute2/rt_tables
100 tab1
101 tab2
# ip rule
<2>添加路由规则
# ip route add default via 202.106.0.20 dev eth1 table tab1 #添加默认路由到表tab1
# ip route add default via 202.106.46.151 dev eth1 table tab2 #添加默认路由到表tab2
# ip rule add from 192.168.1.0/24 table tab1 #来自192.168.1.0/24网段的请求走tab1表
# ip rule add from 192.168.0.0/24 table tab2
# ip rule
# ip route add 192.168.1.0/24 dev eth0 table tab1 #添加直连路由,若不添加则Client1 ping GW后,数据包不能返回,因为tab1表中只有一个default路由
# ip route add 192.168.0.0/24 dev eth0 table tab2
TEL配置:
添加去往Client1/2的路由
# ip route add 192.168.0.0/24 via 202.106.46.152 dev eth0
# ip route add 192.168.1.0/24 via 202.106.46.152 dev eth0
CNC配置:
添加去往Client1/2的路由
# ip route add 192.168.0.0/24 via 202.106.0.21 dev eth0
# ip route add 192.168.1.0/24 via 202.106.0.21 dev eth0
Client配置:
Client1\2:
# ip route add default via 192.168.0.254 dev eth0
# ip route add default via 192.168.1.254 dev eth0
Client3:
# ip route add default via 20.0.0.3 dev eth0 #测试仅从TEL路径返回也可以双路返回
# ip route add default mpath rr nexthop via 20.0.0.2 dev eth0 nexthop via 20.0.0.3 dev eth0
Client1\2测试:
# traceroute 20.0.0.1
------------------------------------------------------------
2、基于防火墙标记的路由(firemark)
将来自不同ip段的数据包打上不同的防火墙标记,根据防火墙的标记路由到相应的地址
GW配置:
1-100的IP标记为1,101-253的IP标记为10
# iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.1-192.168.1.100 -j MARK --set-mark 1
# iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.101-192.168.1.253 -j MARK --set-mark 10
先清空之前的rule
# ip rule del from 192.168.1.0/24
# ip rule del from 192.168.0.0/24
# ip rule add fwmark 1 pref 1000 table tab1 #将标记为1的包送至tab1,pref为优先级
# ip rule add fwmark 10 pref 2000 table tab2
# ip rule
0: from all lookup 255
1000: .... tab1
2000: .... tab2
32766: from all lookup main
32767: from all lookup default
剩余基本同上
# ip route add default via 202.106.0.20 dev eth1 table tab1 #添加默认路由到表tab1
# ip route add default via 202.106.46.151 dev eth1 table tab2
# ip route add 192.168.1.0/24 dev eth0 table tab1 #添加直连路由
# ip route add 192.168.0.0/24 dev eth0 table tab2
........
------------------------------------------------------------
3、基于目的地址策略路由
根据访问的目标地址返回,访问网通的IP段走CNC,访问电信的走TEL
GW配置:
删除规则
# iptables -F -t mangle
# ip rule del fwmark 1 pref 1000 table tab1
# ip rule del fwmark 10 pref 2000 table tab2
# ip rule add to 20.0.0.1 table tab2
# ip rule add to 20.0.0.4 table tab1
# ip rule
其余配置同上
Client1\2测试:
# traceroute 20.0.0.1
# traceroute 20.0.0.4
假如访问教育网或国外网络,可以在main表中添加一条默认的路由
#网络互联
分享到:
举报
浏览(3285) 评论
(3) 转载
(3)
本文最近访客