在RedHat(RHEL)/CentOS/Fedora Linux環境永久添加靜態路由通常是寫在諸如/etc/sysconfig/network-scripts/route-eth0這些文件里。在Debian下有所不同,我們會把這些添加路由的腳本放到/etc/network/interfaces里執行。如下面的例子:
auto eth0 iface eth0 inet static address 192.168.1.2 netmask 255.255.255.0 gateway 192.168.1.254 up route add -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1 down route del -net 192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1
或者
auto eth0 iface eth0 inet static address 192.168.0.10 netmask 255.255.255.0 gateway 192.168.0.1 up route add -net 192.168.2.0/24 gw 192.168.0.2 down route del -net 192.168.2.0/24 gw 192.168.0.2
經過一番折騰發現:新版本的debian由於系統用iproute2代替的老的net-tools,要使用以上配置需要先安裝net-tools,否則route命令是無效的,從而導致配置不成功。網上沒有找關於debian的新配置方法,於是自己結合debian以前的配置格式和centos的配置方式,得出以下結論,經試驗配置成功。
auto eth0 iface eth0 inet static address 192.168.0.10 netmask 255.255.255.0 gateway 192.168.0.1 up ip route add 192.168.2.0/24 via 192.168.0.2 down ip route del 192.168.2.0/24 via 192.168.0.2
ipv6靜態路由配置參考:
up route -A inet6 add 2002:db8:1::/64 gw 2001:da8:203:ec8:f74e:76ea:3ee:7704 dev eth0 down route -A inet6 add 2002:db8:1::/64 gw 2001:da8:203:ec8:f74e:76ea:3ee:7704 dev eth0
注:配置中down那一條表示此接口沒有啟用或者不可用時刪除這一條路由,其實是可以省略的。如果有不同意見,歡迎在剩余價值的博客下方留言討論。