设置Linux服务器为路由器实战

实验中使用了4台Centos虚拟机

[root@D ~]# uname -m
x86_64
[root@D ~]# uname -r
2.6.32-504.el6.x86_64
[root@D ~]# cat /etc/redhat-release 
CentOS release 6.6 (Final)
[root@D ~]# 

  

 
 
规划4台机器,IP规划如下
B机器和C机器均做路由器,数据流向是D--->C----B---->A

 

用途 主机名 eth0 eth1
ssh服务器 A 10.0.1.165
路由器 B 10.0.1.100 192.168.20.100
路由器 C 192.168.20.200 192.168.30.200
客户端 D 192.168.30.150
 
 
 
 
 
 
 

网络结构如下
其中vm0和vm12,vm13均是vmware的虚拟网络
 
 
在vmware中新建2个虚拟网络。用于测试

 

vm12 网络是192.168.20.0/24

 

vm13 网络是192.168.30.0/24

 

 
 

第一部分 IP地址设置

 
A机器设置如下,其中uuid和mac地址可删可不删,因为重启网卡它会自动生成
10.0.1.11是本地的DNS服务器,这里也可以改成一个公网DNS服务器。
[root@rhel ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.1.165
DNS1=10.0.1.11
GATEWAY=10.0.1.1
[root@rhel ~]#

  

 
 
 
B机器的网络设置如下,eth1网卡不设置网关
[root@B ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.1.100
NETMASK=255.255.255.0
GATEWAY=10.0.1.1
[root@B ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.20.100
NETMASK=255.255.255.0
[root@B ~]#

  

 
 
C机器设置如下,两块网卡均没有网关
[root@C ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.20.200
NETMASK=255.255.255.0
[root@C ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.30.200
NETMASK=255.255.255.0
[root@C ~]#

  

 
D机器网卡设置,注意D机器的网关设置为C机器的eth1的地址
[root@D ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.30.150
NETMASK=255.255.255.0
GATEWAY=192.168.30.200
[root@D ~]#

  

 
 

第二部分 内核打开IP转发功能

设置B和C机器修改内核参数,打开路由转发功能,分别在B机器和C机器最后一行添加如下内容
[root@C ~]# tail -1 /etc/sysctl.conf
net.ipv4.ip_forward = 1
然后执行sysctl -p让它生效
[root@C ~]# sysctl -p

  

 
 
 

第三部分 B机器和C机器添加路由

B机器上操作,下面命令表示到192.168.30.0/24的数据包都交给C机器的eth0网卡
因为B机器eth0 通外网,设置了网关,所以最后一行又默认路由
[root@B ~]# route add -net 192.168.30.0/24 gw 192.168.20.200
[root@B ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.30.0    192.168.20.200  255.255.255.0   UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         10.0.1.1        0.0.0.0         UG    0      0        0 eth0
[root@B ~]# 

  

 
C机器上操作
C机器上添加默认网关,把默认数据包发给B机器
[root@C ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
[root@C ~]# route add default gw 192.168.20.100
[root@C ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.20.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.30.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         192.168.20.100  0.0.0.0         UG    0      0        0 eth0
[root@C ~]# 

  

 
 
 
 
 

第四部分 B机器和C机器设置iptables的nat表

 
 
B机器操作
[root@B ~]# iptables -t nat -A POSTROUTING -s 192.168.20.0/24  -j MASQUERADE
[root@B ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  192.168.20.0/24      0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@B ~]# 

  

 
 
C机器操作
[root@C ~]# iptables -t nat -A POSTROUTING -s 192.168.30.0/24  -j MASQUERADE
[root@C ~]# iptables -t nat -L -n
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
MASQUERADE  all  --  192.168.30.0/24      0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@C ~]# 

  

 
 
 
 
 
 
 
 
 
 
 

第五部分 D机器测试结果

 
测试ping和22端口均通,同时也通公网
[root@D ~]# ping 10.0.1.165
PING 10.0.1.165 (10.0.1.165) 56(84) bytes of data.
64 bytes from 10.0.1.165: icmp_seq=1 ttl=62 time=29.0 ms
64 bytes from 10.0.1.165: icmp_seq=2 ttl=62 time=18.3 ms
--- 10.0.1.165 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1695ms
rtt min/avg/max/mdev = 18.353/23.707/29.062/5.356 ms
[root@D ~]# nmap 10.0.1.165 -p22

Starting Nmap 5.51 ( http://nmap.org ) at 2016-12-22 21:03 CST
Nmap scan report for 10.0.1.165
Host is up (0.00072s latency).
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 0.15 seconds
[root@D ~]# 

  

也通外网了(8.8.8.8是谷歌免费的dns服务器地址)
[root@D ~]# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=39 time=70.5 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=39 time=52.3 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=39 time=51.8 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=39 time=51.4 ms
^C
--- 8.8.8.8 ping statistics ---
6 packets transmitted, 4 received, 33% packet loss, time 5378ms
rtt min/avg/max/mdev = 51.487/56.575/70.583/8.095 ms
[root@D ~]# 

  

D机器再把DNS设置了。就可以根据域名访问了
[root@D ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=192.168.30.150
NETMASK=255.255.255.0
GATEWAY=192.168.30.200
DNS1=10.0.1.11
[root@D ~]# /etc/init.d/network restart
Shutting down interface eth0:  Device state: 3 (disconnected)
                                                           [  OK  ]
Shutting down loopback interface:                          [  OK  ]
Bringing up loopback interface:                            [  OK  ]
Bringing up interface eth0:  Active connection state: activated
Active connection path: /org/freedesktop/NetworkManager/ActiveConnection/6
                                                           [  OK  ]
[root@D ~]# ping www.baidu.com
PING www.a.shifen.com (115.239.210.27) 56(84) bytes of data.
64 bytes from 115.239.210.27: icmp_seq=1 ttl=53 time=6.92 ms
64 bytes from 115.239.210.27: icmp_seq=2 ttl=53 time=5.15 ms
64 bytes from 115.239.210.27: icmp_seq=3 ttl=53 time=5.45 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2306ms
rtt min/avg/max/mdev = 5.153/5.845/6.925/0.776 ms
[root@D ~]# 

  

 
 

备注说明:

(1)
iptables -t nat -A POSTROUTING -s 192.168.20.0/24 -j MASQUERADE
设置nat表的时候也可以使用下面这条语句。 上面的优点在于如果
iptables -t nat -A POSTROUTING -s 192.168.20.0/24 -j SNAT --to-source 10.0.1.100
 
SNAT是指在数据包从网卡发送出去的时候,把数据包中的源地址部分替换为指定的IP,这样,接收方就认为数据包的来源是被替换的那个IP的主机。
MASQUERADE是用发送数据的网卡上的IP来替换源IP,因此,对于那些IP不固定的场合,比如拨号网络或者通过dhcp分配IP的情况下,就得用MASQUERADE。
 
(2)
上面网卡配置中涉及的dns都可以改成公网的dns地址,比如8.8.8.8
因为我本地有dns服务器10.0.1.11了。所以就写的本地的地址
 
 
 
 
 
 
 

posted on 2016-12-30 13:18  nmap  阅读(1705)  评论(0)    收藏  举报

导航