溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux route 配置靜態(tài)路由(轉(zhuǎn)載)

發(fā)布時(shí)間:2020-08-10 19:12:17 來源:ITPUB博客 閱讀:313 作者:lhb_immortal 欄目:建站服務(wù)器


本文轉(zhuǎn)載自http://blog.csdn.net/vevenlcf/article/details/48026965 (由于顯示問題,做了一些小的修改)

1 查看主機(jī)路由

route 

route 命令的輸出項(xiàng)說明 

輸出項(xiàng) 說明
Destination 目標(biāo)網(wǎng)段或者主機(jī)
Gateway 網(wǎng)關(guān)地址,”*” 表示目標(biāo)是本主機(jī)所屬的網(wǎng)絡(luò),不需要路由
Genmask 網(wǎng)絡(luò)掩碼
Flags 標(biāo)記。一些可能的標(biāo)記如下:
  U — 路由是活動(dòng)的
  H — 目標(biāo)是一個(gè)主機(jī)
  G — 路由指向網(wǎng)關(guān)
  R — 恢復(fù)動(dòng)態(tài)路由產(chǎn)生的表項(xiàng)
  D — 由路由的后臺(tái)程序動(dòng)態(tài)地安裝
  M — 由路由的后臺(tái)程序修改
  ! — 拒絕路由
Metric 路由距離,到達(dá)指定網(wǎng)絡(luò)所需的中轉(zhuǎn)數(shù)(linux 內(nèi)核中沒有使用)
Ref 路由項(xiàng)引用次數(shù)(linux 內(nèi)核中沒有使用)
Use 此路由項(xiàng)被路由軟件查找的次數(shù)
Iface 該路由表項(xiàng)對(duì)應(yīng)的輸出接口

2 種路由類型

主機(jī)路由

主機(jī)路由是路由選擇表中指向單個(gè)IP地址或主機(jī)名的路由記錄。主機(jī)路由的Flags字段為H。例如,在下面的示例中,本地主機(jī)通過IP地址192.168.1.1的路由器到達(dá)IP地址為10.0.0.10的主機(jī)。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     -------            -----     ------    ---    ---    -----
10.0.0.10     192.168.1.1    255.255.255.255   UH       0    0      0    eth0
網(wǎng)絡(luò)路由

網(wǎng)絡(luò)路由是代表主機(jī)可以到達(dá)的網(wǎng)絡(luò)。網(wǎng)絡(luò)路由的Flags字段為N。例如,在下面的示例中,本地主機(jī)將發(fā)送到網(wǎng)絡(luò)192.19.12的數(shù)據(jù)包轉(zhuǎn)發(fā)到IP地址為192.168.1.1的路由器。

Destination    Gateway       Genmask Flags    Metric    Ref     Use    Iface
-----------    -------     -------         -----    -----   ---    ---    -----
192.19.12     192.168.1.1    255.255.255.0      UN      0       0     0    eth0
默認(rèn)路由

當(dāng)主機(jī)不能在路由表中查找到目標(biāo)主機(jī)的IP地址或網(wǎng)絡(luò)路由時(shí),數(shù)據(jù)包就被發(fā)送到默認(rèn)路由(默認(rèn)網(wǎng)關(guān))上。默認(rèn)路由的Flags字段為G。例如,在下面的示例中,默認(rèn)路由是IP地址為192.168.1.1的路由器。

Destination    Gateway       Genmask Flags     Metric    Ref    Use    Iface
-----------    -------     ------- -----      ------    ---    ---    -----
default       192.168.1.1     0.0.0.0    UG       0        0     0    eth0

配置靜態(tài)路由

route 命令

設(shè)置和查看路由表都可以用 route 命令,設(shè)置內(nèi)核路由表的命令格式是: 

# route  [add|del] [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]

其中: 

  • add : 添加一條路由規(guī)則 
  • del : 刪除一條路由規(guī)則 
  • -net : 目的地址是一個(gè)網(wǎng)絡(luò) 
  • -host : 目的地址是一個(gè)主機(jī) 
  • target : 目的網(wǎng)絡(luò)或主機(jī) 
  • netmask : 目的地址的網(wǎng)絡(luò)掩碼 
  • gw : 路由數(shù)據(jù)包通過的網(wǎng)關(guān) 
  • dev : 為路由指定的網(wǎng)絡(luò)接口 

route 命令使用舉例

添加到主機(jī)的路由 

# route add -host 192.168.1.2 dev eth0 
# route add -host 10.20.30.148 gw 10.20.30.40     #添加到10.20.30.148的網(wǎng)管

添加到網(wǎng)絡(luò)的路由 

# route add -net 10.20.30.40 netmask 255.255.255.248 eth0   #添加10.20.30.40的網(wǎng)絡(luò)
# route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #添加10.20.30.48的網(wǎng)絡(luò)
# route add -net 192.168.1.0/24 eth2

