ubuntu配置靜态路由及重新開機生效
# route add -host 192.168.1.123 dev eth0
# route add -host 192.168.1.123 gw 192.168.1.1
# route add -net 192.168.1.123 netmask 255.255.255.0 eth0
# route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1
# route add -net 192.168.1.123 netmask 255.255.255.0 gw 192.168.1.1 eth1
# route add -net 192.168.1.0/24 eth1
# route add default gw 192.168.1.1
# route del -host 192.168.1.11 dev eth0
# route del -net 192.168.1.123 netmask 255.255.255.0
修改/etc/rc.local,隻是要注意的一點是不要解除安裝 "exit 0"的後面
vi /etc/rc.local
route add default gw 192.168.1.1
這樣在系統啟動的時候會自動加入相關的路由設定
注:
(1)如果某個系統服務,比如說是NFS服務,這個服務是在啟動network服務之後,在執行rc.local之前,如果你設定的有自動挂載的nfs,那麼,這裡鍊路的不通暢,會造成挂載的失敗。
(2)如果你重新開機了網絡伺服器,那麼路由就失效了,這個時候你不得不重新加載這個檔案,但是如果你是遠端操作的呢?是以,這個方法不是非常的不推薦
up route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.1.1
可以看到這個格式和route指令的唯一差別在于,在前面多了一個up
下面列出我的interface檔案僅供參考,由于安全因素,這裡的ip我都用xx替代了:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address xxx.xxx.xxx.xxx
netmask xxx.xxx.xxx.xxx
gateway xxx.xxx.xxx.xxx
auto eth0:0
iface eth0:0 inet static
#dns-nameservers 202.102.224.68 202.102.227.68
up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0
network裡添加到檔案末尾,格式如下
vi /etc/sysconfig/network
GATEWAY=gw-ip 或者 GATEWAY=gw-dev
注:這種方式從外觀上來看隻能用于添加預設網關了,對于多路網絡環境貌似不太适合的樣子
在配置ip位址的時候直接将GATEWAY的配置寫入ifcfg檔案,形式:GATEWAY=gw-ip
适合添加預設路由
預設在/etc/sysconifg目錄中是沒有這個檔案的,需要我們手工建立,對這個檔案的調用在下面:
/etc/init.d/network:
159 # Add non interface-specific static-routes.
160 if [ -f /etc/sysconfig/static-routes ]; then
161 grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
162 /sbin/route add -$args
163 done
<span style="font-fam
本文轉自 運維小當家 51CTO部落格,原文連結:http://blog.51cto.com/solin/1900865,如需轉載請自行聯系原作者