Linux 內核的路由表
通過 route 命令查看 Linux 內核的路由表:
[root@VM_139_74_centos ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0
10.139.128.0 0.0.0.0 255.255.224.0 U 0 0 0 eth0
link-local 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-0ab63c131848
172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-bccbfb788da0
172.20.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-7485db25f958
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 10.139.128.1 0.0.0.0 UG 0 0 0 eth0
10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0
10.139.128.0 0.0.0.0 255.255.224.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-0ab63c131848
172.19.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-bccbfb788da0
172.20.0.0 0.0.0.0 255.255.0.0 U 0 0 0 br-7485db25f958
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
各列字段說明:
列 含義
Destination 目標網絡或目標主機。Destination 爲 default(0.0.0.0)時,表示這個是默認網關,所有數據都發到這個網關(這裏是 10.139.128.1)
Gateway 網關地址,0.0.0.0 表示當前記錄對應的 Destination 跟本機在同一個網段,通信時不需要經過網關
Genmask Destination 字段的網絡掩碼,Destination 是主機時需要設置爲 255.255.255.255,是默認路由時會設置爲 0.0.0.0
Flags 標記,含義參考表格後面的解釋
Metric 路由距離,到達指定網絡所需的中轉數,是大型局域網和廣域網設置所必需的 (不在Linux內核中使用。)
Ref 路由項引用次數 (不在Linux內核中使用。)
Use 此路由項被路由軟件查找的次數
Iface 網卡名字,例如 eth0
Flags 含義:
U 路由是活動的
H 目標是個主機
G 需要經過網關
R 恢復動態路由產生的表項
D 由路由的後臺程序動態地安裝
M 由路由的後臺程序修改
! 拒絕路由
Linux 內核的路由種類
主機路由
路由表中指向單個 IP 地址或主機名的路由記錄,其 Flags 字段爲 H。下面示例中,對於 10.0.0.10 這個主機,通過網關 10.139.128.1 網關路由:
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0
...
1
2
3
4
5
網絡路由
主機可以到達的網絡。下面示例中,對於 10.0.0.0/24 這個網絡,通過網關 10.139.128.1 網關路由:
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 10.139.128.1 255.255.255.0 UG 0 0 0 eth0
1
2
3
4
默認路由
當目標主機的 IP 地址或網絡不在路由表中時,數據包就被髮送到默認路由(默認網關)上。默認路由的 Destination 是 default 或 0.0.0.0。
[root@VM_139_74_centos ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 eth0
1
2
3
4
route 命令
route 命令可以顯示或設置 Linux 內核中的路由表,主要是靜態路由。
對於局域網中的 Linux 主機,要想訪問 Internet,需要將局域網的網關 IP 地址設置爲這個主機的默認路由。在命令行中通過 route 命令添加的路由在網卡重啓或機器重啓後失效。可以在 /etc/rc.local 中添加 route 命令來保證路由設置永久有效。
選項:
-A:設置地址類型
-C:打印 Linux 內核的路由緩存
-v:顯示詳細信息
-n:不執行 DNS 反向查找,直接顯示數字形式的 IP 地址
-e:netstat 格式顯示路由表
-net:到一個網絡的路由表
-host:到一個主機的路由表
參數:
add:增加路由記錄
del:刪除路由記錄
target:目的網絡或目的主機
gw:設置默認網關
mss:設置TCP的最大區塊長度(MSS),單位MB
window:指定通過路由表的TCP連接的TCP窗口大小
dev:路由記錄所表示的網絡接口
添加路由 add
可以添加一條可用路由,或添加一條要屏蔽的路由。
添加路由
添加主機路由
添加主機路由時,需要指定網絡 ID 和主機 ID,此時需要設置 netmask 255.255.255.255:
[root@VM_139_74_centos ~]# route add -net 10.0.0.10 netmask 255.255.255.255 gw 10.139.128.1 dev eth0
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.10 10.139.128.1 255.255.255.255 UGH 0 0 0 eth0
...
1
2
3
4
5
6
添加網絡路由
添加網絡路由時,只需指定網絡 ID,通過 netmask 設置掩碼長度:
[root@VM_139_74_centos ~]# route add -net 10.0.0.0 netmask 255.255.255.0 gw 10.139.128.1 dev eth0
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0 10.139.128.1 255.255.255.0 UG 0 0 0 eth0
...
1
2
3
4
5
6
添加添加同一個局域網的主機
不指定 gw 選項時,添加的路由記錄不使用網關:
[root@VM_139_74_centos ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
224.0.0.0 0.0.0.0 240.0.0.0 U 0 0 0 eth0
...
1
2
3
4
5
6
屏蔽路由
[root@VM_139_74_centos ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@VM_139_74_centos ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
224.0.0.0 - 240.0.0.0 ! 0 - 0 -
...
1
2
3
4
5
6
刪除路由記錄
跟添加路由類似,可以刪除一條可用路由,或刪除一條屏蔽的路由。
刪除可用路由
route del -net 224.0.0.0 netmask 240.0.0.0
1
刪除屏蔽的路由
route del -net 224.0.0.0 netmask 240.0.0.0 reject
1
刪除和添加設置默認網關
添加或刪除默認網關時,Linux 會自動檢查網關的可用性:
[root@VM_139_74_centos ~]# route add default gw 192.168.1.1
SIOCADDRT: Network is unreachable
[root@VM_139_74_centos ~]# route del default gw 192.168.1.1
SIOCDELRT: No such process
---------------------
原文:https://blog.csdn.net/kikajack/article/details/80457841