CentOS7开启iptables防火墙

CentOS7开启iptables防火墙

从CentOS6升级到CentOS7,防火墙命令变化了,但是还是习惯以前的iptables命令,怎么办?

本文将简单介绍下如何在Centos7开启iptables防火墙。

1.Centos7自带防火墙关闭

  • 查看防火墙状态
# firewall-cmd --state

如果显示是not running,则不需要停止自带的防火墙,因为没开启防火墙。

  • 停止firewall
# systemctl stop firewalld.service
  • 禁止firewall开机启动
# systemctl disable firewalld.service

2.安装配置iptables

  • 安装iptables
# yum install -y iptables-services
  • 编辑防火墙配置文件

这个是默认规则,如果需要增加规则,在该配置文件里修改规则保存后退出。

# vim /etc/sysconfig/iptables

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
  • 重启防火墙
# systemctl restart iptables.service

3.其他命令

  • 设置防火墙开机启动,建议开机自启动,否则重启机器防火墙未开启容易被侵入。
# systemctl enable iptables.service
  • 禁止iptables服务
# systemctl disable iptables
  • 暂停iptables服务
# systemctl stop iptables
  • 解除禁止iptables
# systemctl enable iptables
  • 开启服务
# systemctl start iptables

是不是很简单?相应你学会了。


本文没有授权给任何组织、企业和个人转载,未经作者允许禁止转载!

欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。

公众号二维码

updatedupdated2021-10-072021-10-07