添加默認(rèn)路由 

# route add default gw 192.168.1.1

刪除路由 

# route del -host 192.168.1.2 dev eth0:0
# route del -host 10.20.30.148 gw 10.20.30.40
# route del -net 10.20.30.40 netmask 255.255.255.248 eth0
# route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route del -net 192.168.1.0/24 eth2
# route del default gw 192.168.1.1

設(shè)置包轉(zhuǎn)發(fā)

在 CentOS 中默認(rèn)的內(nèi)核配置已經(jīng)包含了路由功能,但默認(rèn)并沒有在系統(tǒng)啟動(dòng)時(shí)啟用此功能。開啟 Linux 的路由功能可以通過調(diào)整內(nèi)核的網(wǎng)絡(luò)參數(shù)來實(shí)現(xiàn)。要配置和調(diào)整內(nèi)核參數(shù)可以使用 sysctl 命令。例如:要開啟 Linux 內(nèi)核的數(shù)據(jù)包轉(zhuǎn)發(fā)功能可以使用如下的命令。

# sysctl -w net.ipv4.ip_forward=1

這樣設(shè)置之后,當(dāng)前系統(tǒng)就能實(shí)現(xiàn)包轉(zhuǎn)發(fā),但下次啟動(dòng)計(jì)算機(jī)時(shí)將失效。為了使在下次啟動(dòng)計(jì)算機(jī)時(shí)仍然有效,需要將下面的行寫入配置文件/etc/sysctl.conf。

# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1

用戶還可以使用如下的命令查看當(dāng)前系統(tǒng)是否支持包轉(zhuǎn)發(fā)。 

# sysctl net.ipv4.ip_forward


route 命令:


Linux系統(tǒng)的route命令用于顯示和操作IP路由表(show / manipulate the IP routing table)。要實(shí)現(xiàn)兩個(gè)不同的子網(wǎng)之間的通信,需要一臺(tái)連接兩個(gè)網(wǎng)絡(luò)的路由器,或者同時(shí)位于兩個(gè)網(wǎng)絡(luò)的網(wǎng)關(guān)來實(shí)現(xiàn)。在Linux系統(tǒng)中,設(shè)置路由通常是為了解決以下問題:該Linux系統(tǒng)在一個(gè)局域網(wǎng)中,局域網(wǎng)中有一個(gè)網(wǎng)關(guān),能夠讓機(jī)器訪問Internet,那么就需要將這臺(tái)機(jī)器的IP地址設(shè)置為Linux機(jī)器的默認(rèn)路由。要注意的是,直接在命令行下執(zhí)行route命令來添加路由,不會(huì)永久保存,當(dāng)網(wǎng)卡重啟或者機(jī)器重啟之后,該路由就失效了;可以在/etc/rc.local中添加route命令來保證該路由設(shè)置永久有效。

1.命令格式:

route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] 

2.命令功能:

Route命令是用于操作基于內(nèi)核ip路由表,它的主要作用是創(chuàng)建一個(gè)靜態(tài)路由讓指定一個(gè)主機(jī)或者一個(gè)網(wǎng)絡(luò)通過一個(gè)網(wǎng)絡(luò)接口,如eth0。當(dāng)使用"add"或者"del"參數(shù)時(shí),路由表被修改,如果沒有參數(shù),則顯示路由表當(dāng)前的內(nèi)容。

3.命令參數(shù):

-c 顯示更多信息

-n 不解析名字

-v 顯示詳細(xì)的處理信息

-F 顯示發(fā)送信息

-C 顯示路由緩存

-f 清除所有網(wǎng)關(guān)入口的路由表。 

-p 與 add 命令一起使用時(shí)使路由具有永久性。


add:添加一條新路由。

del:刪除一條路由。

-net:目標(biāo)地址是一個(gè)網(wǎng)絡(luò)。

-host:目標(biāo)地址是一個(gè)主機(jī)。

netmask:當(dāng)添加一個(gè)網(wǎng)絡(luò)路由時(shí),需要使用網(wǎng)絡(luò)掩碼。

gw:路由數(shù)據(jù)包通過網(wǎng)關(guān)。注意,你指定的網(wǎng)關(guān)必須能夠達(dá)到。

metric:設(shè)置路由跳數(shù)。


Command 指定您想運(yùn)行的命令 (Add/Change/Delete/Print)。 

Destination 指定該路由的網(wǎng)絡(luò)目標(biāo)。 

mask Netmask 指定與網(wǎng)絡(luò)目標(biāo)相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。 

Gateway 指定網(wǎng)絡(luò)目標(biāo)定義的地址集和子網(wǎng)掩碼可以到達(dá)的前進(jìn)或下一躍點(diǎn) IP 地址。 

