linux中路由、策略路由

路由

网关:到其他网段的出口

动态路由:是路由器自动创建的路由表,当网络结构发生变化时,路由器会更具网络情况自动修改路由表。
静态路由:手动制定的路由表,网卡架构发生变化时需要手动修改路由表。

启用路由

临时启用路由功能

[root@snat ~]# cat /proc/sys/net/ipv4/ip_forward
1

永久启动路由功能

[root@snat ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

[root@snat ~]# sysctl -p    #读取配置文件
net.ipv4.ip_forward = 1
设置路由

临时路由

[root@snat ~]# route add -net 192.168.80.0 netmask 255.255.255.0 gw 192.168.6.1

永久路由

[root@snat ~]# vim /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=192.168.7.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.6.1

路由表

查看在linux中所有的路由表

[root@ecs-wyy-snt ~]# cat /etc/iproute2/rt_tables 
#
# reserved values
#
255 local   #本地路由表
254 main    #主要路由表,使用命令ip route 或者 route -n 打印的都是这个路由表中的内容
253 default  #可以存放默认路由,如果不指定该表,默认路由存放在main表中
0   unspec  

格式:路由表的编号 路由表的名称

查看制定路由表中的路由

[root@snat ~]# ip route show table test
default via 192.168.6.1 dev eth1

查看默认的主路由表中的路由

default via 192.168.6.1 dev eth1 
[root@snat ~]# ip route
169.254.169.254 via 192.168.6.1 dev eth0  proto static 
192.168.6.0/24 dev eth0  proto kernel  scope link  src 192.168.6.39 
192.168.6.0/24 dev eth1  proto kernel  scope link  src 192.168.6.36 
192.168.6.0/24 dev eth2  proto kernel  scope link  src 192.168.6.20 
169.254.0.0/16 dev eth0  scope link  metric 1002 
169.254.0.0/16 dev eth1  scope link  metric 1005 
169.254.0.0/16 dev eth2  scope link  metric 1006 
default via 192.168.6.1 dev eth0  proto static 

[root@snat ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
169.254.169.254 192.168.6.1     255.255.255.255 UGH   0      0        0 eth0
192.168.6.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

0.0.0.0         192.168.6.1     0.0.0.0         UG    0      0        0 eth0

Destination :目标地址     
Gateway : 网关
Genmask:子网掩码
Flags :网络标记,U 正在启用,UG 此路由必须制定网关,H 目标是一个主机
Metric :经过几个路由
Ref    
Use :路由使用的次数
Iface :网卡

配置策略路由

在路由器上创建多个路由表,创建规则,可以根据数据包的源地址和应用层协议或者负载选择不同的路由表,进行路径的选择。

使用实例如果一个公司出口出有两条线路(联通、移动),如果配置领导上网使用联通、普通员工上网使用移动。需要写两个路由表,一个是配置联通线路的路由表、另外一个是移动线路的路由表。策略路由根据不同的源IP选择不同的路由表。

实例:一台服务器上的两个网卡在同一网段
服务器网卡1网卡2网关
服务器A192.168.0.100192.168.0.200192.168.0.1
服务器B192.168.0.300192.168.0.1

设置路由表名对于的编号

[root@ecs-wyy-snt ~]# vim /etc/iproute2/rt_tables

32000     test

增加test路由表中的默认路由

[root@ecs-wyy-snt ~]# ip route add default via 192.168.0.1 dev eth1 table test

设置策略路由,
从192.168.0.200来的包,使用路由表test,pref的值是策略路由的优先级。

[root@ecs-wyy-snt ~]# ip rule add from 192.168.0.200 table test pref 32000
实例:在公司的网络中有两个出口,一个电信一个联通,不同的IP访问走不同的路径
服务器网卡1网卡2网卡3网关
服务器A192.168.6.182192.168.6.39
服务器B192.168.6.66192.168.6.39
router192.168.6.39192.168.0.200 (映射公网IP 139.159.221.28)192.168.5.120 (映射公网IP 139.159.228.71)

本次测试在route在linux服务器上部署

[root@ecs-wyy-snt ~]# cat /etc/redhat-release 
CentOS release 6.9 (Final)
实验环境配置(可省略)

为保证多个网卡配置公网IP之后能够上网需要配置策略路由

  • 外网能够通过公网访问该服务器

    增加路由表

    [root@ecs-wyy-snt ~]# vim /etc/iproute2/rt_tables
    32000   test
    32001   test1
    

    配置策略路由保证使用公网IP能远程登录。本次测试的实验环境比较复杂,由于公网IP是映射到服务上的,故需要做策略路由的配置。

    [root@ecs-wyy-snt ~]# ip route add default via 192.168.0.1 dev eth1 table test
    [root@ecs-wyy-snt ~]# ip rule add from 192.168.0.200 table test pref 32000
    
    [root@ecs-wyy-snt ~]# ip route add default via 192.168.5.1 dev eth2 table test1
    [root@ecs-wyy-snt ~]# ip rule add from 192.168.5.120 table test1 pref 32001
  • 配置NAT转发(实现上网)

    [root@ecs-wyy-snt ~]# vim /etc/sysctl.conf
    net.ipv4.ip_forward = 1
    
    [root@ecs-wyy-snt ~]# sysctl -p
    [root@ecs-wyy-snt ~]# iptables -t nat -I POSTROUTING -s 192.168.6.182/32 -j SNAT --to-source 192.168.0.200
    [root@ecs-wyy-snt ~]# iptables -t nat -I POSTROUTING -s 192.168.6.66/32 -j SNAT --to-source 192.168.5.120
    
    
    
    
    [root@router ~]# iptables-save
    
    # Generated by iptables-save v1.4.7 on Sat Dec 16 23:23:51 2017
    
    *nat
    :PREROUTING ACCEPT [2636:117755]
    :POSTROUTING ACCEPT [6:366]
    :OUTPUT ACCEPT [6:366]
    -A POSTROUTING -s 192.168.6.66/32 -j SNAT --to-source 192.168.5.120 
    -A POSTROUTING -s 192.168.6.182/32 -j SNAT --to-source 192.168.0.200 
    COMMIT
    
    # Completed on Sat Dec 16 23:23:51 2017
    
    
    # Generated by iptables-save v1.4.7 on Sat Dec 16 23:23:51 2017
    
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [1450:149349]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
    -A INPUT -p icmp -j ACCEPT 
    -A INPUT -i lo -j ACCEPT 
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
    -A INPUT -j REJECT --reject-with icmp-host-prohibited 
    -A FORWARD -j ACCEPT 
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited 
    COMMIT
    
    # Completed on Sat Dec 16 23:23:51 2017
    
    You have new mail in /var/spool/mail/root
路由器上配置策略路由
  • 确保两张路由表上有默认上网的路由

    [root@ecs-wyy-snt ~]# cat /etc/iproute2/rt_tables 
    #
    
    # reserved values
    
    #
    255 local
    254 main
    253 default
    0   unspec
    32000   test
    32001   test1
    #
    
    # local
    
    #
    
    [root@ecs-wyy-snt ~]# ip route show table test
    default via 192.168.0.1 dev eth1 
    [root@ecs-wyy-snt ~]# ip route show table test1
    default via 192.168.5.1 dev eth2 
  • 增加策略路由

    [root@router ~]# ip rule add from 192.168.6.182 lookup test pref 31000
    
    [root@router ~]# ip rule add from 192.168.6.66 lookup test1 pref 31001
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值