一:使用 route 命令添加
使用route 命令添加的路由,機器重啟或者網卡重啟后路由就失效了,方法:
//添加到主機的路由
# route add –host 192.168.1.11 dev eth0
# route add –host 192.168.1.12 gw 192.168.1.1
//添加到網絡的路由
# route add –net 192.168.1.11 netmask 255.255.255.0 eth0
# route add –net 192.168.1.11 netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 eth1
//添加默認網關
# route add default gw 192.168.2.1
//刪除路由
# route del –host 192.168.1.11 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.2.254
2.在/etc/sysconfig/network里添加到末尾
方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev
3./etc/sysconfig/static-routes : (沒有static-routes的話就手動建立一個這樣的文件)
any net 192.168.3.0/24 gw 192.168.3.254
any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129
4.開啟 IP 轉發:
# echo "1" >/proc/sys/net/ipv4/ip_forward (臨時)
# vi /etc/sysctl.conf --> net.ipv4.ip_forward=1 (永久開啟)
route命令解釋
用於顯示和操作IP路由表。要實現兩個不同的子網之間的通信,需要一台連接兩個網絡的路由器,或者同時位於兩個網絡的網關來實現。在Linux系統中,設置路由通常是 為了解決以下問題:該Linux系統在一個局域網中,局域網中有一個網關,能夠讓機器訪問Internet,那么就需要將這台機器的IP地址設置為 Linux機器的默認路由。要注意的是,直接在命令行下執行route命令來添加路由,不會永久保存,當網卡重啟或者機器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設置永久有效。
格式:route
格式:/sbin/route
用於打印路由表(display the current routing table)。
在非root用戶使用時需要使用完整路徑執行route命令。
route命令輸出的路由表字段含義如下:
Destination 目標
The destination network or destination host. 目標網絡或目標主機。
Gateway 網關
The gateway address or '*' if none set. 網關地址,如果沒有就顯示星號。
Genmask 網絡掩碼
The netmask for the destination net; '255.255.255.255' for a
host destination and '0.0.0.0' for the default route.
Flags Possible flags include 標志,常用的是U和G。
U (route is up) 路由啟用
H (target is a host) 目標是主機
G (use gateway) 使用網關
R (reinstate route for dynamic routing)
D (dynamically installed by daemon or redirect)
M (modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (reject route)
Metric 距離、跳數。暫無用。
The 'distance' to the target (usually counted in hops). It is
not used by recent kernels, but may be needed by routing dae-
mons.
Ref 不用管,恆為0。
Number of references to this route. (Not used in the Linux ker-nel.)
Use 該路由被使用的次數,可以粗略估計通向指定網絡地址的網絡流量。
Count of lookups for the route. Depending on the use of -F and
-C this will be either route cache misses (-F) or hits (-C).
Iface 接口,即eth0,eth0等網絡接口名
Interface to which packets for this route will be sent.
格式:route -n
格式:/sbin/route -n
用於打印路由表,加上-n參數就是在輸出的信息中不打印主機名而直接打印ip地址。
格式:route add default gw {IP-ADDRESS} {INTERFACE-NAME}
用於設置默認路由,其中,
參數{IP-ADDRESS): 用於指定路由器(網關)的IP地址;
參數{INTERFACE-NAME}: 用於指定接口名稱,如eth0。使用/sbin/ifconfig -a可以顯示所有接口信息。
例:route add default gw mango
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
添加到指定網絡的路由規則,其中
參數{NETWORK-ADDRESS}: 用於指定網絡地址
參數{NETMASK}: 用於指定子網掩碼
參數{INTERFACE-NAME}: 用於指定接口名稱,如eth0。
例1:route add -net 192.56.76.0 netmask 255.255.255.0 dev eth0
例2:route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
格式:route add -net {NETWORK-ADDRESS} netmask {NETMASK} reject
設置到指定網絡為不可達,避免在連接到這個網絡的地址時程序過長時間的等待,直接就知道該網絡不可達。
例:route add -net 10.0.0.0 netmask 255.0.0.0 reject
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} dev {INTERFACE-NAME}
格式:route del -net {NETWORK-ADDRESS} netmask {NETMASK} reject
用於刪除路由設置。參數指定的方式與route add相似。