metric Metric 為路由指定一個(gè)整數(shù)成本值標(biāo)(從 1 至 9999),當(dāng)在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標(biāo)地址最匹配)的多個(gè)路由中進(jìn)行選擇時(shí)可以使用。 

if Interface 為可以訪問目標(biāo)的接口指定接口索引。若要獲得一個(gè)接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能??梢允褂檬M(jìn)制或十六進(jìn)制值進(jìn)行接口索引。


4.使用實(shí)例:

實(shí)例1:顯示當(dāng)前路由

命令:

route

route -n

輸出:


Linux route 配置靜態(tài)路由(轉(zhuǎn)載)
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
0.0.0.0         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
Linux route 配置靜態(tài)路由(轉(zhuǎn)載)


說明:

第一行表示主機(jī)所在網(wǎng)絡(luò)的地址為192.168.120.0,若數(shù)據(jù)傳送目標(biāo)是在本局域網(wǎng)內(nèi)通信,則可直接通過eth0轉(zhuǎn)發(fā)數(shù)據(jù)包;

行表示數(shù)據(jù)傳送目的是訪問Internet,則由接口eth0,將數(shù)據(jù)包發(fā)送到網(wǎng)關(guān)192.168.120.240

其中Flags為路由標(biāo)志,標(biāo)記當(dāng)前網(wǎng)絡(luò)節(jié)點(diǎn)的狀態(tài)。

Flags標(biāo)志說明

U Up表示此路由當(dāng)前為啟動(dòng)狀態(tài)

H Host,表示此網(wǎng)關(guān)為一主機(jī)

G Gateway,表示此網(wǎng)關(guān)為一路由器

R Reinstate Route,使用動(dòng)態(tài)路由重新初始化的路由

D Dynamically,此路由是動(dòng)態(tài)性地寫入

M Modified,此路由是由路由守護(hù)程序或?qū)蚱鲃?dòng)態(tài)修改

! 表示此路由當(dāng)前為關(guān)閉狀態(tài)


備注:

route -n (-n 表示不解析名字,列出速度會(huì)比route 快)


實(shí)例2:添加網(wǎng)關(guān)/設(shè)置網(wǎng)關(guān)

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

輸出:

Linux route 配置靜態(tài)路由(轉(zhuǎn)載)
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
Linux route 配置靜態(tài)路由(轉(zhuǎn)載)

[root@localhost ~]#  

說明:

增加一條 到達(dá)244.0.0.0的路由


實(shí)例3:屏蔽一條路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:


Linux route 配置靜態(tài)路由(轉(zhuǎn)載)
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
Linux route 配置靜態(tài)路由(轉(zhuǎn)載)


說明:

增加一條屏蔽的路由,目的地址為 224.x.x.x 將被拒絕


實(shí)例4:刪除路由記錄

命令:

route del -net 224.0.0.0 netmask 240.0.0.0

route del -net 224.0.0.0 netmask 240.0.0.0 reject

輸出:


Linux route 配置靜態(tài)路由(轉(zhuǎn)載)
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
Linux route 配置靜態(tài)路由(轉(zhuǎn)載)


說明:


實(shí)例5:刪除和添加設(shè)置默認(rèn)網(wǎng)關(guān)

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

輸出:


