路由配置(route IP 您所在的位置:网站首页 永久路由设置默认路由器 路由配置(route IP

路由配置(route IP

#路由配置(route IP| 来源: 网络整理| 查看: 265

转载于:http://blog.csdn.net/chenlycly/article/details/52141854

使用下面的 route 命令可以查看 Linux 内核路由表。

[cpp] view plain copy # route   Destination     Gateway         Genmask Flags Metric Ref    Use Iface   192.168.0.0     *               255.255.255.0   U     0      0        0 eth0   169.254.0.0     *               255.255.0.0     U     0      0        0 eth0   default         192.168.0.1     0.0.0.0         UG    0      0        0 eth0 

route 命令的输出项说明

输出项 说明

Destination 目标网段或者主机 Gateway 网关地址,”*” 表示目标是本主机所属的网络,不需要路由 Genmask 网络掩码 Flags 标记。一些可能的标记如下:   U — 路由是活动的   H — 目标是一个主机   G — 路由指向网关   R — 恢复动态路由产生的表项   D — 由路由的后台程序动态地安装   M — 由路由的后台程序修改   ! — 拒绝路由 Metric 路由距离,到达指定网络所需的中转数(linux 内核中没有使用) Ref 路由项引用次数(linux 内核中没有使用) Use 此路由项被路由软件查找的次数 Iface 该路由表项对应的输出接口 3 种路由类型 主机路由

主机路由是路由选择表中指向单个IP地址或主机名的路由记录。主机路由的Flags字段为H。例如,在下面的示例中,本地主机通过IP地址192.168.1.1的路由器到达IP地址为10.0.0.10的主机。

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 网络路由

网络路由是代表主机可以到达的网络。网络路由的Flags字段为N。例如,在下面的示例中,本地主机将发送到网络192.19.12的数据包转发到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 默认路由

当主机不能在路由表中查找到目标主机的IP地址或网络路由时,数据包就被发送到默认路由(默认网关)上。默认路由的Flags字段为G。例如,在下面的示例中,默认路由是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 配置静态路由 route 命令

设置和查看路由表都可以用 route 命令,设置内核路由表的命令格式是:

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

其中:

add : 添加一条路由规则 del : 删除一条路由规则 -net : 目的地址是一个网络 -host : 目的地址是一个主机 target : 目的网络或主机 netmask : 目的地址的网络掩码 gw : 路由数据包通过的网关 dev : 为路由指定的网络接口 route 命令使用举例

添加到主机的路由

[cpp] view plain copy print? # 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的网管   # 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的网管

 

添加到网络的路由

[cpp] view plain copy print? # route add -net 10.20.30.40 netmask 255.255.255.248 eth0   #添加10.20.30.40的网络   # route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #添加10.20.30.48的网络   # route add -net 192.168.1.0/24 eth1   # route add -net 10.20.30.40 netmask 255.255.255.248 eth0 #添加10.20.30.40的网络 # route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 #添加10.20.30.48的网络 # route add -net 192.168.1.0/24 eth1

 

添加默认路由

[cpp] view plain copy print? # route add default gw 192.168.1.1   # route add default gw 192.168.1.1

 

删除路由

[cpp] view plain copy print? # 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 eth1   # route del 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 eth1 # route del default gw 192.168.1.1 设置包转发

在 CentOS 中默认的内核配置已经包含了路由功能,但默认并没有在系统启动时启用此功能。开启 linux 的路由功能可以通过调整内核的网络参数来实现。要配置和调整内核参数可以使用 sysctl 命令。例如:要开启 Linux 内核的数据包转发功能可以使用如下的命令。

[cpp] view plain copy print? # sysctl -w net.ipv4.ip_forward=1   # sysctl -w net.ipv4.ip_forward=1

 

这样设置之后,当前系统就能实现包转发,但下次启动计算机时将失效。为了使在下次启动计算机时仍然有效,需要将下面的行写入配置文件/etc/sysctl.conf。

[cpp] view plain copy print? # vi /etc/sysctl.conf   net.ipv4.ip_forward = 1   # vi /etc/sysctl.conf net.ipv4.ip_forward = 1

 

用户还可以使用如下的命令查看当前系统是否支持包转发。

[cpp] view plain copy print? # sysctl net.ipv4.ip_forward   # sysctl net.ipv4.ip_forward

 

route 命令:

Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table)。要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或者同时位于两个网络的网关来实现。在Linux系统中,设置路由通常是为了解决以下问题:该Linux系统在一个局域网中,局域网中有一个网关,能够让机器访问Internet,那么就需要将这台机器的IP地址设置为Linux机器的默认路由。要注意的是,直接在命令行下执行route命令来添加路由,不会永久保存,当网卡重启或者机器重启之后,该路由就失效了;可以在/etc/rc.local中添加route命令来保证该路由设置永久有效。

1.命令格式:

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

2.命令功能:

Route命令是用于操作基于内核ip路由表,它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口,如eth0。当使用"add"或者"del"参数时,路由表被修改,如果没有参数,则显示路由表当前的内容。

3.命令参数:

-c 显示更多信息

-n 不解析名字

-v 显示详细的处理信息

-F 显示发送信息

-C 显示路由缓存

-f 清除所有网关入口的路由表。 

-p 与 add 命令一起使用时使路由具有永久性。

 

add:添加一条新路由。

del:删除一条路由。

-net:目标地址是一个网络。

-host:目标地址是一个主机。

netmask:当添加一个网络路由时,需要使用网络掩码。

gw:路由数据包通过网关。注意,你指定的网关必须能够达到。

metric:设置路由跳数。

 

Command 指定您想运行的命令 (Add/Change/Delete/Print)。 

Destination 指定该路由的网络目标。 

mask Netmask 指定与网络目标相关的网络掩码(也被称作子网掩码)。 

Gateway 指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点 IP 地址。 

metric Metric 为路由指定一个整数成本值标(从 1 至 9999),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。 

if Interface 为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用 route print 命令的显示功能。可以使用十进制或十六进制值进行接口索引。

 

4.使用实例:

实例1:显示当前路由

命令:

route

route -n

输出:

 

 

[cpp] view plain copy print? [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   [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

 

说明:

第一行表示主机所在网络的地址为192.168.120.0,若数据传送目标是在本局域网内通信,则可直接通过eth0转发数据包;

第四行表示数据传送目的是访问Internet,则由接口eth0,将数据包发送到网关192.168.120.240

其中Flags为路由标志,标记当前网络节点的状态。

Flags标志说明:

U Up表示此路由当前为启动状态

H Host,表示此网关为一主机

G Gateway,表示此网关为一路由器

R Reinstate Route,使用动态路由重新初始化的路由

D Dynamically,此路由是动态性地写入

M Modified,此路由是由路由守护程序或导向器动态修改

! 表示此路由当前为关闭状态

 

备注:

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

 

实例2:添加网关/设置网关

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

输出:

 

[cpp] view plain copy print? [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   [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

 

[root@localhost ~]#  

说明:

增加一条 到达244.0.0.0的路由

 

实例3:屏蔽一条路由

命令:

route add -net 224.0.0.0 netmask 240.0.0.0 reject

输出:

 

 

[cpp] view plain copy print? [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   [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

说明:

增加一条屏蔽的路由,目的地址为 224.x.x.x 将被拒绝

 

实例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

输出:

 

 

[cpp] view plain copy print? [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 ~]#    [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 ~]#

 

说明:

 

实例5:删除和添加设置默认网关

命令:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

输出:

 

[cpp] view plain copy print? [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 ~]#    [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 ~]#

 

[cpp] view plain copy print? 显示现在所有路由            #route -n            root@Ubuntu:~# route            Kernel IP routing table            Destination Gateway Genmask Flags Metric Ref Use Iface            10.147.9.0 * 255.255.255.0 U 1 0 0 eth0            192.168.1.0 * 255.255.255.0 U 2 0 0 wlan0            192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0            link-local * 255.255.0.0 U 1000 0 0 eth0            192.168.0.0 192.168.1.1 255.255.0.0 UG 0 0 0 wlan0            default 10.147.9.1 0.0.0.0 UG 0 0 0 eth0            root@Ubuntu:~#            结果是自上而下, 就是说, 哪条在前面, 哪条就有优先, 前面都没有, 就用最后一条default            举例, 添加一条路由(发往192.168.62这个网段的全部要经过网关192.168.1.1)            route add -net 192.168.62.0 netmask 255.255.255.0 gw 192.168.1.1            删除一条路由            route del -net 192.168.122.0 netmask 255.255.255.0            删除的时候不用写网关            linux下添加路由的方法:            一:使用 route 命令添加            使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:            //添加到主机的路由            # route add –host 192.168.168.110 dev eth0            # route add –host 192.168.168.119 gw 192.168.168.1            //添加到网络的路由            # route add –net IP netmask MASK eth0            # route add –net IP netmask MASK gw IP            # route add –net IP/24 eth1            //添加默认网关            # route add default gw IP            //删除路由            # route del –host 192.168.168.110 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.3.254            2.在/etc/sysconfig/network里添加到末尾            方法:GATEWAY=gw-ip 或者 GATEWAY=gw-dev            3./etc/sysconfig/static-router :            any net x.x.x.x/24 gw y.y.y.y               ------------------------------------------------------------------------------------------     --  Route命令的正确用法     使用 Route 命令行工具查看并编辑计算机的 IP 路由表。Route 命令和语法如下所示:     route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]]     -f 清除所有网关入口的路由表。       -p 与 add 命令一起使用时使路由具有永久性。      Command 指定您想运行的命令 (Add/Change/Delete/Print)。      Destination 指定该路由的网络目标。       mask Netmask 指定与网络目标相关的网络掩码(也被称作子网掩码)。       Gateway 指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点 IP 地址。       metric Metric 为路由指定一个整数成本值标(从 1 至 ArrayArrayArrayArray),当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。       if Interface 为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引,使用 route print 命令的显示功能。可以使用十进制或十六进制值进行接口索引。      /?  在命令提示符处显示帮助。       示例     若要显示 IP 路由表的全部内容,请键入:     route print     若要显示以 10. 起始的 IP 路由表中的路由,请键入:     route print 10.*     若要添加带有 1Array2.168.12.1 默认网关地址的默认路由,请键入:     route add 0.0.0.0 mask 0.0.0.0 1Array2.168.12.1     若要向带有 255.255.0.0 子网掩码和 10.27.0.1 下一跃点地址的 10.41.0.0 目标中添加一个路由,请键入:     route add 10.41.0.0 mask 255.255.0.0 10.27.0.1     若要向带有 255.255.0.0 子网掩码和 10.27.0.1 下一跃点地址的 10.41.0.0 目标中添加一个永久路由,请键入:     route -p add 10.41.0.0 mask 255.255.0.0 10.27.0.1     若要向带有 255.255.0.0 子网掩码、10.27.0.1 下一跃点地址且其成本值标为 7 的 10.41.0.0 目标中添加一个路由,请键入:     route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 metric 7     若要向带有 255.255.0.0 子网掩码、10.27.0.1 下一跃点地址且使用 0x3 接口索引的 10.41.0.0 目标中添加一个路由,请键入:     route add 10.41.0.0 mask 255.255.0.0 10.27.0.1 if 0x3     若要删除到带有 255.255.0.0 子网掩码的 10.41.0.0 目标的路由,请键入:     route delete 10.41.0.0 mask 255.255.0.0     若要删除以 10. 起始的 IP 路由表中的所有路由,请键入:     route delete 10.*     若要将带有 10.41.0.0 目标和 255.255.0.0 子网掩码的下一跃点地址从 10.27.0.1 修改为 10.27.0.25,请键入:     route change 10.41.0.0 mask 255.255.0.0 10.27.0.25          -------------------------------------------------------------------------       首先,先了解传统的网络配置命令:       1. 使用ifconfig命令配置并查看网络接口情况       示例1: 配置eth0的IP,同时激活设备:       # ifconfig eth0 192.168.4.1 netmask 255.255.255.0 up       示例2: 配置eth0别名设备 eth0:1 的IP,并添加路由       # ifconfig eth0:1 192.168.4.2       # route add –host 192.168.4.2 dev eth0:1       示例3:激活(禁用)设备       # ifconfig eth0:1 up(down)       示例4:查看所有(指定)网络接口配置       # ifconfig (eth0)       2. 使用route 命令配置路由表       示例1:添加到主机路由       # route add –host 192.168.4.2 dev eth0:1       # route add –host 192.168.4.1 gw 192.168.4.250       示例2:添加到网络的路由       # route add –net IP netmask MASK eth0       # route add –net IP netmask MASK gw IP       # route add –net IP/24 eth1       示例3:添加默认网关       # route add default gw IP       示例4:删除路由       # route del –host 192.168.4.1 dev eth0:1       示例5:查看路由信息       # route 或 route -n (-n 表示不解析名字,列出速度会比route 快)       3.ARP 管理命令       示例1:查看ARP缓存       # arp       示例2: 添加       # arp –s IP MAC       示例3: 删除       # arp –d IP       4. ip是iproute2软件包里面的一个强大的网络配置工具,它能够替代一些传统的网络管理工具。例如:ifconfig、route等,       上面的示例完全可以用下面的ip命令实现,而且ip命令可以实现更多的功能.下面介绍一些示例:       4.0 ip命令的语法       ip命令的用法如下:       ip [OPTIONS] OBJECT [COMMAND [ARGUMENTS]]       4.1 ip link set--改变设备的属性. 缩写:set、s       示例1:up/down 起动/关闭设备。       # ip link set dev eth0 up       这个等于传统的 # ifconfig eth0 up(down)       示例2:改变设备传输队列的长度。       参数:txqueuelen NUMBER或者txqlen NUMBER       # ip link set dev eth0 txqueuelen 100       示例3:改变网络设备MTU(最大传输单元)的值。       # ip link set dev eth0 mtu 1500       示例4: 修改网络设备的MAC地址。       参数: address LLADDRESS       # ip link set dev eth0 address 00:01:4f:00:15:f1       4.2 ip link show--显示设备属性. 缩写:show、list、lst、sh、ls、l       -s选项出现两次或者更多次,ip会输出更为详细的错误信息统计。       示例:       # ip -s -s link ls eth0       eth0: mtu 1500 qdisc cbq qlen 100       link/ether 00:a0:cc:66:18:78 brd ff:ff:ff:ff:ff:ff       RX: bytes packets errors dropped overrun mcast       2449949362 2786187 0 0 0 0       RX errors: length crc fifo missed       0 0 0 0 0       TX: bytes packets errors dropped carrier collsns       178558497 1783946 332 0 332 35172       TX errors: aborted fifo window heartbeat       0 0 0 332       这个命令等于传统的 ifconfig eth0       5.1 ip address add--添加一个新的协议地址. 缩写:add、a       示例1:为每个地址设置一个字符串作为标签。为了和Linux-2.0的网络别名兼容,这个字符串必须以设备名开头,接着一个冒号,       # ip addr add local 192.168.4.1/28 brd + label eth0:1 dev eth0       示例2: 在以太网接口eth0上增加一个地址192.168.20.0,掩码长度为24位(155.155.155.0),标准广播地址,标签为eth0:Alias:       # ip addr add 192.168.4.2/24 brd + dev eth1 label eth1:1       这个命令等于传统的: ifconfig eth1:1 192.168.4.2       5.2 ip address delete--删除一个协议地址. 缩写:delete、del、d       # ip addr del 192.168.4.1/24 brd + dev eth0 label eth0:Alias1       5.3 ip address show--显示协议地址. 缩写:show、list、lst、sh、ls、l       # ip addr ls eth0       5.4.ip address flush--清除协议地址. 缩写:flush、f       示例1 : 删除属于私网10.0.0.0/8的所有地址:       # ip -s -s a f to 10/8       示例2 : 取消所有以太网卡的IP地址       # ip -4 addr flush label "eth0"       6. ip neighbour--neighbour/arp表管理命令       缩写 neighbour、neighbor、neigh、n       命令 add、change、replace、delete、fulsh、show(或者list)       6.1 ip neighbour add -- 添加一个新的邻接条目       ip neighbour change--修改一个现有的条目       ip neighbour replace--替换一个已有的条目       缩写:add、a;change、chg;replace、repl       示例1: 在设备eth0上,为地址10.0.0.3添加一个permanent ARP条目:       # ip neigh add 10.0.0.3 lladdr 0:0:0:0:0:1 dev eth0 nud perm       示例2:把状态改为reachable       # ip neigh chg 10.0.0.3 dev eth0 nud reachable       6.2.ip neighbour delete--删除一个邻接条目       示例1:删除设备eth0上的一个ARP条目10.0.0.3       # ip neigh del 10.0.0.3 dev eth0       6.3.ip neighbour show--显示网络邻居的信息. 缩写:show、list、sh、ls       示例1: # ip -s n ls 193.233.7.254       193.233.7.254. dev eth0 lladdr 00:00:0c:76:3f:85 ref 5 used 12/13/20 nud reachable       6.4.ip neighbour flush--清除邻接条目. 缩写:flush、f       示例1: (-s 可以显示详细信息)       # ip -s -s n f 193.233.7.254       7. 路由表管理       7.1.缩写 route、ro、r       7.2.路由表       从Linux-2.2开始,内核把路由归纳到许多路由表中,这些表都进行了编号,编号数字的范围是1到255。另外,       为了方便,还可以在/etc/iproute2/rt_tables中为路由表命名。       默认情况下,所有的路由都会被插入到表main(编号254)中。在进行路由查询时,内核只使用路由表main。       7.3.ip route add -- 添加新路由       ip route change -- 修改路由       ip route replace -- 替换已有的路由       缩写:add、a;change、chg;replace、repl       示例1: 设置到网络10.0.0/24的路由经过网关193.233.7.65       # ip route add 10.0.0/24 via 193.233.7.65       示例2: 修改到网络10.0.0/24的直接路由,使其经过设备dummy       # ip route chg 10.0.0/24 dev dummy       示例3: 实现链路负载平衡.加入缺省多路径路由,让ppp0和ppp1分担负载(注意:scope值并非必需,它只不过是告诉内核,       这个路由要经过网关而不是直连的。实际上,如果你知道远程端点的地址,使用via参数来设置就更好了)。       # ip route add default scope global nexthop dev ppp0 nexthop dev ppp1       # ip route replace default scope global nexthop dev ppp0 nexthop dev ppp1       示例4: 设置NAT路由。在转发来自192.203.80.144的数据包之前,先进行网络地址转换,把这个地址转换为193.233.7.83       # ip route add nat 192.203.80.142 via 193.233.7.83       示例5: 实现数据包级负载平衡,允许把数据包随机从多个路由发出。weight 可以设置权重.       # ip route replace default equalize nexthop via 211.139.218.145 dev eth0 weight 1 nexthop via 211.139.218.145 dev eth1 weight 1       7.4.ip route delete-- 删除路由       缩写:delete、del、d       示例1:删除上一节命令加入的多路径路由       # ip route del default scope global nexthop dev ppp0 nexthop dev ppp1       7.5.ip route show -- 列出路由       缩写:show、list、sh、ls、l       示例1: 计算使用gated/bgp协议的路由个数       # ip route ls proto gated/bgp |wc       1413 9891 79010       示例2: 计算路由缓存里面的条数,由于被缓存路由的属性可能大于一行,以此需要使用-o选项       # ip -o route ls cloned |wc       159 2543 18707       示例3: 列出路由表TABLEID里面的路由。缺省设置是table main。TABLEID或者是一个真正的路由表ID或者是/etc/iproute2/rt_tables文件定义的字符串,       或者是以下的特殊值:       all -- 列出所有表的路由;       cache -- 列出路由缓存的内容。       ip ro ls 193.233.7.82 tab cache       示例4: 列出某个路由表的内容       # ip route ls table fddi153       示例5: 列出默认路由表的内容       # ip route ls       这个命令等于传统的: route       7.6.ip route flush -- 擦除路由表       示例1: 删除路由表main中的所有网关路由(示例:在路由监控程序挂掉之后):       # ip -4 ro flush scope global type unicast       示例2:清除所有被克隆出来的IPv6路由:       # ip -6 -s -s ro flush cache       示例3: 在gated程序挂掉之后,清除所有的BGP路由:       # ip -s ro f proto gated/bgp       示例4: 清除所有ipv4路由cache       # ip route flush cache       *** IPv4 routing cache is flushed.       7.7 ip route get -- 获得单个路由 .缩写:get、g       使用这个命令可以获得到达目的地址的一个路由以及它的确切内容。       ip route get命令和ip route show命令执行的操作是不同的。ip route show命令只是显示现有的路由,而ip route get命令在必要时会派生出新的路由。       示例1: 搜索到193.233.7.82的路由       # ip route get 193.233.7.82       193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac cache mtu 1500 rtt 300       示例2: 搜索目的地址是193.233.7.82,来自193.233.7.82,从eth0设备到达的路由(这条命令会产生一条非常有意思的路由,这是一条到193.233.7.82的回环路由)       # ip r g 193.233.7.82 from 193.233.7.82 iif eth0       193.233.7.82 from 193.233.7.82 dev eth0 src 193.233.7.65 realms inr.ac/inr.ac       cache       mtu 1500 rtt 300 iif eth0       8. ip route -- 路由策略数据库管理命令       命令       add、delete、show(或者list)       注意:策略路由(policy routing)不等于路由策略(rouing policy)。       在某些情况下,我们不只是需要通过数据包的目的地址决定路由,可能还需要通过其他一些域:源地址、IP协议、传输层端口甚至数据包的负载。       这就叫做:策略路由(policy routing)。       8.1. ip rule add -- 插入新的规则       ip rule delete -- 删除规则       缩写:add、a;delete、del、d       示例1: 通过路由表inr.ruhep路由来自源地址为192.203.80/24的数据包       ip ru add from 192.203.80/24 table inr.ruhep prio 220       示例2:把源地址为193.233.7.83的数据报的源地址转换为192.203.80.144,并通过表1进行路由       ip ru add from 193.233.7.83 nat 192.203.80.144 table 1 prio 320       示例3:删除无用的缺省规则       ip ru del prio 32767       8.2. ip rule show -- 列出路由规则       缩写:show、list、sh、ls、l       示例1: # ip ru ls       0: from all lookup local       32762: from 192.168.4.89 lookup fddi153       32764: from 192.168.4.88 lookup fddi153       32766: from all lookup main       32767: from all lookup 253       9. ip maddress -- 多播地址管理       缩写:show、list、sh、ls、l       9.1.ip maddress show -- 列出多播地址       示例1: # ip maddr ls dummy       9.2. ip maddress add -- 加入多播地址       ip maddress delete -- 删除多播地址       缩写:add、a;delete、del、d       使用这两个命令,我们可以添加/删除在网络接口上监听的链路层多播地址。这个命令只能管理链路层地址。       示例1: 增加 # ip maddr add 33:33:00:00:00:01 dev dummy       示例2: 查看 # ip -O maddr ls dummy       2: dummy       link 33:33:00:00:00:01 users 2 static       link 01:00:5e:00:00:01       示例3: 删除 # ip maddr del 33:33:00:00:00:01 dev dummy       10.ip mroute -- 多播路由缓存管理       10.1. ip mroute show -- 列出多播路由缓存条目       缩写:show、list、sh、ls、l       示例1:查看 # ip mroute ls       (193.232.127.6, 224.0.1.39) Iif: unresolved       (193.232.244.34, 224.0.1.40) Iif: unresolved       (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg       示例2:查看 # ip -s mr ls 224.66/16       (193.233.7.65, 224.66.66.66) Iif: eth0 Oifs: pimreg       9383 packets, 300256 bytes       11. ip tunnel -- 通道配置       缩写       tunnel、tunl       11.1.ip tunnel add -- 添加新的通道       ip tunnel change -- 修改现有的通道       ip tunnel delete -- 删除一个通道       缩写:add、a;change、chg;delete、del、d       示例1:建立一个点对点通道,最大TTL是32       # ip tunnel add Cisco mode sit remote 192.31.7.104 local 192.203.80.1 ttl 32       11.2.ip tunnel show -- 列出现有的通道       缩写:show、list、sh、ls、l       示例1: # ip -s tunl ls Cisco       12. ip monitor和rtmon -- 状态监视       ip命令可以用于连续地监视设备、地址和路由的状态。这个命令选项的格式有点不同,命令选项的名字叫做monitor,接着是操作对象:       ip monitor [ file FILE ] [ all | OBJECT-LIST ]       示例1: # rtmon file /var/log/rtmon.log       示例2: # ip monitor file /var/log/rtmon.log r 


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有