天天看點

ubuntu 設定靜态路由_ubuntu配置靜态路由及重新開機生效-阿裡雲開發者社群

ubuntu配置靜态路由及重新開機生效

第一種方法:使用route指令(添加臨時路由)

添加到主機的路由

# 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

修改/etc/rc.local,隻是要注意的一點是不要解除安裝 "exit 0"的後面

vi /etc/rc.local

route add default gw 192.168.1.1

這樣在系統啟動的時候會自動加入相關的路由設定

注:

(1)如果某個系統服務,比如說是NFS服務,這個服務是在啟動network服務之後,在執行rc.local之前,如果你設定的有自動挂載的nfs,那麼,這裡鍊路的不通暢,會造成挂載的失敗。

(2)如果你重新開機了網絡伺服器,那麼路由就失效了,這個時候你不得不重新加載這個檔案,但是如果你是遠端操作的呢?是以,這個方法不是非常的不推薦

第三種方法:修改interfaces檔案

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

address xxx.xxx.xxx.xxx

netmask xxx.xxx.xxx.xxx

#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

up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0

up route add -net xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx gw xxx.xxx.xxx.xxx eth0

第四種:在/etc/sysconfig/network

network裡添加到檔案末尾,格式如下

vi /etc/sysconfig/network

GATEWAY=gw-ip 或者 GATEWAY=gw-dev

注:這種方式從外觀上來看隻能用于添加預設網關了,對于多路網絡環境貌似不太适合的樣子

第五種:直接寫入ifcfg檔案

在配置ip位址的時候直接将GATEWAY的配置寫入ifcfg檔案,形式:GATEWAY=gw-ip

适合添加預設路由

第六種:寫入/etc/sysconfig/static-routes檔案

預設在/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

————————————————

版權聲明:本文為CSDN部落客「普通網友」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_39620629/article/details/111808305