1. 程式人生 > >路由器配置

路由器配置

卸載 默認路由 table etc config ctrl 最佳路徑 怎麽 font

在日常的使用中,或是在服務器中,有兩個網卡配置兩個地址,訪問不同的網絡段,是很常見的。但是,我們需要額外的添加路由表來決定發送的數據包經過正確的網關進行通信。所以讓我們一塊來看看如何在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


路由器配置