Linux route 配置靜態(tài)路由(轉(zhuǎn)載)
[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# 
Linux route 配置靜態(tài)路由(轉(zhuǎn)載)



[cpp] view plain copy
  1. 顯示現(xiàn)在所有路由  
  2.   
  3.   #route -n  
  4.   
  5.   root@Ubuntu:~# route  
  6.   
  7.   Kernel IP routing table  
  8.   
  9.   Destination Gateway Genmask Flags Metric Ref Use Iface  
  10.   
  11.   10.147.9.0 * 255.255.255.0 U 1 0 0 eth0  
  12.   
  13.   192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0  
  14.   
  15.   192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0  
  16.   
  17.   link-local * 255.255.0.0 U 1000 0 0 eth0  
  18.   
  19.   192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 wlan0  
  20.   
  21.   default 10.147.9.1 0.0.0.0 UG 0 0 0 eth0  
  22.   
  23.   root@Ubuntu:~#  
  24.   
  25.   結(jié)果是自上而下, 就是說, 哪條在前面, 哪條就有優(yōu)先, 前面都沒有, 就用最后一條default  
  26.   
  27.   舉例, 添加一條路由(發(fā)往192.168.62這個(gè)網(wǎng)段的全部要經(jīng)過網(wǎng)關(guān)192.168.1.1)  
  28.   
  29.   route add -net 192.168.62.0 netmask 255.255.255.0 gw 192.168.1.1  
  30.   
  31.   刪除一條路由  
  32.   
  33.   route del -net 192.168.122.0 netmask 255.255.255.0  
  34.   
  35.   刪除的時(shí)候不用寫網(wǎng)關(guān)  
  36.   
  37.   linux下添加路由的方法:  
  38.   
  39.   一:使用 route 命令添加  
  40.   
  41.   使用route 命令添加的路由,機(jī)器重啟或者網(wǎng)卡重啟后路由就失效了,方法:  
  42.   
  43.   //添加到主機(jī)的路由  
  44.   
  45.   # route add –host 192.168.168.110 dev eth0  
  46.   
  47.   # route add –host 192.168.168.119 gw 192.168.168.1  
  48.   
  49.   //添加到網(wǎng)絡(luò)的路由  
  50.   
  51.   # route add –net IP netmask MASK eth0  
  52.   
  53.   # route add –net IP netmask MASK gw IP  
  54.   
  55.   # route add –net IP/24 eth2  
  56.   
  57.   //添加默認(rèn)網(wǎng)關(guān)  
  58.   
  59.   # route add default gw IP  
  60.   
  61.   //刪除路由  
  62.   
  63.   # route del –host 192.168.168.110 dev eth0  
  64.   
  65.   二:在linux下設(shè)置永久路由的方法:  
  66.   
  67.   1.在/etc/rc.local里添加  
  68.   
  69.   方法:  
  70.   
  71.   route add -net 192.168.3.0/24 dev eth0  
  72.   
  73.   route add -net 192.168.2.0/24 gw 192.168.3.254  
  74.   
  75.   2.在/etc/sysconfig/network里添加到末尾  
  76.   
  77.   方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev  
  78.   
  79.   3./etc/sysconfig/static-router :  
  80.   
  81.   any net x.x.x.x/24 gw y.y.y.y  
  82.   
  83.   
  84. ------------------------------------------------------------------------------------------  
  85. --  Route命令的正確用法  
  86. 使用 Route 命令行工具查看并編輯計(jì)算機(jī)的 IP 路由表。Route 命令和語法如下所示:  
  87. route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]  
  88. -f 清除所有網(wǎng)關(guān)入口的路由表。    
  89. -p 與 add 命令一起使用時(shí)使路由具有永久性。   
  90. Command 指定您想運(yùn)行的命令 (Add/Change/Delete/Print)。   
  91. Destination 指定該路由的網(wǎng)絡(luò)目標(biāo)。    
  92. mask Netmask 指定與網(wǎng)絡(luò)目標(biāo)相關(guān)的網(wǎng)絡(luò)掩碼(也被稱作子網(wǎng)掩碼)。    
  93. Gateway 指定網(wǎng)絡(luò)目標(biāo)定義的地址集和子網(wǎng)掩碼可以到達(dá)的前進(jìn)或下一躍點(diǎn) IP 地址。    
  94. metric Metric 為路由指定一個(gè)整數(shù)成本值標(biāo)(從 1 至 ArrayArrayArrayArray),當(dāng)在路由表(與轉(zhuǎn)發(fā)的數(shù)據(jù)包目標(biāo)地址最匹配)的多個(gè)路由中進(jìn)行選擇時(shí)可以使用。    
  95. if Interface 為可以訪問目標(biāo)的接口指定接口索引。若要獲得一個(gè)接口列表和它們相應(yīng)的接口索引,使用 route print 命令的顯示功能。可以使用十進(jìn)制或十六進(jìn)制值進(jìn)行接口索引。   
  96. /?  在命令提示符處顯示幫助。    
  97. 示例  
  98. 若要顯示 IP 路由表的全部內(nèi)容,請(qǐng)鍵入:  
  99. route print  
  100. 若要顯示以 10. 起始的 IP 路由表中的路由,請(qǐng)鍵入:  
  101. route print 10.*  
  102. 若要添加帶有 1Array2.168.12.1 默認(rèn)網(wǎng)關(guān)地址的默認(rèn)路由,請(qǐng)鍵入:  
  103. route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1  
  104. 若要向帶有 255.255.0.0 子網(wǎng)掩碼和 10.27.0.1 下一躍點(diǎn)地址的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:  
  105. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1  
  106. 若要向帶有 255.255.0.0 子網(wǎng)掩碼和 10.27.0.1 下一躍點(diǎn)地址的 10.41.0.0 目標(biāo)中添加一個(gè)永久路由,請(qǐng)鍵入:  
  107. route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1  
  108. 若要向帶有 255.255.0.0 子網(wǎng)掩碼、10.27.0.1 下一躍點(diǎn)地址且其成本值標(biāo)為 7 的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:  
  109. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7  
  110. 若要向帶有 255.255.0.0 子網(wǎng)掩碼、10.27.0.1 下一躍點(diǎn)地址且使用 0x3 接口索引的 10.41.0.0 目標(biāo)中添加一個(gè)路由,請(qǐng)鍵入:  
  111. route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3  
  112. 若要?jiǎng)h除到帶有 255.255.0.0 子網(wǎng)掩碼的 10.41.0.0 目標(biāo)的路由,請(qǐng)鍵入:  
  113. route delete 10.41.0.0 mask 255.255.0.0  
  114. 若要?jiǎng)h除以 10. 起始的 IP 路由表中的所有路由,請(qǐng)鍵入:  
  115. route delete 10.*  
  116. 若要將帶有 10.41.0.0 目標(biāo)和 255.255.0.0 子網(wǎng)掩碼的下一躍點(diǎn)地址從 10.27.0.1 修改為 10.27.0.25,請(qǐng)鍵入:  
  117. route change 10.41.0.0 mask 255.255.0.0 10.27.0.25  
  118.   
  119. -------------------------------------------------------------------------  
  120.   首先,先了解傳統(tǒng)的網(wǎng)絡(luò)配置命令:  
  121.   1. 使用ifconfig命令配置并查看網(wǎng)絡(luò)接口情況  
  122.   示例1: 配置eth0的IP,同時(shí)激活設(shè)備:  
  123.   # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up  
  124.   示例2: 配置eth0別名設(shè)備 eth0:1 的IP,并添加路由  
  125.   # ifconfig eth0:1 192.168.4.2  
  126.   # route add –host 192.168.4.2 dev eth0:1  
  127.   示例3:激活(禁用)設(shè)備  
  128.   # ifconfig eth0:1 up(down)  
  129.   示例4:查看所有(指定)網(wǎng)絡(luò)接口配置  
  130.   # ifconfig (eth0)  
  131.   2. 使用route 命令配置路由表  
  132.   示例1:添加到主機(jī)路由  
  133.   # route add –host 192.168.4.2 dev eth0:1  
  134.   # route add –host 192.168.4.1 gw 192.168.4.250  
  135.   示例2:添加到網(wǎng)絡(luò)的路由  
  136.   # route add –net IP netmask MASK eth0  
  137.   # route add –net IP netmask MASK gw IP  
  138.   # route add –net IP/24 eth2  
  139.   示例3:添加默認(rèn)網(wǎng)關(guān)  
  140.   # route add default gw IP  
  141.   示例4:刪除路由  
  142.   # route del –host 192.168.4.1 dev eth0:1  
  143.   示例5:查看路由信息  
  144.   # route 或 route -n (-n 表示不解析名字,列出速度會(huì)比route 快)  
  145.   3.ARP 管理命令  
  146.   示例1:查看ARP緩存  
  147.   # arp  
  148.   示例2: 添加  
  149.   # arp –s IP MAC  
  150.   示例3: 刪除  
  151.   # arp –d IP  
  152.   4. ip是iproute2軟件包里面的一個(gè)強(qiáng)大的網(wǎng)絡(luò)配置工具,它能夠替代一些傳統(tǒng)的網(wǎng)絡(luò)管理工具。例如:ifconfig、route等,  
  153.   上面的示例完全可以用下面的ip命令實(shí)現(xiàn),而且ip命令可以實(shí)現(xiàn)更多的功能.下面介紹一些示例:  
  154.   4.0 ip命令的語法  
  155.   ip命令的用法如下:  
  156.   ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]  
  157.   4.1 ip link set--改變?cè)O(shè)備的屬性. 縮寫:set、s  
  158.   示例1:up/down 起動(dòng)/關(guān)閉設(shè)備。  
  159.   # ip link set dev eth0 up  
  160.   這個(gè)等于傳統(tǒng)的 # ifconfig eth0 up(down)  
  161.   示例2:改變?cè)O(shè)備傳輸隊(duì)列的長度。  
  162.   參數(shù):txqueuelen NUMBER或者txqlen NUMBER  
  163.   # ip link set dev eth0 txqueuelen 100  
  164.   示例3:改變網(wǎng)絡(luò)設(shè)備MTU(最大傳輸單元)的值。  
  165.   # ip link set dev eth0 mtu 1500  
  166.   示例4: 修改網(wǎng)絡(luò)設(shè)備的MAC地址。  
  167.   參數(shù): address LLADDRESS  
  168.   # ip link set dev eth0 address 00:01:4f:00:15:f1  
  169.   4.2 ip link show--顯示設(shè)備屬性. 縮寫:show、list、lst、sh、ls、l  
  170.   -s選項(xiàng)出現(xiàn)兩次或者更多次,ip會(huì)輸出更為詳細(xì)的錯(cuò)誤信息統(tǒng)計(jì)。  
  171.   示例:  
  172.   # ip -s -s link ls eth0  
  173.   eth0: mtu 1500 qdisc cbq qlen 100  
  174.   link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff  
  175.   RX: bytes packets errors dropped overrun mcast  
  176.   2449949362 2786187 0 0 0 0  
  177.   RX errors: length crc fifo missed  
  178.   0 0 0 0 0  
  179.   TX: bytes packets errors dropped carrier collsns  
  180.   178558497 1783946 332 0 332 35172  
  181.   TX errors: aborted fifo window heartbeat  
  182.   0 0 0 332  
  183.   這個(gè)命令等于傳統(tǒng)的 ifconfig eth0  
  184.   5.1 ip address add--添加一個(gè)新的協(xié)議地址. 縮寫:add、a  
  185.   示例1:為每個(gè)地址設(shè)置一個(gè)字符串作為標(biāo)簽。為了和Linux-2.0的網(wǎng)絡(luò)別名兼容,這個(gè)字符串必須以設(shè)備名開頭,接著一個(gè)冒號(hào),  
  186.   # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0  
  187.   示例2: 在以太網(wǎng)接口eth0上增加一個(gè)地址192.168.20.0,掩碼長度為24位(155.155.155.0),標(biāo)準(zhǔn)廣播地址,標(biāo)簽為eth0:Alias:  
  188.   # ip addr add 192.168.4.2/24 brd + dev eth2 label eth2:1  
  189.   這個(gè)命令等于傳統(tǒng)的: ifconfig eth2:1 192.168.4.2  
  190.   5.2 ip address delete--刪除一個(gè)協(xié)議地址. 縮寫:delete、del、d  
  191.   # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1  
  192.   5.3 ip address show--顯示協(xié)議地址. 縮寫:show、list、lst、sh、ls、l  
  193.   # ip addr ls eth0  
  194.   5.4.ip address flush--清除協(xié)議地址. 縮寫:flush、f  
  195.   示例1 : 刪除屬于私網(wǎng)10.0.0.0/8的所有地址:  
  196.   # ip -s -s a f to 10/8  
  197.   示例2 : 取消所有以太網(wǎng)卡的IP地址  
  198.   # ip -4 addr flush label "eth0"  
  199.   6. ip neighbour--neighbour/arp表管理命令  
  200.   縮寫 neighbour、neighbor、neigh、n  
  201.   命令 add、change、replace、delete、fulsh、show(或者list)  
  202.   6.1 ip neighbour add -- 添加一個(gè)新的鄰接條目  
  203.   ip neighbour change--修改一個(gè)現(xiàn)有的條目  
  204.   ip neighbour replace--替換一個(gè)已有的條目  
  205.   縮寫:add、a;change、chg;replace、repl  
  206.   示例1: 在設(shè)備eth0上,為地址10.0.0.3添加一個(gè)permanent ARP條目:  
  207.   # ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm  
  208.   示例2:把狀態(tài)改為reachable  
  209.   # ip neigh chg 10.0.0.3 dev eth0 nud reachable  
  210.   6.2.ip neighbour delete--刪除一個(gè)鄰接條目  
  211.   示例1:刪除設(shè)備eth0上的一個(gè)ARP條目10.0.0.3  
  212.   # ip neigh del 10.0.0.3 dev eth0  
  213.   6.3.ip neighbour show--顯示網(wǎng)絡(luò)鄰居的信息. 縮寫:show、list、sh、ls  
  214.   示例1: # ip -s n ls 193.233.7.254  
  215.   193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable  
  216.   6.4.ip neighbour flush--清除鄰接條目. 縮寫:flush、f  
  217.   示例1: (-s 可以顯示詳細(xì)信息)  
  218.   # ip -s -s n f 193.233.7.254  
  219.   7. 路由表管理  
  220.   7.1.縮寫 route、ro、r  
  221.   7.2.路由表  
  222.   從Linux-2.2開始,內(nèi)核把路由歸納到許多路由表中,這些表都進(jìn)行了編號(hào),編號(hào)數(shù)字的范圍是1到255。另外,  
  223.   為了方便,還可以在/etc/iproute2/rt_tables中為路由表命名。  
  224.   默認(rèn)情況下,所有的路由都會(huì)被插入到表main(編號(hào)254)中。在進(jìn)行路由查詢時(shí),內(nèi)核只使用路由表main。  
  225.   7.3.ip route add -- 添加新路由  
  226.   ip route change -- 修改路由  
  227.   ip route replace -- 替換已有的路由  
  228.   縮寫:add、a;change、chg;replace、repl  
  229.   示例1: 設(shè)置到網(wǎng)絡(luò)10.0.0/24的路由經(jīng)過網(wǎng)關(guān)193.233.7.65  
  230.   # ip route add 10.0.0/24 via 193.233.7.65  
  231.   示例2: 修改到網(wǎng)絡(luò)10.0.0/24的直接路由,使其經(jīng)過設(shè)備dummy  
  232.   # ip route chg 10.0.0/24 dev dummy  
  233.   示例3: 實(shí)現(xiàn)鏈路負(fù)載平衡.加入缺省多路徑路由,讓ppp0和ppp1分擔(dān)負(fù)載(注意:scope值并非必需,它只不過是告訴內(nèi)核,  
  234.   這個(gè)路由要經(jīng)過網(wǎng)關(guān)而不是直連的。實(shí)際上,如果你知道遠(yuǎn)程端點(diǎn)的地址,使用via參數(shù)來設(shè)置就更好了)。  
  235.   # ip route add default scope global nexthop dev ppp0 nexthop dev ppp1  
  236.   # ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1  
  237.   示例4: 設(shè)置NAT路由。在轉(zhuǎn)發(fā)來自192.203.80.144的數(shù)據(jù)包之前,先進(jìn)行網(wǎng)絡(luò)地址轉(zhuǎn)換,把這個(gè)地址轉(zhuǎn)換為193.233.7.83  
  238.   # ip route add nat 192.203.80.142 via 193.233.7.83  
  239.   示例5: 實(shí)現(xiàn)數(shù)據(jù)包級(jí)負(fù)載平衡,允許把數(shù)據(jù)包隨機(jī)從多個(gè)路由發(fā)出。weight 可以設(shè)置權(quán)重.  
  240.   # ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth2 weight 1  
  241.   7.4.ip route delete-- 刪除路由  
  242.   縮寫:delete、del、d  
  243.   示例1:刪除上一節(jié)命令加入的多路徑路由  
  244.   # ip route del default scope global nexthop dev ppp0 nexthop dev ppp1  
  245.   7.5.ip route show -- 列出路由  
  246.   縮寫:show、list、sh、ls、l  
  247.   示例1: 計(jì)算使用gated/bgp協(xié)議的路由個(gè)數(shù)  
  248.   # ip route ls proto gated/bgp |wc  
  249.   1413 9891 79010  
  250.   示例2: 計(jì)算路由緩存里面的條數(shù),由于被緩存路由的屬性可能大于一行,以此需要使用-o選項(xiàng)  
  251.   # ip -o route ls cloned |wc  
  252.   159 2543 18707  
  253.   示例3: 列出路由表TABLEID里面的路由。缺省設(shè)置是table main。TABLEID或者是一個(gè)真正的路由表ID或者是/etc/iproute2/rt_tables文件定義的字符串,  
  254.   或者是以下的特殊值:  
  255.   all -- 列出所有表的路由;  
  256.   cache -- 列出路由緩存的內(nèi)容。  
  257.   ip ro ls 193.233.7.82 tab cache  
  258.   示例4: 列出某個(gè)路由表的內(nèi)容  
  259.   # ip route ls table fddi153  
  260.   示例5: 列出默認(rèn)路由表的內(nèi)容  
  261.   # ip route ls  
  262.   這個(gè)命令等于傳統(tǒng)的: route  
  263.   7.6.ip route flush -- 擦除路由表  
  264.   示例1: 刪除路由表main中的所有網(wǎng)關(guān)路由(示例:在路由監(jiān)控程序掛掉之后):  
  265.   # ip -4 ro flush scope global type unicast  
  266.   示例2:清除所有被克隆出來的IPv6路由:  
  267.   # ip -6 -s -s ro flush cache  
  268.   示例3: 在gated程序掛掉之后,清除所有的BGP路由:  
  269.   # ip -s ro f proto gated/bgp  
  270.   示例4: 清除所有ipv4路由cache  
  271.   # ip route flush cache  
  272.   *** IPv4 routing cache is flushed.  
  273.   7.7 ip route get -- 獲得單個(gè)路由 .縮寫:get、g  
  274.   使用這個(gè)命令可以獲得到達(dá)目的地址的一個(gè)路由以及它的確切內(nèi)容。  
  275.   ip route get命令和ip route show命令執(zhí)行的操作是不同的。ip route show命令只是顯示現(xiàn)有的路由,而ip route get命令在必要時(shí)會(huì)派生出新的路由。  
  276.   示例1: 搜索到193.233.7.82的路由  
  277.   # ip route get 193.233.7.82  
  278.   193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt 300  
  279.   示例2: 搜索目的地址是193.233.7.82,來自193.233.7.82,從eth0設(shè)備到達(dá)的路由(這條命令會(huì)產(chǎn)生一條非常有意思的路由,這是一條到193.233.7.82的回環(huán)路由)  
  280.   # ip r g 193.233.7.82 from 193.233.7.82 iif eth0  
  281.   193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac/inr.ac  
  282.   cache   
  283.  mtu 1500 rtt 300 iif eth0  
  284.   8. ip route -- 路由策略數(shù)據(jù)庫管理命令  
  285.   命令  
  286.   add、delete、show(或者list)  
  287.   注意:策略路由(policy routing)不等于路由策略(rouing policy)。  
  288.   在某些情況下,我們不只是需要通過數(shù)據(jù)包的目的地址決定路由,可能還需要通過其他一些域:源地址、IP協(xié)議、傳輸層端口甚至數(shù)據(jù)包的負(fù)載。  
  289.   這就叫做:策略路由(policy routing)。  
  290.   8.1. ip rule add -- 插入新的規(guī)則  
  291.   ip rule delete -- 刪除規(guī)則  
  292.   縮寫:add、a;delete、del、d  
  293.   示例1: 通過路由表inr.ruhep路由來自源地址為192.203.80/24的數(shù)據(jù)包  
  294.   ip ru add from 192.203.80/24 table inr.ruhep prio 220  
  295.   示例2:把源地址為193.233.7.83的數(shù)據(jù)報(bào)的源地址轉(zhuǎn)換為192.203.80.144,并通過表1進(jìn)行路由  
  296.   ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320  
  297.   示例3:刪除無用的缺省規(guī)則  
  298.   ip ru del prio 32767  
  299.   8.2. ip rule show -- 列出路由規(guī)則  
  300.   縮寫:show、list、sh、ls、l  
  301.   示例1: # ip ru ls  
  302.   0: from all lookup local  
  303.   32762: from 192.168.4.89 lookup fddi153  
  304.   32764: from 192.168.4.88 lookup fddi153  
  305.   32766: from all lookup main  
  306.   32767: from all lookup 253  
  307.   9. ip maddress -- 多播地址管理  
  308.   縮寫:show、list、sh、ls、l  
  309.   9.1.ip maddress show -- 列出多播地址  
  310.   示例1: # ip maddr ls dummy  
  311.   9.2. ip maddress add -- 加入多播地址  
  312.   ip maddress delete -- 刪除多播地址  
  313.   縮寫:add、a;delete、del、d  
  314.   使用這兩個(gè)命令,我們可以添加/刪除在網(wǎng)絡(luò)接口上監(jiān)聽的鏈路層多播地址。這個(gè)命令只能管理鏈路層地址。  
  315.   示例1: 增加 # ip maddr add 33:33:00:00:00:01 dev dummy  
  316.   示例2: 查看 # ip -O maddr ls dummy  
  317.   2: dummy  
  318.   link 33:33:00:00:00:01 users 2 static  
  319.   link 01:00:5e:00:00:01  
  320.   示例3: 刪除 # ip maddr del 33:33:00:00:00:01 dev dummy  
  321.   10.ip mroute -- 多播路由緩存管理  
  322.   10.1. ip mroute show -- 列出多播路由緩存條目  
  323.   縮寫:show、list、sh、ls、l  
  324.   示例1:查看 # ip mroute ls  
  325.   (193.232.127.6, 224.0.1.39) Iif: unresolved  
  326.   (193.232.244.34, 224.0.1.40) Iif: unresolved  
  327.   (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg  
  328.   示例2:查看 # ip -s mr ls 224.66/16  
  329.   (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg  
  330.   9383 packets, 300256 bytes  
  331.   11. ip tunnel -- 通道配置  
  332.   縮寫  
  333.   tunnel、tunl  
  334.   11.1.ip tunnel add -- 添加新的通道  
  335.   ip tunnel change -- 修改現(xiàn)有的通道  
  336.   ip tunnel delete -- 刪除一個(gè)通道  
  337.   縮寫:add、a;change、chg;delete、del、d  
  338.   示例1:建立一個(gè)點(diǎn)對(duì)點(diǎn)通道,最大TTL是32  
  339.   # ip tunnel add Cisco mode sit remote 192.31.7.104 local 192.203.80.1 ttl 32  
  340.   11.2.ip tunnel show -- 列出現(xiàn)有的通道  
  341.   縮寫:show、list、sh、ls、l  
  342.   示例1: # ip -s tunl ls Cisco  
  343.   12. ip monitor和rtmon -- 狀態(tài)監(jiān)視  
  344.   ip命令可以用于連續(xù)地監(jiān)視設(shè)備、地址和路由的狀態(tài)。這個(gè)命令選項(xiàng)的格式有點(diǎn)不同,命令選項(xiàng)的名字叫做monitor,接著是操作對(duì)象:  
  345.   ip monitor [ file FILE ] [ all | OBJECT-LIST ]  
  346.   示例1: # rtmon file /var/log/rtmon.log  
  347.   示例2: # ip monitor file /var/log/rtmon.log r  
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI