zoukankan      html  css  js  c++  java
  • Linux--NiaoGe-Service-08(路由)

    路由

    Linux系统下的路由表是由小到大排列的,即C类地址-->B类地址-->A类地址-->0.0.0.0(默认路由)。

    Linux系统中使用route命令查看路由表

    [root@www ~]# route -n 
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
    0.0.0.0         192.168.30.2    0.0.0.0         UG    0      0        0 eth0

    依据网络接口产生的IP而存在的路由,即主机上有几个网络接口,就会有对应的路由。如上结果显示,主机有一个网络接口eth0,所以对应的路由192.168.30.2。

    手动或默认路由(Default Route)

    如果当前主机需要与另一个网络接口进行通信时,此时就要添加一条路由

    [root@www ~]# ifconfig eth1 192.168.130.110  //添加一块网卡设备,然后设置IP为192.168.130.110
    [root@www ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.130.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
    192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
    0.0.0.0         192.168.30.2    0.0.0.0         UG    0      0        0 eth0
    [root@www ~]# route add -net 192.168.130.0 
    > netmask 255.255.255.0 gw 192.168.130.254
    [root@www ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.130.0   192.168.130.254 255.255.255.0   UG    0      0        0 eth1
    192.168.130.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
    192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
    0.0.0.0         192.168.30.2    0.0.0.0         UG    0      0        0 eth0
    
    [root@www ~]# route add -net 192.168.130.0 netmask 255.255.255.0 dev eth0
    [root@www ~]# route -n
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.130.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.130.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
    192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
    169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
    0.0.0.0         192.168.30.2    0.0.0.0         UG    0      0        0 eth0
    [root@www ~]# ping -c 2 192.168.130.110
    PING 192.168.130.110 (192.168.130.110) 56(84) bytes of data.
    64 bytes from 192.168.130.110: icmp_seq=1 ttl=64 time=0.061 ms
    64 bytes from 192.168.130.110: icmp_seq=2 ttl=64 time=0.027 ms
    
    --- 192.168.130.110 ping statistics ---
    2 packets transmitted, 2 received, 0% packet loss, time 999ms
    rtt min/avg/max/mdev = 0.027/0.044/0.061/0.017 ms

    动态路由:自己Google一下吧!

    Quagga:

    官方网址:https://www.quagga.net/
       Quagga是一个路由软件套件,为Unix平台提供OSPFv2,OSPFv3,RIP v1和v2,RIPng和BGP-4的实现,特别是FreeBSD,Linux,Solaris和NetBSD。 Quagga是由Kunihiro Ishiguro开发的GNU Zebra的分支。Quagga体系结构由一个核心守护程序zebra组成,它作为底层Unix内核的抽象层,并通过Unix或TCP流向Quagga客户端呈现Zserv API。正是这些Zserv客户端通常实现路由协议并将路由更新传递给zebra守护程序。现有的Zserv实现是:

    IPv4IPv6
    zebra - kernel interface, static routes, zserv server
    ripd ripngd - RIPv1/RIPv2 for IPv4 and RIPng for IPv6
    ospfd ospf6d - OSPFv2 and OSPFv3
    bgpd - BGPv4+ (including address family support for multicast and IPv6)
    isisd - IS-IS with support for IPv4 and IPv6

    Quagga守护进程每个都可以通过网络可访问的CLI(称为'vty')进行配置。 CLI遵循类似于其他路由软件的样式。 Quagga还有一个名为'vtysh'的附加工具,它作为所有守护进程的单一内聚前端,允许人们在一个地方管理各种Quagga守护进程的几乎所有方面。

    Zebra

    官方网址:https://zabra.com/#family-safety-solutionhttps://zabra.com/#family-safety-solution

    一个网卡绑定多个IP:IP Alias的测试用途

    测试用:

    [root@www ~]# ifconfig [device] [IP] netmask [netmask ip] [up|down]
    [root@www ~]# ifconfig eth0:0 192.168.0.12 netmask 255.255.255.0 up

    在一个实体网络中含有多个IP网络

    比如有时候单位或学校的主机不允许修改IP,此时就可以使用IP Alias来设置一个可用的IP。

    既有设备无法提供更多实体网卡时

    注意:所有的IP Alias都依据实体网卡,所以,当要启动eth0:0时,eth0必须先启动,而当eth0被关闭后,所有eth0:n的模拟网卡也将被关闭。

    小技巧:如果想将IP Alias设置为开机启动,按照如下做法

    [root@www ~]# cd /etc/sysconfig/network-scripts/
    [root@www network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0
    [root@www network-scripts]# vim ifcfg-eth0:0
    DEVICE=eth0:0
    TYPE=Ethernet
    UUID=df2276d3-82d4-4e2b-9695-b587ae061759
    ONBOOT=yes
    NM_CONTROLLED=yes
    BOOTPROTO=static
    IPADDR=192.168.0.12
    NETMASK=255.255.255.0
    HWADDR=00:0C:29:6B:6E:1B
    DEFROUTE=yes
    PEERDNS=yes
    PEERROUTES=yes
    IPV4_FAILURE_FATAL=yes
    IPV6INIT=no
    NAME="System eth0:0"
    USERCTL=n

    路由重复问题

    问题:可不可以使用两张网卡,两个相同网络的IP来增加主机的网络流量?

    eth0:192.168.30.130

    eth1:192.168.30.30

    如果以上是这样设置,那么理论上路由应该是:

    [root@www ~]# route -n 
    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    192.168.130.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1
    192.168.130.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

    就是说当主机发送数据包到192.168.130.0/24的网络时,都只会通过第一条规则,也是通过eth1来传出去;在响应数据包方面,不管是由eth0还是由还是由eth1进来的网络数据包,都会通过eth1来转发。

    这样带来的问题,尤其是一些防火墙的规则方面,因此,根本无法实现负载均衡,也不会增加网络流量的效果。甚至,还可能发生数据包传递错误的情况,所以,同一主机上面的设置相同的网络IP时,需要特别留意路由规则,一般来说,不应该设置同一网段的不同IP在同一台主机上面。

    路由器配置

    Linux内核提供的路由转发配置文件在/proc/sys/net/ipv4/ip_forward

    将其值设置为1,表示开启,0表示关闭,使用命令sysctl -p让设置立刻生效。

    静态路由

    待   写              

    动态路由

    常见的动态路由协议RIPv1、RIPv2、OSPF、BGP等。

    CentOS镜像提供了一个动态路由软件Quagga,首先安装

    [root@xueji ~]# yum install -y quagga
    [root@xueji ~]# ls -l /etc/quagga/ #quagga提供的各项动态路由协议都放置在该目录下,接下来以比较简单的RIPv2协议为例来处理胴体啊路由。
    总用量 36
    -rw-r--r--. 1 root   root      566 3月  23 2017 bgpd.conf.sample
    -rw-r--r--. 1 root   root     2801 3月  23 2017 bgpd.conf.sample2
    -rw-r--r--. 1 root   root     1110 3月  23 2017 ospf6d.conf.sample
    -rw-r--r--. 1 root   root      182 3月  23 2017 ospfd.conf.sample
    -rw-r--r--. 1 root   root      406 3月  23 2017 ripd.conf.sample
    -rw-r--r--. 1 root   root      390 3月  23 2017 ripngd.conf.sample
    -rw-r-----. 1 quagga quaggavt    0 9月  12 19:33 vtysh.conf
    -rw-r--r--. 1 quagga quaggavt  128 3月  23 2017 vtysh.conf.sample
    -rw-r-----. 1 quagga quagga     15 9月  12 19:33 zebra.conf
    -rw-r--r--. 1 root   root      369 3月  23 2017 zebra.conf.sample

    值得注意的是:无路启动什么动态路由协议,都必须先启动zebra,因为:

    1.zebra这个daemon的功能是更新内核的路由规则。

    2.RIP这个daemon则是用于向附近的其他Router沟通协调路由规则的传送与否。

    动态路由练习原理图示:

    实验会用到四台主机:Router 1和Router 2,PC Router 1和PC Router 2,将这四台主机IP按图所示设置。

    在Router 1和2上设置zebra

    #设置zebra并启动zebra服务
    [root@xueji ~]# cp /etc/quagga/zebra.conf{,.bak}
    [root@xueji ~]# vim /etc/quagga/zebra.conf
    hostname xueji-01 //默认读取的是当前主机名,该选项可以随意设置
    password linux123  //设置一个密码
    enable password linux123  //使设置的密码生效
    log file /var/log/quagga/zebra.log  //将所有的zebra产生的信息存到日志文件中
    #启动zebra服务
    [root@xueji ~]# /etc/init.d/zebra start 
    启动zebra: [确定]
    [root@xueji ~]# netstat -lntup | grep zebra
    tcp 0 0 127.0.0.1:2601 0.0.0.0:* LISTEN 33601/zebra

    由于zebra这个服务的任务主要是修改Linux系统内核内的路由,所以它仅监听本机接口,并不会监听外部的接口,另外,在zebra.conf这个文件中,我们上面所设置的密码是用于登录zebra使用的。查看2601端口是否启动

    [root@xueji ~]# telnet localhost 2601
    Trying ::1...
    telnet: connect to address ::1: Connection refused
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    
    Hello, this is Quagga (version 0.99.15).
    Copyright 1996-2005 Kunihiro Ishiguro, et al.
    
    
    User Access Verification
    
    Password:  //输入刚才设置的密码linux123
    xueji-01>  //输入问号“?”或者help
    echo      Echo a message back to the vty
      enable    Turn on privileged mode command
      exit      Exit current mode and down to previous mode
      help      Description of the interactive help system
      list      Print command list
      quit      Exit current mode and down to previous mode
      show      Show running system information
      terminal  Set terminal line parameters
      who       Display who is on vty
    xueji-01> list
      enable
      exit
      help
      list
      quit
      show debugging zebra
      show history
      show interface [IFNAME]
      show ip forwarding
      show ip prefix-list
    ......省略......
    xueji-01> show ip route
    Codes: K - kernel route, C - connected, S - static, R - RIP, O - OSPF,
           I - ISIS, B - BGP, > - selected route, * - FIB route
    
    K>* 0.0.0.0/0 via 192.168.30.2, eth0
    C>* 127.0.0.0/8 is directly connected, lo
    K>* 169.254.0.0/16 is directly connected, eth0
    C>* 192.168.30.0/24 is directly connected, eth0
    xueji-01> exit
    Connection closed by foreign host.
    含义说明:
    K>代表以类似route命令加入内核的路由规则,包括route-ethN所产生的规则
    C>代表由网络接口所设置的IP而产生的相关路由规则
    S>以zebra功能所设置的静态路由信息
    R>通过RIP协议增加的路由规则

    如果想要增加10.0.0.0/24给eth0,除了route命令之外,也可以更改zebra.conf文件

    [root@xueji ~]# vim /etc/quagga/zebra.conf
    [root@xueji ~]# cat /etc/quagga/zebra.conf
    .......末尾添加.....
    ip route 10.0.0.0/24 eth0
    [root@xueji ~]# /etc/init.d/zebra restart
    [root@xueji ~]# telnet localhost 2601
    Trying ::1...
    telnet: connect to address ::1: Connection refused
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    
    Hello, this is Quagga (version 0.99.15).
    Copyright 1996-2005 Kunihiro Ishiguro, et al.
    
    
    User Access Verification
    
    Password: 
    xueji-01> show ip route
    Codes: K - kernel route, C - connected, S - static, R - RIP, O - OSPF,
           I - ISIS, B - BGP, > - selected route, * - FIB route
    
    K>* 0.0.0.0/0 via 192.168.30.2, eth0
    S>* 10.0.0.0/24 [1/0] is directly connected, eth0
    C>* 127.0.0.0/8 is directly connected, lo
    K>* 169.254.0.0/16 is directly connected, eth0
    C>* 192.168.30.0/24 is directly connected, eth0
    多出来的一条路由规则显示S,表示静态路由。

    Router 2跟Router 1设置原理一样,只是主机名与密码修改下即可。

     接着,在两台Router上设置Ripd服务

    [root@xueji ~]# cp /etc/quagga/ripd.conf.sample /etc/quagga/ripd.conf
    [root@xueji ~]# vim !$
    vim /etc/quagga/ripd.conf
    ........省略......
    hostname xueji-01
    password linux123
    !
    debug rip events
    debug rip packet
    !
    ......省略......
    router rip
     version 2
     network 192.168.30.0/24
     network 192.168.12.0/24
     interface eth0
     no ip rip authentication mode
    ....省略.....
    [root@xueji ~]# /etc/init.d/ripd start
    启动ripd:                                                  [确定]
    [root@xueji ~]# chkconfig ripd on
    [root@xueji ~]# netstat -lntup | grep ripd
    tcp        0      0 127.0.0.1:2602              0.0.0.0:*                   LISTEN      33866/ripd          
    udp        0      0 0.0.0.0:520                 0.0.0.0:*                               33866/ripd
    #注意,新版本的quagga启动的2602尽在127.0.0.1主机中,是通过520来传递信息

    检查RIP协议的沟通结果

    (这里没有实现,有待解决!不清楚NiaoGe是虚拟环境的网络设置)

    最后一种特殊状况--路由两边界面是同一个IP网段:ARP Proxy

  • 相关阅读:
    Java BEAN与EJB的区别
    Java设计模式(22)命令模式(Command模式)
    Java设计模式(21)访问模式(Visitor者模式)
    Java设计模式(20)观察者模式(Observer模式)
    Java设计模式(19)状态模式(State模式)
    Java设计模式(18)策略模式(Strategy模式)
    Java设计模式(17)解释器模式(Interpreter模式)
    Java设计模式(16)中介模式(Mediator模式)
    Java设计模式(15)备忘录模式(Memento模式)
    C# SignalR 即时通讯 聊天室
  • 原文地址:https://www.cnblogs.com/zd520pyx1314/p/9630134.html
Copyright © 2011-2022 走看看