一:使用route 命令添加
1、使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:
//添加到主机的路由
# route add –host 192.168.8.11 dev eth0
# route add –host 192.168.8.12 gw 192.168.8.1
//添加到网络的路由
# route add –net 192.168.8.11 netmask 255.255.255.0 dev eth0
# route add –net 192.168.8.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.8.0/24 dev eth1
# route add 10.15.150.0/24 via 192.168.150.253 dev eth0
//添加默认网关
# route add default gw 192.168.8.1
//删除路由
# route del –host 192.168.8.11 dev eth0
//屏蔽一条路由,增加一条屏蔽的路由,目的地址为224.x.x.x将被拒绝。
route add -net 224.0.0.0 netmask 240.0.0.0 reject
2、还可以使用ip命令来添加、删除路由
ip route add default via 172.16.10.2 dev eth0
ip route add 172.16.1.0/24 via 172.16.10.2 dev eth0
二:在Linux下设置永久路由的方法:
1.在/etc/rc.local里添加
方法:
route add -net 192.168.60.0/24 dev eth0
route add -net 192.168.63.0/24 gw 192.168.63.254
2.在/etc/sysconfig/network里添加到末尾
方法:
GATEWAY=gw-ip
或者
GATEWAY=gw-dev
3./etc/sysconfig/static-routes :
any net 192.168.63.0/24 gw 192.168.63.254
any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129
保存退出后,service network restart。
使用route -n或netstat -r,ip route show|column -t查看路由表。
语法
route(选项)(参数)
选项
-A:设置地址类型;
-C:打印将Linux核心的路由缓存;
-v:详细信息模式;
-n:不执行DNS反向查找,直接显示数字形式的IP地址;
-e:netstat格式显示路由表;
-net:到一个网络的路由表;
-host:到一个主机的路由表。
参数
Add:增加指定的路由记录;
Del:删除指定的路由记录;
Target:目的网络或目的主机;
gw:设置默认网关;
mss:设置TCP的最大区块长度(MSS),单位MB;
window:指定通过路由表的TCP连接的TCP窗口大小;
dev:路由记录所表示的网络接口。