CentOS 7 安装使用 iptables 防火墙方法介绍

Cen­tOS 7 默认的防火墙使用的是 fire­walld,不是 ipt­a­bles。当你还在尝试运行 ipt­a­bles 的一些常用命令会报错误提示:例如 The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

如果想在 Cen­tOS 7 系统中安装使用 ipt­a­bles,首先应该停用默认的 fire­walld,然后重新安装 ipt­a­bles 服务,并且开启 80/​3306/​22 等常用端口。

图片[1]-CentOS 7 安装使用 iptables 防火墙方法介绍-百科资源

重新启用iptables方法

1、禁用 fire­walld

systemctl stop firewalld.service
systemctl disable firewalld.service

2、安装或更新服务

yum install iptables-services

3、编辑 ipt­a­bles 防火墙配置文件

运行命令 vi /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 ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

输入 :wq 保存退出

systemctl start iptables.service      #开启iptables防火墙
systemctl restart iptables.service      #重启iptables防火墙使配置生效
systemctl enable iptables.service      #设置iptables防火墙开机启动
iptables -L      #查看防火墙规则,默认的是-t filter,如果是nat表查看,即iptables -t nat -L

4、关闭 SELINUX

运行命令 vi /etc/selinux/config

#SELINUX=enforcing      #注释掉
#SELINUXTYPE=targeted      #注释掉
SELINUX=disabled      #增加
setenforce 0      #使配置立即生效

© 版权声明
THE END
喜欢就支持一下吧
点赞5赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容