Linux中添加靜態路由的方法(臨時的和永久的)

 

Linux下添加路由的方法:
  一:使用 route 命令添加
  使用route 命令添加的路由,機器重啓或者網卡重啓後路由就失效了,方法:
  //添加到主機的路由
  # route add –host 192.168.168.110 dev eth0
  # route add –host 192.168.168.119 gw 192.168.168.1
  //添加到網絡的路由
  # route add –net IP netmask MASK eth0
  # route add –net IP netmask MASK gw IP
  # route add –net IP/24 eth1
  //添加默認網關
  # route add default gw IP
  //刪除路由
  # route del –host 192.168.168.110 dev eth0
  二:在linux下設置永久路由的方法:
  1.在/etc/rc.local裏添加
  方法:
  route add -net 192.168.3.0/24 dev eth0
  route add -net 192.168.2.0/24 gw 192.168.3.254
  2.在/etc/sysconfig/network裏添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev

修改過的文件

/etc/rc.d/rc.local  文件
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.1.100 dev eth0
 
最後一行是自己添加的路由
說明:
route add:命令關鍵字,表示增加路由
-net:表示路由目標網段
netmask:表示路由目標爲網段時纔會使用到,表示路由目標網段的子網掩碼
gw:命令關鍵字,後面跟下一條網關
dev:命令關鍵字,後面跟具體設備名,表示路由是從該設備出去的
改完以後是需要重啓的   重啓命令是shutdown -r now
使用route -n命令查看是否添加上路由條目
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章