Iptables是Linux操作系统常用的防火墙软件,用于服务器的访问控制。Centos7默认的防火墙是Firewalle。个人理解两者都是对规则的配置,Firewalle只是在Iptables基础上进行封装,让操作更简单。内核netfilter执行规则。
本教程使用于 RHEL 7 / CentOS 7,首选查看操作系统的版本:
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost ~]#
本次测试用到的虚拟机地址:192.168.56.5;192.168.56.5;192.168.56.6
1 安装iptables
首先检查是否安装iptables,如果已经安装,或者版本比Centos7低,请略过安装Iptables的步骤,直接看使用和配置。
(1)检查是否安装iptables
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.
[root@localhost ~]#
(2)安装前的准备
#停止firewalle
[root@localhost ~]# systemctl stop firewalld
#禁用firewalld服务
[root@localhost ~]# systemctl mask firewalld
Created symlink from /etc/systemd/system/firewalld.service to /dev/null.
[root@localhost ~]#
1.1 yum安装
如果目标服务器能够联网,可选择yum的方式安装。
#yum安装iptables
[root@localhost ~]# yum install iptables-services
已加载插件:fastestmirror
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
(1/4): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 200 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.0 MB 00:00:02
(4/4): 3/3
……省略中间安装日志……
已安装:
iptables-services.x86_64 0:1.4.21-28.el7
作为依赖被升级:
iptables.x86_64 0:1.4.21-28.el7
完毕!
[root@localhost ~]#
(2)安装后验证
#查看是否安装成功
[root@springboot sysconfig]# rpm -qa |grep iptables
iptables-1.4.21-28.el7.x86_64
iptables-services-1.4.21-28.el7.x86_64
[root@springboot sysconfig]#
1.2 手动安装
如果目标服务器不能联网,请手工下载安装包。下载地址
(1) iptables-1.4.21-28.el7.x86_64下载链接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-1.4.21-28.el7.x86_64.rpm]
(2) iptables-services-1.4.21-28.el7.x86_64.rpm下载链接:[http://mirrors.163.com/centos/7/os/x86_64/Packages/iptables-services-1.4.21-28.el7.x86_64.rpm]
(1) 安装前的操作
#创建目录
[root@ZabbixServer ~]# cd /usr
[root@ZabbixServer usr]# mkdir iptables
#将本地下载的安装包上传到目标服务器
[root@ZabbixServer iptables]# ll
总用量 488
-rw-r--r-- 1 root root 442964 5月 29 18:26 iptables-1.4.21-28.el7.x86_64.rpm
-rw-r--r-- 1 root root 53128 5月 29 18:26 iptables-services-1.4.21-28.el7.x86_64.rpm
#为安装添加执行权限
[root@ZabbixServer iptables]# chmod +x iptables-1.4.21-28.el7.x86_64.rpm
[root@ZabbixServer iptables]# chmod +x iptables-services-1.4.21-28.el7.x86_64.rpm
(2) 安装iptables
#开始安装
[root@ZabbixServer iptables]# rpm -Uvh iptables-1.4.21-28.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:iptables-1.4.21-28.el7 ################################# [ 50%]
正在清理/删除...
2:iptables-1.4.21-17.el7 ################################# [100%]
[root@ZabbixServer iptables]# rpm -Uvh iptables-services-1.4.21-28.el7.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:iptables-services-1.4.21-28.el7 ################################# [100%]
(3) 安装后验证
#验证是否安装成功
[root@ZabbixServer iptables]# rpm -qa |grep iptables
iptables-services-1.4.21-28.el7.x86_64
iptables-1.4.21-28.el7.x86_64
[root@ZabbixServer iptables]#
#查看状态,Active: inactive (dead)为未启动
[root@ZabbixServer iptables]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@ZabbixServer iptables]#
2 启动iptables
(1) 设置开机启动
[root@localhost ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@localhost ~]#
(2)启动iptables
#可用命令:systemctl [stop|start|restart] iptables
[root@localhost ~]# systemctl start iptables
(3)查看状态
#Active: active (exited)表示已经启动
[root@localhost ~]# service iptables status
Redirecting to /bin/systemctl status iptables.service
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; enabled; vendor preset: disabled)
Active: active (exited) since 三 2019-05-29 15:06:27 CST; 6s ago
Process: 2409 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
Main PID: 2409 (code=exited, status=0/SUCCESS)
5月 29 15:06:27 localhost.localdomain systemd[1]: Starting IPv4 firewall with iptables...
5月 29 15:06:27 localhost.localdomain iptables.init[2409]: iptables: Applying firewall rules: [ 确定 ]
5月 29 15:06:27 localhost.localdomain systemd[1]: Started IPv4 firewall with iptables.
[root@localhost ~]#
3 设置白名单
3.1 查看默认规则
#查看iptables现有规则
[root@localhost ~]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#
3.2 设置白名单
iptables配置文件目录/etc/sysconfig/iptables。由于本次安全加固生产环境服务器较多,所以定义脚本,执行脚本使策略生效。运维的同学可在目标服务器上做如下操作:
(1) 新建脚本文件,命令:vi /opt/iptables.sh
(2) 拷贝下面的脚本内容到/opt/iptables.sh
(3) 执行脚本:sh /opt/iptables.sh
需修改脚本中开放的端口和白名单地址
脚本内容:
#脚本名称:iptables.sh,脚本内容如下:
#!/bin/sh
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#定义白名单
iptables -N whitelist
#设置白名单的ip或ip段
iptables -A whitelist -s 192.168.56.3 -j ACCEPT
#iptables -A whitelist -s 192.168.56.0 -j ACCEPT
#重新设置iptables
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#只有白名单才能访问,测试开发了22、8010,其它应用根据需求添加即可。
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j whitelist
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8010 -j whitelist
iptables -A INPUT -p icmp -j whitelist
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
#配置写入文件,重启也生效。
service iptables save
#重启iptables服务
systemctl restart iptables.service
3.3 查看规则
再次查看配置是否已经生效,whitelist为白名单,下面的配置表示icmp、tcp的22、8010端口只能白名单中192.168.56.3访问。
[root@springboot sysconfig]# iptables -L -n
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
whitelist tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
whitelist tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8010
whitelist icmp -- 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain whitelist (3 references)
target prot opt source destination
ACCEPT all -- 192.168.56.3 0.0.0.0/0
[root@springboot sysconfig]#
4 测试白名单
目标机器:192.168.56.5【白名单生效的机器】
源机器1:192.168.56.3【在白名单中】
源机器2:192.168.56.6【不在白名单中】
4.1 测试ICMP
(1)在源机器1上可以ping通,表示icmp配置生效。
PS C:\WINDOWS\system32> ping 192.168.56.5
正在 Ping 192.168.56.5 具有 32 字节的数据:
来自 192.168.56.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.5 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.56.5 的回复: 字节=32 时间<1ms TTL=64
192.168.56.5 的 Ping 统计信息:
数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 0ms,最长 = 0ms,平均 = 0ms
Control-C
PS C:\WINDOWS\system32>
(2)在源机器2上不能ping通,表示icmp配置生效。
[root@appos /]# ping 192.168.56.5
PING 192.168.56.5 (192.168.56.5) 56(84) bytes of data.
4.2 测试TCP
目标机器192.168.56.5部署了REST测试接口、端口是8010。其它应用根据需求添加即可。
(1)在源机器1的访问接口,可以返回数据,表示tcp配置生效。

(2)在源机器2的访问接口,不能返回数据,表示tcp配置生效。
[root@appos /]# curl 192.168.56.5:8010/speech/test
执行后没有反应。