CentOS7下操作iptables防火墙和firewalld防火墙

简介: CentOS7下操作iptables防火墙和firewalld防火墙

【1】安装iptables

步骤与命令如下:

#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables
#升级iptables(安装的最新版本则不需要)
yum update iptables 
#安装iptables-services
yum install iptables-services

永久关闭防火墙:

[root@localhost ~] chkconfig   iptables off

永久关闭后启用:

[root@localhost ~] chkconfig   iptables on

【2】禁用/停止自带的firewalld服务

步骤与命令如下:

#查看firewalld运行状态
systemctl status firewalld
#启动firewalld服务
systemctl start firewalld
#停止firewalld服务
systemctl stop  firewalld
#禁用firewalld服务
systemctl disable  firewalld

【3】设置现有规则

步骤与命令参考如下:

#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
#开放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允许ping
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
iptables -P INPUT DROP
#所有出站一律绿灯
iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
iptables -P FORWARD DROP

其他参考规则如下:

#如果要添加内网ip信任(接受其所有TCP请求)
iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT
#过滤所有非以上规则的请求
iptables -P INPUT DROP
#要封停一个IP,使用下面这条命令:
iptables -I INPUT -s ***.***.***.*** -j DROP
#要解封一个IP,使用下面这条命令:
iptables -D INPUT -s ***.***.***.*** -j DROP

【4】保存规则设定

将会在/etc/sysconfig/路径下生产iptables文件。


#保存上述规则
service iptables save

aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMjIxMTc0NjAxNjY1.png


【5】开启iptables服务

步骤与命令如下:

#注册iptables服务
#相当于以前的chkconfig iptables on
systemctl enable iptables.service
#开启服务
systemctl start iptables.service
#查看状态
systemctl status iptables.service

aHR0cDovL2ltZy5ibG9nLmNzZG4ubmV0LzIwMTcxMjIxMTc0NzU0OTk5.png


参考iptables完整配置如下:



【6】Centos7下服务基本使用命令

  • 启动一个服务:systemctl start firewalld.service
  • 关闭一个服务:systemctl stop firewalld.service
  • 重启一个服务:systemctl restart firewalld.service
  • 显示一个服务的状态:systemctl status firewalld.service
  • 在开机时启用一个服务:systemctl enable firewalld.service
  • 在开机时禁用一个服务:systemctl disable firewalld.service
  • 查看服务是否开机启动:systemctl is-enabled firewalld.service
  • 查看已启动的服务列表:systemctl list-unit-files|grep enabled
  • 查看启动失败的服务列表:systemctl–failed

【7】Centos7下firewalld防火墙的使用

① 配置firewalld一些命令

查看版本: firewall-cmd --version

查看帮助: firewall-cmd --help

显示状态: firewall-cmd --state

查看所有打开的端口: firewall-cmd --zone=public --list-ports

更新防火墙规则: firewall-cmd --reload

查看区域信息: firewall-cmd --get-active-zones

查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0

拒绝所有包:firewall-cmd --panic-on

取消拒绝状态: firewall-cmd --panic-off

查看是否拒绝: firewall-cmd --query-panic

添加服务:

firewall-cmd --add-service=http --permanent

查看所有开放服务:

[root@bogon apache-tomcat-8.5.42]#  firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client http ssh
  ports: 80/tcp 8080/tcp 8081/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:
[root@bogon apache-tomcat-8.5.42]# firewall-cmd --zone=public --list-ports
80/tcp 8080/tcp 8081/tcp

② 添加一个端口

命令如下:

firewall-cmd --add-port=8081/tcp --permanent
#或如下
firewall-cmd --zone=public --add-port=3306/tcp --permanent   
//(--permanent永久生效,没有此参数重启后失效)

重新载入

firewall-cmd --reload


查看

firewall-cmd --zone=public --query-port=3306/tcp

③ 删除一个端口

firewall-cmd --zone=public --remove-port=3306/tcp --permanent


目录
相关文章
|
28天前
|
Linux 网络安全
Centos 防火墙端口控制命令
Centos 防火墙端口控制命令
32 3
|
3月前
|
安全 算法 Linux
CentOS7下部署长亭科技雷池Web应用防火墙(WAF)开源社区版
CentOS7下部署长亭科技雷池Web应用防火墙(WAF)开源社区版
445 0
|
3天前
|
XML 安全 Linux
【Linux】深入探究CentOS防火墙(Firewalld):基础概念、常用命令及实例操作
【Linux】深入探究CentOS防火墙(Firewalld):基础概念、常用命令及实例操作
|
14天前
|
Linux 网络安全
centos7如何关闭防火墙
centos7如何关闭防火墙
18 3
|
1月前
|
运维 Linux 应用服务中间件
Centos7如何配置firewalld防火墙规则
Centos7如何配置firewalld防火墙规则
53 0
|
3月前
|
监控 网络协议 Linux
防火墙规则动态管理器 - firewalld
【1月更文挑战第11天】
53 0
|
4月前
|
网络协议 Linux 网络安全
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
Centos7中如何打开和关闭防火墙??CentOS 7以上默认使用firewall作为防火墙改为iptables
|
运维 Linux 网络安全
Centos 运维之防火墙篇——②firewalld
防火墙 一般来说,iptables和firewalld启用一个即可
208 0
|
网络协议 Linux 网络安全
Centos 7防火墙firewalld开放80端口
开启80端口 1、firewall-cmd --zone=public --add-port=80/tcp --permanent  出现success表明添加成功  命令含义: --zone #作用域 --add-port=80/tcp  #添加端口,格...
8184 0