Linux—firewalld高级配置
一.实验环境
二.需求描述
1、 网关服务器连接互联网网卡ens33地址为100.1.1.10,为公网IP地址,分配到firewall的external区域;连接内网网卡ens34地址为192.168.1.1,分配到firewall的trusted区域;连接服务器网卡ens35地址为192.168.2.1,分配到firewall的dmz区域
2、 网站服务器和网关服务器均通过SSH来远程管理,为了安全,将SSH默认端口改为12345
3、 网站服务器开启 HTTPS,过滤未加密的HTTP流量
4、 网站务器拒绝ping,网关服务器拒绝来自互联网上的ping
5、 公司内网用户需要通过网关服务器共享上网
6、 互联网用户需要访问网站服务器
三.具体实验步骤
虚拟机试验环境搭建:
1.打开虚拟机网络编辑器
2.添加对应的网段
环境配置:
###在网关服务器##开启路由转发功能
vi /etc/sysctl.conf //永久生效
net.ipv4.ip_forward = 1
sysctl -p //生效
###在网站服务器###
1、 检查路由
[root@FW3 ~]# 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
192.168.2.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
2、检查防火墙必须开启
root@FW3 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2019-02-15 18:38:46 CST; 8min ago
3、 搭建yum
[root@FW3 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@FW3 ~]# cd /etc/yum.repos.d/
[root@FW3 yum.repos.d]# ll
总用量 32
-rw-r--r--. 1 root root 1664 4月 29 2018 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 4月 29 2018 CentOS-CR.repo
-rw-r--r--. 1 root root 649 4月 29 2018 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root 314 4月 29 2018 CentOS-fasttrack.repo
-rw-r--r--. 1 root root 630 4月 29 2018 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 4月 29 2018 CentOS-Sources.repo
-rw-r--r--. 1 root root 4768 4月 29 2018 CentOS-Vault.repo
[root@FW3 yum.repos.d]# mkdir back
[root@FW3 yum.repos.d]# mv C* back/
[root@FW3 yum.repos.d]# cp back/CentOS-Base.repo local.repo
[root@FW3 yum.repos.d]# vi local.repo
[centos]
name=centos
baseurl=file:///mnt
gpgcheck=0
enabled=1
\#gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@FW3 yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: centos
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@FW3 yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
centos | 3.6 kB 00:00
(1/4): centos/group_gz | 166 kB 00:00
(2/4): centos/filelists_db | 3.1 MB 00:00
(3/4): centos/primary_db | 3.1 MB 00:00
(4/4): centos/other_db | 1.3 MB 00:00
元数据缓存已建立
[root@FW3 yum.repos.d]# yum -y install httpd mod_ssl ###安装HTTP 安装https模块
[root@FW3 yum.repos.d]# systemctl start httpd
[root@FW3 yum.repos.d]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@FW3 yum.repos.d]# vi /var/www/html/index.html
<h1>wwww.51xit.top<h1>
###测试正常###
###更改ssh端口####
[root@FW3 yum.repos.d]# setenforce 0
[root@FW3 yum.repos.d]# vi /etc/sysconfig/selinux
SELINUX=disabled
[root@FW3 yum.repos.d]# vi /etc/ssh/sshd_config
port 12345
####在网站服务器上配置防火墙###
1、 设置默认区域为DMZ
[root@FW3 yum.repos.d]# firewall-cmd --set-default-zone=dmz
2、为DMZ区域打开https服务添加TCP为12345端口
[root@FW3 yum.repos.d]# firewall-cmd --zone=dmz --add-service=https --permanent
[root@FW3 yum.repos.d]# firewall-cmd --zone=dmz --add-port=12345/tcp --permanent
3、配置dmz区域移除ssh服务
[root@FW3 ~]# firewall-cmd --zone=dmz --remove-service=ssh --permanent
4、禁止ping
[root@FW3 yum.repos.d]# firewall-cmd --add-icmp-block=echo-request --zone=dmz --permanent
5、重新加载防火墙
[root@FW3 yum.repos.d]# firewall-cmd --reload
####网关服务器配置防火墙###
1、 检查下防火墙
[root@FW1 ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 五 2019-02-15 18:18:55 CST; 44min ago
2、设置默认区域external区域
[root@FW1 ~]# firewall-cmd --set-default-zone=external
success
[root@FW1 ~]# firewall-cmd --list-all
external (active)
target: default
icmp-block-inversion: no
interfaces: ens33 ens34 ens35
sources:
services: ssh
ports:
protocols:
masquerade: yes
forward-ports:
source-ports:
icmp-blocks:
rich rules:
3、设置ens34网卡为trusted区域。将ens35网卡设置为DMZ区域
[root@FW1 ~]# firewall-cmd --change-interface=ens34 --zone=trusted
[root@FW1 ~]# firewall-cmd --change-interface=ens35 --zone=dmz
4、查看配置详情
[root@FW1 ~]# firewall-cmd --get-active-zones
dmz
interfaces: ens35
external
interfaces: ens33
trusted
interfaces: ens34
5、在企业内网测试机
打开浏览器输入https://192.168.2.10
6、更改ssh端口号
[root@FW1 ~]# setenforce 0
[root@FW1 ~]# vi /etc/sysconfig/selinux
SELINUX=disabled
[root@FW1 ~]# vi /etc/ssh/sshd_config
port 12345
7、配置external区域添加TCP12345端口
[root@FW1 ~]# firewall-cmd --zone=external --add-port=12345/tcp --permanent
8、配置external区域移除ssh服务
[root@FW1 ~]# firewall-cmd --zone=external --remove-service=ssh --permanent
9、配置external禁止ping
[root@FW1 ~]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent
10、重新加载防火墙
[root@FW1 ~]# firewall-cmd --reload
11、在测试互联网测试机上ssh登录网关服务器的外部接口地址的12345端口
ssh -p 12345 100.1.1.10
12、在企业内部测试机上ssh登录网站服务器的12345端口
Ssh -p 12345 192.168.2.10
###内网用户通过网关服务器能访问外网测试机的http服务器###
1、 在外网服务器上搭建HTTP服务
[root@FW4 ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 写保护,将以只读方式挂载
[root@FW4 ~]# cd /etc/yum.repos.d/
[root@FW4 yum.repos.d]# mkdir back
[root@FW4 yum.repos.d]# mv C* back/
[root@FW4 yum.repos.d]# cp back/CentOS-Base.repo local.repo
[root@FW4 yum.repos.d]# vi local.repo
[root@FW4 yum.repos.d]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: centos
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@FW4 yum.repos.d]# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
centos | 3.6 kB 00:00
(1/4): centos/group_gz | 166 kB 00:00
(2/4): centos/filelists_db | 3.1 MB 00:00
(3/4): centos/primary_db | 3.1 MB 00:00
(4/4): centos/other_db | 1.3 MB 00:00
元数据缓存已建立
[root@FW4 yum.repos.d]# yum -y install httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-80.el7.centos 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-80.el7.centos,它被软件包 httpd-2.4.6-80.el7.centos.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-80.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 httpd-tools.x86_64.0.2.4.6-80.el7.centos 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
================================================================================
Package 架构 版本 源 大小
================================================================================
正在安装:
httpd x86_64 2.4.6-80.el7.centos centos 2.7 M
为依赖而安装:
httpd-tools x86_64 2.4.6-80.el7.centos centos 89 k
mailcap noarch 2.1.41-2.el7 centos 31 k
事务概要
================================================================================
安装 1 软件包 (+2 依赖软件包)
总下载量:2.8 M
安装大小:9.6 M
Downloading packages:
\--------------------------------------------------------------------------------
总计 2.9 MB/s | 2.8 MB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : mailcap-2.1.41-2.el7.noarch 1/3
正在安装 : httpd-tools-2.4.6-80.el7.centos.x86_64 2/3
正在安装 : httpd-2.4.6-80.el7.centos.x86_64 3/3
验证中 : httpd-tools-2.4.6-80.el7.centos.x86_64 1/3
验证中 : mailcap-2.1.41-2.el7.noarch 2/3
验证中 : httpd-2.4.6-80.el7.centos.x86_64 3/3
已安装:
httpd.x86_64 0:2.4.6-80.el7.centos
作为依赖被安装:
httpd-tools.x86_64 0:2.4.6-80.el7.centos mailcap.noarch 0:2.1.41-2.el7
完毕!
[root@FW4 yum.repos.d]# systemctl start httpd
[root@FW4 yum.repos.d]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
2、外网服务器测试http服务###
3、DMZ网站服务器测试###
4、在网关服务器上查看是否开启伪装
[root@FW1 ~]# 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:
source-ports:
icmp-blocks: echo-request
rich rules:
5、网关服务器关闭伪装
6在命令启动伪装 192.168.1.0/24
[root@FW1 ~]# firewall-cmd --zone=external --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 masquerade'
7、在企业内网站上测试访问internet网站服务器100.1.1.20
不能访问。说明已经伪装生效=
8、在网关服务器上配置端口转发,实现互联网测试服务器能访问企业内部网站服务器
[root@FW1 ~]# firewall-cmd --zone=external --add-forward-port=port=443:proto=tcp:toaddr=192.168.2.10 --permanent
[root@FW1 ~]# firewall-cmd –reload