平时工作经常需要配置路由,接下来总结一下linux服务器配置路由的方法。
1、首先看一下本机的网路环境网络信息及路由信息:ip addrroute -n。
2、接着使用route命令添加一条静态路由:route add -net 192.168.3.0 netmask 255.255.255.0 gw 192.168.2.1。
3、然后使用route命令添加一条路由,并指定eth1网卡:route add -net 192.168.4.0 netmask 255.255.255.0 gw 192.168.2.1 dev eth1。
4、接着使用route命令添加一条指向某一个主机的路由:route add -host 192.168.5.125 gw 192.168.2.1 。
5、最后使用ip命令添加一条静态路由,并指定从eth1网卡收发数据:ip route add 192.168.7.0/24 via 192.168.2.1 dev eth1,如下图所示添加静态路由就完成了。
1、查看本机路由信息[root@Router ~]# ip route ls
192.168.70.0/24 dev eth0 proto kernel scope link src 192.168.70.70
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.70
169.254.0.0/16 dev eth1 scope link
default via 192.168.1.1 dev eth0
2、确认是否需要改变默认路由
[root@Router ~]# ip route replace default via 192.168.70.254 dev eth0 table main
[root@Router ~]# ip route ls
192.168.70.0/24 dev eth0 proto kernel scope link src 192.168.70.70
192.168.1.0/24 dev eth1 proto kernel scope link src 192.168.1.70
169.254.0.0/16 dev eth1 scope link
default via 192.168.1.1 dev eth0
3、建立特殊路由表
[root@Router ~]# vi /etc/iproute2/rt_tables
255 local
254 main
253 default
200 test
4、向test路由表中添加它自己的默认路由
[root@Router ~]# ip route add default via 192.168.1.1 table test
注意:这个table test一定不要忘了写,否则写到了主路由表中
5、先看看机器当前的ip rule
[root@Router ~]# ip rule ls
0: from all lookup local
32766: from all lookup main
32767: from all lookup default
可以看到,规则中走了3个路由表,local、main、default
我们平常用route看到的,实际是路由表main
这些规则是按序号大小顺序走的,一个不同,则走下一个,知道通路或走完为止
6、添加路由到路由表test中
[root@Router ~]# ip rule add to 59.76.0.0/16 pref 10000 table test
这个意思是说,去向IP地址范围为59.76.0.0/16的访问,则启用test的路由表中的路由规则
而test的路由规则是什么呢?上面已经设置了,走的是202.196.x.1的路由.
现在再来看一下当前的ip rule
[root@Router ~]# ip rule ls
0: from all lookup local
10000: from all to 59.76.0.0/16 lookup test
32766: from all lookup main
32767: from all lookup default
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)