Thursday, January 8, 2009

route 笔记1

关于添加网关的一些汇总

  • Linux 部分
  • 网络环境:
  • 有两个 Class C 网络:
    有线Lan : 192.168.1.0/24
    无线Lan : 192.168.2.0/24

    主机中有两块网卡:
    网卡1: eth0 , 拥有一个 global IP 221.xxx.xxx.xxx
    网卡1: eth1 , 拥有一个 local IP 192.168.1.xxx

  • 设置目标
  • 设置主机使得:
    1) 从 global IP 来的 access 通过 default GW (Global IP GW) 返回 package
    2) 从 local IP 来的 access 通过 192.168.1.10 返回 package

    由于在 route add 命令中需要使用网关的 hostname,所以首先在 /etc/hosts 中定义 gateway 的名称。例如:

    192.168.1.10 gw8f
    查看现有的设置:
    # route
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    221.12.345.56 * 255.255.255.240 U 0 0 0 eth0
    192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
    169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
    default 221x114x249x193 0.0.0.0 UG 0 0 0 eth0
    然后添加route并确认:
    # route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.10 dev eth1
    #说明:来自192.168.2.0/24的package通过192.168.1.10网关返回package。
    # route
    Kernel IP routing table
    Destination Gateway Genmask Flags Metric Ref Use Iface
    221.12.345.56 * 255.255.255.240 U 0 0 0 eth0
    192.168.2.0 gw8f 255.255.255.0 UG 0 0 0 eth1
    192.168.1.0 * 255.255.255.0 U 0 0 0 eth1
    169.254.0.0 * 255.255.0.0 U 0 0 0 eth1
    default 221x114x249x193 0.0.0.0 UG 0 0 0 eth0

    保存设置在静态文件中:

    # vi /etc/sysconfig/network-scripts/route-eth1
    添加以下一行即可:
    192.168.2.0/24 via 192.168.1.10

    Windows

    route add 202.126.225.0 mask 255.255.255.0 192.168.1.5 metric 1 if eth1
    其他:
    如果有防火墙设置,需要修改一些设置。比如通过 Easy Firewall Generator 制作的 firewall 中需要添加:
    LOCAL_NET2="192.168.2.0/24"

    # Drop packets received on the external interface
    # claiming a source of the local network
    $IPT -A bad_packets -p ALL -i $INET_IFACE -s $LOCAL_NET2 -j LOG \
    --log-prefix "Illegal source: "

    $IPT -A bad_packets -p ALL -i $INET_IFACE -s $LOCAL_NET2 -j DROP

    # Rules for the private network (accessing gateway system itself)
    $IPT -A INPUT -p ALL -i $LOCAL_IFACE -s $LOCAL_NET2 -j ACCEPT

    参考资料:
    http://www.akadia.com/services/redhat_static_routes.html

    No comments: