路由器配置

        在日常的使用中,或是在服务器中,有两个网卡配置两个地址,访问不同的网络段,是很常见的。但是,我们需要额外的添加路由表来决定发送的数据包经过正确的网关进行通信。所以让我们一块来看看如何在linux操作系统中添加路由。

       一、首先要知道路由的概念:

       路由:把一个数据包从一个设备发送到不同网络里的另一个设备上去。这些工作靠路由器来完成。路由器只关心网络的状态和决定网络中的最佳路径。

路由的实现依靠路由器中的路由表来完成。

实验:pc1,pc2,r1,r2,r3,r4,r5  配置5个网络,让两个pc机,穿越5个路由可以实现互通

       下面是我在做此实验时遇到的问题

1.在建路由器之前,需要先编辑虚拟网络编辑器,在右下角出现更改设置,以管理员身份打开。会出现一些隐藏的模式。在添加VMware的时候要选择仅主机模式,同时取消dhcp前面的√。

2.在用虚拟机进行链接克隆路由器的过程中,开机后出现一直开不了机状况是因为给链接克隆分配的内存不够用,关机,扩大内存就可以啦!

3.链接克隆过程中出现找不到文件问题,或者虚拟机本身有问题,可以恢复快照。

       二、在做实验之前要知道怎么修改网卡的配置文件,以及怎么修改网卡名。

centos6网卡名修改

udev  所有硬件的命名,可写入脚本,对硬件进行操作。

1.加载网卡驱动,该网卡名

[root@centos6 ~]# vim /etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:77:8e:d3", ATTR{type}=="1", KERNEL=="eth*", NAME="ethmage"

 2.查看网卡驱动名

[root@centos6 ~]# ethtool -i eth0 |head -1
 driver: e1000

 3.修改网卡配置文件

[root@centos6 ~]# cd /etc/sysconfig/network-scripts/
[root@centos6 network-scripts]# mv ifcfg-eth1 ifcfg-ethmage (非必须)
[root@centos6 network-scripts]# vim ifcfg-ethmage
DEVICE=ethmage
NAME="System ethmage" 非必须

 4.卸载驱动并加载驱动以重读udev设置

root@centos6 ~]# modprobe -r eth1; modprobe eth1

 注意:不能分步执行!!!
网卡的配置文件是在/etc/sysconfig/network-scripts目录下
DEVICE=eth0                  设备名
ONBOOT=yes                 表示随着网络服务的启动,该网卡是否随之启动
NM_CONTROLLED=yes 表示该网卡是否支持NetworkManager管理
BOOTPROTO=dhcp|static|none  如删除该行,则为手动设置地址
HWADDR= 该网卡的MAC地址

PEERDNS=no 默认是yes 当该网卡启动时,而PEERDNS=yes,则dns配置文件中的dns会被该网卡的dns设置所覆盖,若网卡是dhcp则被dhcp的设置覆盖,若网卡是静态地址,则被静态配置的dns覆盖

IPADDR=6.6.6.6
NETMASK=255.255.255.0
GATEWAY=6.6.6.1
USERCTRL=yes 该网卡的开关是否被普通用户所调整

显示路由表

[root@pc1 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 1.1.1.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

 为pc1添加默认路由

[root@pc1 ~]#route add default gw 1.1.1.249
 [root@pc1 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 1.1.1.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0
 0.0.0.0         1.1.1.249       0.0.0.0         UG    0      0        0 eth2

两个主机之间的通信

[root@pc1 ~]#ping 6.6.6.6
 PING 6.6.6.6 (6.6.6.6) 56(84) bytes of data.
 64 bytes from 6.6.6.6: icmp_seq=1 ttl=59 time=7.11 ms
 64 bytes from 6.6.6.6: icmp_seq=2 ttl=59 time=3.03 ms
 64 bytes from 6.6.6.6: icmp_seq=3 ttl=59 time=2.65 ms
 64 bytes from 6.6.6.6: icmp_seq=4 ttl=59 time=1.99 ms
 64 bytes from 6.6.6.6: icmp_seq=5 ttl=59 time=1.54 ms
 64 bytes from 6.6.6.6: icmp_seq=6 ttl=59 time=5.27 ms
 64 bytes from 6.6.6.6: icmp_seq=7 ttl=59 time=2.29 ms

 查看pc2的路由表

[root@pc2 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 6.6.6.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

 为pc2添加默认路由

[root@pc2 ~]#route add default gw 6.6.6.253
 [root@pc2 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 6.6.6.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0
 0.0.0.0         6.6.6.253       0.0.0.0         UG    0      0        0 eth2

[root@pc2 ~]#ping 1.1.1.1
 PING 1.1.1.1 (1.1.1.1) 56(84) bytes of data.
 64 bytes from 1.1.1.1: icmp_seq=1 ttl=59 time=7.79 ms
 64 bytes from 1.1.1.1: icmp_seq=2 ttl=59 time=1.68 ms
 64 bytes from 1.1.1.1: icmp_seq=3 ttl=59 time=2.37 ms
 ^C
 --- 1.1.1.1 ping statistics ---
 3 packets transmitted, 3 received, 0% packet loss, time 2249ms
 rtt min/avg/max/mdev = 1.682/3.950/7.790/2.730 ms

添加路由器1到各个网络的路由

[root@route1 ~]#route add -net 3.3.3.0/24 gw 2.2.2.250
[root@route1 ~]#route add -net 4.4.4.0/24 gw 2.2.2.250
[root@route1 ~]#route add -net 5.5.5.0/24 gw 2.2.2.250
[root@route1 ~]#route add -net 6.6.6.0/24 gw 2.2.2.250
[root@route1 network-scripts]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 5.5.5.0         2.2.2.250       255.255.255.0   UG    0      0        0 eth3
 2.2.2.0         0.0.0.0         255.255.255.0   U     1      0        0 eth3
 6.6.6.0         2.2.2.250       255.255.255.0   UG    0      0        0 eth3
 1.1.1.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 4.4.4.0         2.2.2.250       255.255.255.0   UG    0      0        0 eth3
 3.3.3.0         2.2.2.250       255.255.255.0   UG    0      0        0 eth3
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

 设置包转发

开启 Linux 的路由功能可以通过调整内核的网络参数来实现。要配置和调整内核参数可以使用 sysctl 命令。例如:要开启 Linux 内核的数据包转发功能可以使用如下的命令。

# sysctl -w net.ipv4.ip_forward=1

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

[root@route1 ~]# vim /etc/sysctl.conf 
 net.ipv4.ip_forward = 1

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

[root@route1 ~]# sysctl –p

关闭防火墙
[root@route1 ~]# iptables –F

添加路由器2到各个网络的路由

[root@route2 ~]#route add -net 1.1.1.0/24 gw 2.2.2.249
 [root@route2 ~]#route add -net 4.4.4.0/24 gw 3.3.3.251
 [root@route2 ~]#route add -net 5.5.5.0/24 gw 3.3.3.251
 [root@route2 ~]#route add -net 6.6.6.0/24 gw 3.3.3.251

[root@route2 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 5.5.5.0         3.3.3.251       255.255.255.0   UG    0      0        0 eth3
 2.2.2.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 6.6.6.0         3.3.3.251       255.255.255.0   UG    0      0        0 eth3
 1.1.1.0         2.2.2.249       255.255.255.0   UG    0      0        0 eth2
 4.4.4.0         3.3.3.251       255.255.255.0   UG    0      0        0 eth3
 3.3.3.0         0.0.0.0         255.255.255.0   U     1      0        0 eth3
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

[root@route2 ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1

[root@route2 ~]# sysctl -p
[root@route2 ~]# iptables –F

 添加路由器3到各个网络的路由

[root@route3 ~]#route add -net 1.1.1.0/24 gw 3.3.3.250
 [root@route3 ~]#route add -net 2.2.2.0/24 gw 3.3.3.250
 [root@route3 ~]#route add -net 5.5.5.0/24 gw 4.4.4.252
 [root@route3 ~]#route add -net 6.6.6.0/24 gw 4.4.4.252

[root@route3 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 5.5.5.0         4.4.4.252       255.255.255.0   UG    0      0        0 eth3
 2.2.2.0         3.3.3.250       255.255.255.0   UG    0      0        0 eth2
 6.6.6.0         4.4.4.252       255.255.255.0   UG    0      0        0 eth3
 1.1.1.0         3.3.3.250       255.255.255.0   UG    0      0        0 eth2
 4.4.4.0         0.0.0.0         255.255.255.0   U     1      0        0 eth3
 3.3.3.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

[root@route3 ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1

[root@route3 ~]# sysctl -p
[root@route3 ~]# iptables –F

 添加路由器4到各个网络的路由

[root@route4 network-scripts]#route add -net 1.1.1.0/24 gw 4.4.4.251
 [root@route4network-scripts]#route add -net 2.2.2.0/24 gw 4.4.4.251
 [root@route4  network-scripts]#route add -net 3.3.3.0/24 gw 4.4.4.251
 [root@route4  network-scripts]#route add -net 6.6.6.0/24 gw 5.5.5.253

[root@ route4 network-scripts]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 2.2.2.0         4.4.4.251       255.255.255.0   UG    0      0        0 eth2
 5.5.5.0         0.0.0.0         255.255.255.0   U     1      0        0 eth3
 6.6.6.0         5.5.5.253       255.255.255.0   UG    0      0        0 eth3
1.1.1.0         4.4.4.251       255.255.255.0   UG    0      0        0 eth2
 3.3.3.0         4.4.4.251       255.255.255.0   UG    0      0        0 eth2
 4.4.4.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0

[root@route4 ~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1

[root@route4 ~]# sysctl -p
[root@route4 ~]# iptables –F

添加路由器5到各个网络的路由

[root@route5 ~]#route add -net 1.1.1.0/24 gw 5.5.5.252
 [root@route5 ~]#route add -net 2.2.2.0/24 gw 5.5.5.252
 [root@route5 ~]#route add -net 3.3.3.0/24 gw 5.5.5.252
 [root@route5 ~]#route add -net 4.4.4.0/24 gw 5.5.5.252

[root@route5 ~]#route -n
 Kernel IP routing table
 Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
 2.2.2.0         5.5.5.252       255.255.255.0   UG    0      0        0 eth2
 5.5.5.0         0.0.0.0         255.255.255.0   U     1      0        0 eth2
 1.1.1.0         5.5.5.252       255.255.255.0   UG    0      0        0 eth2
 6.6.6.0         0.0.0.0         255.255.255.0   U     1      0        0 eth3
 4.4.4.0         5.5.5.252       255.255.255.0   UG    0      0        0 eth2
 3.3.3.0         5.5.5.252       255.255.255.0   UG    0      0        0 eth2
 192.168.40.0    0.0.0.0         255.255.255.0   U     1      0        0 eth1
 172.18.0.0      0.0.0.0         255.255.0.0     U     1      0        0 eth0
 0.0.0.0         172.18.0.1      0.0.0.0         UG    0      0        0 eth0

[root@route5~]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1

[root@route5 ~]# sysctl -p
[root@route5 ~]# iptables -F

注意:遇到这种状况是防火墙没开,检查一下路由器的防火墙。

[root@pc1 ~]#ping 6.6.6.6
 PING 6.6.6.6 (6.6.6.6) 56(84) bytes of data.
 From 4.4.4.252 icmp_seq=1 Destination Host Prohibited

 

 


转载于:https://www.cnblogs.com/f-h-j-11-7/p/9498177.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值