CentOS7 iptables

简介:

在CentOS7上默认安装的是Firewalld防火墙,虽然在新的firewalld中对流量的语法和控制更加的简单,也能让管理者更加清晰的控制访问策略,但是对于大部分习惯使用iptables的用户来说,使用原有的iptables 来控制更加顺手。


在使用iptables 之前需要安装iptables服务。

1
yum  install  iptables-services

在没有安装iptables服务时,是没有/etc/sysconfig/iptables文件的,执行iptables-save也无法保存。


管理iptables服务

在安装了iptables服务后,可以查看配置文件信息来确认通过哪种方式来添加规则:

1
2
3
4
head  -2  /etc/sysconfig/iptables
 
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall

直接对文件进行编辑,添加规则,添加完成之后,使用如下命令检查配置语法是否正确:

1
sh -c  'iptables-restore -t < /etc/sysconfig/iptables'


根据提示,可以对此文件直接编辑,如果文件说明是另一种情况:

1
2
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.

那说明已经安装了system-config-firewall管理工具,使用此工具可以直接对文件进行管理,如果手动编辑,工具将会覆盖之前编辑的内容。

如果是这种情况,可以使用下面的命令对文件进行编辑:

1
2
system-config-firewall-tui
system-config-firewall


当然,也可以使用iptables命令来逐条添加规则,使用如下命令来打印规则信息和保存规则到配置文件中:

1
2
iptables -S
iptables-save


关闭Firewalld服务

在启用iptables服务前,需要关闭并禁用firewalld的服务,以防发生冲突:

1
2
systemctl stop firewalld
firewall-cmd --state

关闭firewalld开机自启动,并禁用手动启动firewalld:

1
2
systemctl disable firewalld
systemctl mask firewalld

如果要恢复firewalld手动启动:

1
systemctl unmask firewalld


生产环境iptables配置实例


对于访问量不高,安全要求比较高的web服务而暴露在公网的web服务器(一般人不会这么干),可以使用如下规则:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
iptables -I INPUT -p tcp -s 203.186.52.53 --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
      
iptables -I OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
        
iptables -I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
         
iptables -I INPUT  -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
         
iptables -I OUTPUT  -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
          
iptables -I INPUT -p icmp --icmp- type  echo -request -j ACCEPT
         
iptables -I OUTPUT -p icmp --icmp- type  echo -reply -j ACCEPT
         
iptables -I INPUT -i lo -j ACCEPT
         
iptables -I OUTPUT -o lo -j ACCEPT
        
iptables -I OUTPUT -p udp -o eth0 --dport 53 -j ACCEPT
         
iptables -I INPUT -p udp -i eth0 --sport 53 -j ACCEPT
 
iptables -P INPUT DROP
 
iptables -P OUTPUT DROP

查看具体的配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# iptables -S
-P INPUT DROP
-P FORWARD ACCEPT
-P OUTPUT DROP
-A INPUT -i eth0 -p udp -m udp --sport 53 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p icmp -m icmp --icmp- type  8 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -s 203.186.52.53 -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p udp -m udp --dport 53 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp- type  0 -j ACCEPT
-A OUTPUT -p tcp -m tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

使用iptables-save保存配置即可。

警告:如果是远程配置iptables,需要使用 iptables -F 时一定要先确保默认的 INPUT和OUTPUT 规则为ACCEPT,否则执行清空命令后,默认规则依然是DROP,不会被清除,将永远与远程主机断开连接。


可借助crontab,每15分钟恢复到默认,直到iptables配置完成。


参考链接:

https://www.digitalocean.com/community/tutorials/how-to-migrate-from-firewalld-to-iptables-on-centos-7 




 本文转自 酥心糖 51CTO博客,原文链接:http://blog.51cto.com/tryingstuff/1944082
相关文章
|
11月前
|
Linux
centos使用iptables实现nat端口转发
centos使用iptables实现nat端口转发
423 4
|
安全 Linux 网络安全
百度搜索:蓝易云【服务器安全设置Centos7 防火墙firewall与iptables】
CentOS 7使用的默认防火墙是firewall,它是一种基于Netfilter的用户空间工具,用于管理Linux内核中的iptables规则。为了加强服务器的安全性,可以通过配置CentOS 7防火墙和iptables规则来保护服务器。
130 0
|
4月前
|
网络协议 Linux 网络安全
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
|
网络协议 Linux 网络安全
CentOS 7 :Failed to start IPv4 firewall with iptables.
CentOS 7 :Failed to start IPv4 firewall with iptables.
187 0
|
NoSQL Linux 网络安全
CentOS使用iptables禁止某IP访问
CentOS使用iptables禁止某IP访问
615 0
|
Linux 网络安全
iptables防火墙限制 centos中有 firewalld selinux 还有 iptables
iptables防火墙限制 centos中有 firewalld selinux 还有 iptables
112 0
iptables防火墙限制 centos中有 firewalld selinux 还有 iptables
|
运维 安全 网络协议
Centos 运维之防火墙篇——①iptables
防火墙 一般来说,iptables和firewalld启用一个即可
235 0
|
Linux 网络安全
centOS7.3 安装启用 iptables 记录
iptables 执行规则时,是从从规则表中从上至下顺序执行的,如果没遇到匹配的规则,就一条一条往下执行,如果遇到匹配的规则后,那么就执行本规则
155 0
|
网络协议 Linux 网络安全
CentOS 7.2:Failed to start IPv4 firewall with iptables
CentOS 7.2:Failed to start IPv4 firewall with iptables
1003 0