route 命令

使用 Route 命令,可以查看并配置计算机的 IP 路由表。

Route 命令和语法如下所示:

1.  查看本地路由表:route -vn

xxha@PAF4:~$ route -vn
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.0.0     0.0.0.0         255.255.0.0     U     1      0        0 eth1
2. 添加指定路由:

xxha@PAF4:~$ sudo route add 1.1.1.1 gw 192.168.9.1 dev eth1
xxha@PAF4:~$ sudo route -vn
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
1.1.1.1         192.168.9.1     255.255.255.255 UGH   0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.0.0     0.0.0.0         255.255.0.0     U     1      0        0 eth1
这样就添加了网关为192.168.9.1的, 目的地址是1.1.1.1的路径。

如果网关是默认值:0.0.0.0,可以用如下命令添加路由。

xxha@PAF4:~$ sudo route add 2.2.2.2 dev eth1
xxha@PAF4:~$ sudo route -vn
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
1.1.1.1         192.168.9.1     255.255.255.255 UGH   0      0        0 eth1
2.2.2.2         0.0.0.0         255.255.255.255 UH    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.0.0     0.0.0.0         255.255.0.0     U     1      0        0 eth1
3. 删除指定路由: 

xxha@PAF4:~$ sudo route del 1.1.1.1 gw 192.168.9.1 dev eth1
xxha@PAF4:~$ sudo route -vn
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
2.2.2.2         0.0.0.0         255.255.255.255 UH    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.0.0     0.0.0.0         255.255.0.0     U     1      0        0 eth1
也可以:

xxha@PAF4:~$ sudo route del 2.2.2.2 dev eth1
xxha@PAF4:~$ sudo route -vn
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.8.1     0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1000   0        0 eth1
192.168.0.0     0.0.0.0         255.255.0.0     U     1      0        0 eth1
Gateway 是指网络目标地址集可以到达的前进或下一跃点 IP 地址。

route 实现的架构 如下图所示:


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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值