centos编辑iptables?centos创建一个文件
centos7 iptables
在 CentOS7系统中,firewalld和 iptables是两种常见的网络防火墙解决方案。虽然两者都提供了类似的功能,但在使用场景和配置方式上存在显著差异。
iptables默认规则相对开放,而 firewalld默认规则相对封闭。因此,推荐使用 iptables进行更细致的网络控制。
iptables的规则在/etc/sysconfig/iptables中存储,而 firewalld的配置则储存在/usr/lib/firewalld/和/etc/firewalld/中的 XML文件中。iptables在更改规则时,会清除所有旧规则并读取新规则,而 firewalld则不会创建新规则,仅运行不同规则,使其在运行时改变设置而不丢失当前配置。
在使用 firewalld时,可以通过以下命令查看和管理防火墙规则:
bash
firewall-cmd--list-all
firewall-cmd--list-services
firewall-cmd--list-port
firewall-cmd--reload
firewall-cmd--add-port=8080/tcp--permanent
firewall-cmd--add-service=http--permanent
firewall-cmd--remove-port=8080/tcp--permanent
firewall-cmd--remove-service=http--permanent
firewall-cmd--add-forward-port=port=80:proto=tcp:toport=8080--permanent
firewall-cmd--zone=public--add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.217.128--permanent
为了使用 iptables,需先关闭 firewalld并安装 iptables:
bash
systemctl stop firewalld.service
systemctl disable firewalld.service
yum-y install iptables-services
systemctl restart iptables.service
在 iptables中配置规则的常用命令有:
bash
iptables-L-n
iptables-F
iptables-X
可以使用以下方式在 iptables中修改防火墙规则:
修改/etc/sysconfig/iptables文件(重启 iptables使更改生效)。
直接使用命令(需要使用 `save`命令使配置生效)。
例如,直接添加一条开放 8080端口的规则:
bash
iptables-I INPUT-p tcp-m state--state NEW-m tcp--dport 8080-j ACCEPT
service iptables save
遇到的问题是,使用 `iptables-A`添加规则时,虽然规则被写入配置文件,但无法访问。后来发现使用 `iptables-I`添加配置可以解决问题。原因是 `-A`添加在规则列表末尾,而 `-I`添加在第一条,因此优先级不同。
在 iptables中,可以使用以下规则进行更细粒度的控制:
同时开放多个端口:`iptables-I INPUT-p tcp-m multiport--dport 22,80-j ACCEPT`。
开放连续端口范围:`iptables-I INPUT-p tcp--dport 5000:6000-j ACCEPT`。
允许特定网段访问:`iptables-I INPUT-p all-s 0.0.0.0/0-j ACCEPT`。
允许特定 IP的特定端口访问:`iptables-I INPUT-p tcp-s 0.0.0.0-dport 8080-j ACCEPT`。
禁止特定主机访问:`iptables-I INPUT-p tcp-s 0.0.0.0-j DROP`。
移除规则:`iptables-D INPUT 7`。
在 iptables配置中,放行使用 `ACCEPT`,禁止使用 `DROP`。
Centos7 开启 iptables 日志
Netfilter/Iptables(简称Iptables)是Unix/Linux系统自带的优秀且完全免费的基于包过滤的防火墙工具,其功能强大,使用灵活,能够对流入、流出及流经服务器的数据包进行精细控制。
在实际生产环境中,有时可能需要对特定数据包进行过滤。然而,在复杂的网络环境中,可能出现收不到正常的数据报文的情况。这时,查看数据报文是否被Iptables过滤导致无法接收就显得尤为重要。
下面介绍如何开启Iptables日志,以进行数据报文情况的查看:
首先,在rsyslog.conf中添加配置:
# vim/etc/rsyslog.conf
在文件中添加以下行:
kern.*/var/log/iptables.log
随后重启日志配置:
# systemctl restart rsyslog.service
接着,若需让日志滚动,可在配置文件中添加如下行:
# vim/etc/logrotate.d/syslog
添加以下行:
/var/log/iptables
在Iptables配置中添加日志选项,以测试配置是否生效:
# iptables-A INPUT-j LOG--log-prefix"iptables"
检查日志文件是否生成:
# tailf/var/log/iptables.log
完成测试后,删除测试链:
# iptables-D INPUT-j LOG--log-prefix"iptables"
清空Iptables日志文件:
# echo"">/var/log/iptables.log
此方法能够帮助用户追踪数据包过滤情况,提高网络管理效率。
CentOS中iptables防火墙 开放80端口方法和常用命令
在CentOS中,要开放80端口并配置iptables防火墙,你需要遵循以下步骤:
1.首先,使用命令行添加一个新规则,允许TCP协议的80端口访问:
/sbin/iptables-I INPUT-p tcp--dport 80-j ACCEPT
2.保存你的配置更改,确保防火墙的更改生效:
/etc/rc.d/init.d/iptables save
3.重启iptables服务以应用新的规则:
/etc/init.d/iptables restart
4.检查防火墙的状态,确保80端口已打开:
service iptables status
常用的iptables命令包括配置规则、查看状态、清除规则和屏蔽IP。例如,要允许特定IP访问80端口:
iptables-A INPUT-s 127.0.0.1-d 127.0.0.1-j ACCEPT
iptables-A INPUT-p tcp--dport 80-j ACCEPT
要查看当前的iptables规则,可以使用:
iptables-L-n-v
删除特定规则时,先列出所有规则的序号,再执行:
iptables-L-n--line-numbers
iptables-D INPUT 8
这样,你就成功地在CentOS中配置了iptables防火墙,开放了80端口。