Linux如何添加路由

Linux如何添加路由
a.如何使用命令給Linux添加一個默認網關?
缺省網關路由:
默認網關就是數據包不匹配任何的路由規則,最後流經的地址關口!網關按字面意思就是網絡的關口
,就相當於我們辦公室的大門一樣,大家上班就要經過辦公室的門一樣。
使用route -n 查看網關信息,或者 netstat -rn查看路由
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
如何刪除路由表:
[root@machine1 ~]# route del default gw 192.168.0.1
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
配置方法:
1、setup
2、配置網卡配置文件:/etc/sysconfig/network-scripts/ifcfg-eth0
關鍵字:GATEWAY=192.168.0.
重啓網卡配置生效
3、route add default gw 192.168.0.1 #(臨時生效)
[root@machine1 ~]# route add default gw 192.168.0.1
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
如何添加網絡路由?
b.網絡路由:即去往某個網段的路由
一般多個網段之間相互通信,希望建立一條優先的路由,而不是通過默認的網關。拿家裏來說,
你要去衛生間有衛生間的門,去臥室有臥室的門。這兩個門就是網絡路由了。家裏的大門呢,就是
默認路由。


[root@machine1 ~]# route add -net 192.168.1.0 netmask 255.255.255.0 gw 192.168.0.1
route add -net [目的地] [網關]
[root@machine1 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.1.0     192.168.0.1     255.255.255.0   UG    0      0        0 eth0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth0
可以將路由添加命令加到rc.local裏,開機生效,但是重啓網卡配置文件不會生效。
或者配置路由配置文件/etc/sysconfig/network-scripts/route-eth0
如下字段:
192.168.1.0/24 via 192.168.0.1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章