Linux 做网关
首先创建两张路由表,只需要添加到相应的文件中即可,Linux一共支持255个路由表,rt_tables文件中默认已经存在了三张路由表,分别是:255 local254 main253 default[root@localhost /]# echo "10 cnc" >>/etc/iproute2/rt_tables[root@localhost /]# echo "20 cernet" >>/etc/iproute2/rt_tables注意:路由表前面的数字只是编号并不代表优先级,路由表没有优先级,只有策略规则才有优先级。以下配置内容每次重启系统后都会消失,所以要把脚本设置为随系统一块启动。######################配置脚本################################!/bin/bash#加载iptables的nat和filter模块,iptables服务最好设置成在开机时自动运行modprobe iptable_natmodprobe iptable_filter#打开Linux内核包转发功能echo "1" > /proc/sys/net/ipv4/ip_forward #配置接口的IP地址,并激活接口#eth0连接联通线路,eth1连接教育网线路,eth2下连三层交换机#这里使用iproute2的新命令来配置IP,不在使用旧的命令如:ifconfigip address add 115.158.113.164/25 dev eth0ip link set dev eth0 upip address add 10.212.46.100/24 dev eth1ip link set dev eth1 upip address add 10.10.10.1/30 dev eth2ip link set dev eth2 up#向路由表中添加路由#向cnc路由表中添加一条默认路由ip route add default via 115.158.113.129 table cnc#向cernet路由表中添加一条默认路由ip route add default via 10.212.46.1 table cernet#向主路由表中添加指向内部网段的路由,不然数据包反回时找不到路由信息ip route add 192.168.100.0/24 via 10.10.10.2 table mainip route add 192.168.200.0/24 via 10.10.10.2 table main#设置路由规则rule,注意规则是按优先级来执行的。#所有目的地访问115.158.119.0/25网段的用户都走cernet线路出去。ip rule add from 0.0.0.0/0 to 115.158.119.0/25 table cernet pref 99#网段192.168.100.0/24的用户都走联通线路出去,优先级设置为100ip rule add from 192.168.100.0/24 table cnc pref 100#网段192.168.200.0/24的用户都走教育网线路出去,优先级设置为101ip rule add from 192.168.200.0/24 table cernet pref 101#刷新路由表,使新配置的路由生效ip route flush cache#按求对数据包进行NAT转换#把192.168.100.0/24网段的用户的源IP转换成联通线路接口的IPiptables -t nat -A POSTROUTING -s 192.168.100.0/24 -j SNAT --to 115.158.113.164iptables -t nat -A POSTROUTING -s 192.168.200.0/24 -j SNAT --to 10.212.46.100######################结束##################################
http://www.cnblogs.com/spiritman/

浙公网安备 33010602011771号