linux常識:添加靜態路由

方法一:使用命令route

//添加路由
# route add -host 192.168.0.221 dev eth0
# route add -host 192.168.1.221 gw 192.168.1.1
# route add -net 192.168.0.221/24 dev eth0
# route add -net 192.168.1.221/24 gw 192.168.1.1

//刪除路由
# route del -net 192.168.1.221/24 gw 192.168.1.1

add    增加路由
del    刪除路由
-net   設置到某個網段的路由
-host 設置到某臺主機的路由
gw     出口網關IP地址
dev    出口網關物理設備名

//增加默認路由(一條就夠了)
# route add default gw 192.168.0.1

//查看路由表
# route -n

方法二:使用命令ip route

//添加路由
# ip route add 192.168.0.0/24 via 192.168.0.1 dev eth0
# ip route add default via 192.168.1.1 dev eth0 src 192.168.1.221 table special

//刪除路由
# ip route del 192.168.0.0/24 via 192.168.0.1 dev eth0

ip route add   增加路由
ip route del   刪除路由
ip route change 更改路由表
ip route show   查看路由表
ip route flush 清空路由表
via   網關出口IP地址
dev 網關出口物理設備名
src 轉發數據包前設定該地址爲數據包源地址
table 指定添加到該表special

//增加默認路由
# ip route add default via 192.168.0.1 dev eth0

//查看路由信息
# ip route
# ip route show cache
# ip route show table special

設置開機後生效

1、保存路由設置,使其在網絡重啓後任然有效
# vi /etc/sysconfig/network-script/route-eth0
192.168.1.0/24 via 192.168.1.1

2、static-routes
/etc/rc.d/init.d/network中有這麼幾行:
# Add non interface-specific static-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep “^any” /etc/sysconfig/static-routes | while read ignore args ; do
/sbin/route add -$args
done
fi

也就是說,將靜態路由加到/etc/sysconfig/static-routes 文件中就行了。
如加入:
# route add -net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1
則static-routes的格式爲
any net 11.1.1.0 netmask 255.255.255.0 gw 11.1.1.1

參考鏈接:
http://linux-ip.net/html/tools-route.html
http://linux-ip.net/html/tools-ip-route.html

發佈了47 篇原創文章 · 獲贊 11 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章