網(wǎng)關(guān)服務(wù)器和網(wǎng)站服務(wù)器都采用centos 7操作系統(tǒng);
網(wǎng)關(guān)服務(wù)器安裝3塊千兆網(wǎng)卡,分別連接在Internet、企業(yè)內(nèi)網(wǎng)、網(wǎng)站服務(wù)器。
要求如下:
網(wǎng)關(guān)服務(wù)器連接互聯(lián)網(wǎng)卡ens33配置為公網(wǎng)IP地址,分配到firewall的external區(qū)域;連接內(nèi)網(wǎng)網(wǎng)卡ens37地址為192.168.1.1,分配到firewall的trusted區(qū)域;連接服務(wù)器網(wǎng)卡ens38地址為192.168.2.1,分配到firewall的DMZ區(qū)域。
網(wǎng)站服務(wù)器和網(wǎng)關(guān)服務(wù)器都通過(guò)SSH來(lái)遠(yuǎn)程管理,為了安全,將SSH默認(rèn)端口改為12345。
網(wǎng)站服務(wù)器開(kāi)啟https,過(guò)濾未加密的http流量。
網(wǎng)站服務(wù)器拒絕ping,網(wǎng)關(guān)服務(wù)器拒絕來(lái)自互聯(lián)網(wǎng)上的ping。
內(nèi)網(wǎng)用戶需要通過(guò)網(wǎng)關(guān)服務(wù)器共享上網(wǎng)。
互聯(lián)網(wǎng)用戶需要訪問(wèn)網(wǎng)站服務(wù)器。
基本的環(huán)境配置:
網(wǎng)關(guān)服務(wù)器配置 網(wǎng)卡 :
成都創(chuàng)新互聯(lián)公司主營(yíng)洞頭網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),洞頭h5微信小程序搭建,洞頭網(wǎng)站營(yíng)銷推廣歡迎洞頭等地區(qū)企業(yè)咨詢
[root@localhost network-scripts]# ip a = ip addr
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fc:60:4b brd ff:ff:ff:ff:ff:ff
inet 100.0.0.1/8 brd 100.255.255.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::5f65:9c3d:d218:7cea/64 scope link
valid_lft forever preferred_lft forever
3: ens36: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fc:60:55 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 192.168.1.255 scope global ens36
valid_lft forever preferred_lft forever
inet6 fe80::7456:2bbc:dc20:31bd/64 scope link
valid_lft forever preferred_lft forever
4: ens37: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fc:60:5f brd ff:ff:ff:ff:ff:ff
inet 192.168.2.1/24 brd 192.168.2.255 scope global ens37
valid_lft forever preferred_lft forever
inet6 fe80::1e90:6601:56c2:c9ba/64 scope link
valid_lft forever preferred_lft forever
啟動(dòng)網(wǎng)關(guān)服務(wù)器上的路由轉(zhuǎn)發(fā)功能:
[root@localhost /]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@localhost /]# sysctl -p
net.ipv4.ip_forward = 1
配置 web 服務(wù)器的網(wǎng)卡:
[root@localhost /]# ip a
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2f:24:4b brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/24 brd 192.168.2.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::19de:d9a7:568a:f34f/64 scope link
valid_lft forever preferred_lft forever
[root@localhost /]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.2.1 0.0.0.0 UG 100 0 0 ens33
Internet 測(cè)試機(jī)網(wǎng)卡配置:
[root@localhost /]# ip a
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:45:66:64 brd ff:ff:ff:ff:ff:ff
inet 100.0.0.10/8 brd 100.255.255.255 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::3ebc:8f75:cd28:e516/64 scope link tentative
valid_lft forever preferred_lft forever
web 服務(wù)器環(huán)境搭建:
[root@localhost ~]# systemctl start firewalld #啟動(dòng)防火墻
[root@localhost ~]# yum -y install httpd mod_ssl #安裝httpd和mod_ssl
[root@localhost ~]# systemctl start httpd #啟動(dòng)httpd服務(wù)
[root@localhost ~]# systemctl enable httpd #設(shè)置為開(kāi)機(jī)自啟
[root@localhost ~]# vim /var/www/html/index.html #新建網(wǎng)站測(cè)試首頁(yè)文件
test.com
[root@localhost ~]# vim /etc/ssh/sshd_config #更改SSH的偵聽(tīng)端口(需關(guān)閉SELinux):
................
Port 12345
................
[root@localhost ~]# systemctl restart sshd
在網(wǎng)站服務(wù)器上配置firewalld防火墻:
1、設(shè)置默認(rèn)區(qū)域?yàn)閐mz區(qū)域:
[root@localhost ~]# firewall-cmd --set-default-zone=dmz
success
2、為dmz區(qū)域打開(kāi)https服務(wù)并添加tcp的12345端口:
[root@localhost ~]# firewall-cmd --zone=dmz --add-service=https --permanent
success
[root@localhost ~]# firewall-cmd --zone=dmz --add-port=12345/tcp --permanent
success
3、禁止ping:
[root@localhost ~]# firewall-cmd --add-icmp-block=echo-request --zone=dmz --permanent
success
4、因?yàn)橐呀?jīng)更改了預(yù)定義SSH服務(wù)的默認(rèn)端口,所以將預(yù)定義SSH服務(wù)移除:
[root@localhost ~]# firewall-cmd --zone=dmz --remove-service=ssh --permanent
success
5、重新加載firewalld配置,并查看之前的配置:
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
dmz (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: https
ports: 12345/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks: echo-request
rich rules:
網(wǎng)關(guān)服務(wù)器上配置firewalld防火墻:
1、將網(wǎng)卡添加至指定區(qū)域:
[root@localhost /]# systemctl start firewalld #啟用防火墻
[root@localhost /]# firewall-cmd --set-default-zone=external #設(shè)置默認(rèn)區(qū)域?yàn)閑xternal
success
[root@localhost /]# firewall-cmd --change-interface=ens37 --zone=trusted #將ens37添加至trusted區(qū)域
The interface is under control of NetworkManager, setting zone to 'trusted'.
success
[root@localhost /]# firewall-cmd --change-interface=ens38 --zone=dmz #將ens38添加至dmz區(qū)域
The interface is under control of NetworkManager, setting zone to 'dmz'.
success
2、查看配置,并保存為永久
[root@localhost /]# firewall-cmd --get-active-zones
dmz
interfaces: ens37
external
interfaces: ens33
trusted
interfaces: ens36
[root@localhost /]# firewall-cmd --runtime-to-permanent # 將當(dāng)前的配置保存到文件中
success
3、在企業(yè)內(nèi)部主機(jī)上測(cè)試:
4、更改SSH的偵聽(tīng)端口,并重啟服務(wù)(需關(guān)閉SELinux):
[root@localhost ~]# vim /etc/ssh/sshd_config
................
Port 12345
................
[root@localhost ~]# systemctl restart sshd
5、配置external區(qū)域添加tcp的12345端口:
[root@localhost /]# firewall-cmd --zone=external --add-port=12345/tcp --permanent
success
6、external區(qū)域移除SSH服務(wù):
[root@localhost /]# firewall-cmd --zone=external --remove-service=ssh --permanent
success
7、配置external區(qū)域禁止ping:
[root@localhost /]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent
success
8、重新加載防火墻配置:
[root@localhost /]# firewall-cmd --reload
success
測(cè)試ssh連接:
在 Internet 測(cè)試機(jī)通過(guò)SSH連接網(wǎng)關(guān)服務(wù)器的外部接口地址的12345端口:
[root@localhost /]# ssh -p 12345 100.0.0.1
The authenticity of host '[100.0.0.1]:12345 ([100.0.0.1]:12345)' can't be established.
ECDSA key fingerprint is 68:df:0f:ac:c7:75:df:02:88:7d:36:6a:1a:ae:27:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[100.0.0.1]:12345' (ECDSA) to the list of known hosts.
root@100.0.0.1's password:
Last login: Sun Sep 1 16:36:33 2019
[root@localhost ~]#
使用內(nèi)網(wǎng)測(cè)試機(jī)SSH登錄web網(wǎng)站服務(wù)器的12345端口:
[root@localhost /]# ssh -p 12345 192.168.2.10
The authenticity of host '[192.168.2.10]:12345 ([192.168.2.10]:12345)' can't be established.
ECDSA key fingerprint is 68:df:0f:ac:c7:75:df:02:88:7d:36:6a:1a:ae:27:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.2.10]:12345' (ECDSA) to the list of known hosts.
root@192.168.2.10's password:
Last login: Sun Sep 1 16:36:39 2019
[root@localhost ~]#
實(shí)現(xiàn) IP 偽裝與端口轉(zhuǎn)發(fā):
1、在Internet測(cè)試機(jī)上搭建web服務(wù),用來(lái)測(cè)試:
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /var/www/html/index.html
www.baidu.com
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# systemctl start httpd
2、在內(nèi)部測(cè)試機(jī)和dmz的網(wǎng)站服務(wù)區(qū)都可以訪問(wèn)外網(wǎng)的網(wǎng)站(若訪問(wèn)不了,則可能是公網(wǎng)測(cè)試機(jī)的防火墻配置問(wèn)題,可先將公網(wǎng)測(cè)試機(jī)的防火墻關(guān)閉,或放行相關(guān)服務(wù)的流量即可):
3、查看網(wǎng)關(guān)服務(wù)器的external區(qū)域是否開(kāi)啟了地址偽裝:
[root@localhost /]# firewall-cmd --list-all --zone=external
external (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services:
ports: 12345/tcp
protocols:
masquerade: yes # 表示地址偽裝已啟用
forward-ports:
sourceports:
icmp-blocks: echo-request
rich rules:
4、只為源地址192.168.1.0/24網(wǎng)段的地址開(kāi)啟地址IP偽裝。
在網(wǎng)關(guān)服務(wù)器上關(guān)閉external默認(rèn)的地址偽裝,添加富規(guī)則,要求external區(qū)域內(nèi),源地址為192.168.1.0/24網(wǎng)段的地址開(kāi)啟地址IP偽裝:
[root@localhost ~]# firewall-cmd --remove-masquerade --zone=external
success
[root@localhost ~]# firewall-cmd --zone=external --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 masquerade'
success
在dmz區(qū)域上測(cè)試訪問(wèn)發(fā)現(xiàn)無(wú)法訪問(wèn),但內(nèi)網(wǎng)主機(jī)卻可以:
[root@localhost /]# curl http://100.0.0.10
curl: (7) Failed connect to 100.0.0.10:80; No route to host
5、配置端口轉(zhuǎn)發(fā)實(shí)現(xiàn)互聯(lián)網(wǎng)用戶可以訪問(wèn)內(nèi)部web服務(wù)器:
在網(wǎng)關(guān)服務(wù)器上配置:
[root@localhost /]# firewall-cmd --zone=external --add-forward-port=port=443:proto=tcp:toaddr=192.168.2.10
success
在Internet測(cè)試機(jī)上訪問(wèn)內(nèi)網(wǎng)的web服務(wù)器成功:
六、使用富規(guī)則實(shí)現(xiàn)端口轉(zhuǎn)發(fā):
上述配置完成后,若現(xiàn)在公司申請(qǐng)了一個(gè)新的公網(wǎng)ip地址100.0.0.254,那么就需要重新做端口轉(zhuǎn)發(fā)了:
1、將新申請(qǐng)的公網(wǎng)地址100.0.0.254配置在網(wǎng)關(guān)服務(wù)器的外網(wǎng)接口ens33上,作為第二個(gè)IP地址:
[root@localhost /]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR0=100.0.0.1 # 改到下四行
PREFIX0=24
IPADDR1=100.0.0.254
PREFIX1=24 # 添加成兩個(gè)IP
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
ZONE=external
[root@localhost /]# ifdown ens33;ifup ens33 # 重啟網(wǎng)卡使配置生效
[root@localhost /]# ip a # 查看配置是否成功
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fc:60:4b brd ff:ff:ff:ff:ff:ff
inet 100.0.0.1/24 brd 100.0.0.255 scope global ens33
valid_lft forever preferred_lft forever
inet 100.0.0.254/24 brd 100.0.0.255 scope global secondary ens33
valid_lft forever preferred_lft forever
inet6 fe80::5f65:9c3d:d218:7cea/64 scope link
valid_lft forever preferred_lft forever
2、使用富規(guī)則配置端口轉(zhuǎn)發(fā):
[root@localhost /]# firewall-cmd --zone=external --add-rich-rule='rule family=ipv4 destination address=100.0.0.254/24 forward-port port=443 protocol=tcp to-addr=192.168.2.10'
success
3、驗(yàn)證: