Centos6.X和Centos7.X防火墙的区别

230 阅读2分钟

Centos6.X的默认防火墙使用iptables

前言:iptables的启动文件位置在: /etc/init.d/iptables,srevice iptables调用的就是这里的执行文件

一、命令

1、iptables的命令

service iptables start #启动 
service iptables status #查看状态 
chkconfig iptables on #设置开机自启 
chkconfig iptables off #设置开机禁用

2、iptables常用命令

iptables -h #查询帮助
iptables -L -n # 
iptables -F #清除所有规则

二、语法

INPUT:过滤进入主机的数据包 
OUTPUT:处理从本机出去的数据包 
FORWARD:负责转发流经本机但不进入本机的数据包,起到转发作用 
-A:追加到规则的最后一条 
-D:删除记录 
-I:添加到规则的第一条 
-p:规定通信协议,常用协议:tcp、udp、icmp、all 
-j:指定跳转的目标,常见的目标:ACCEPT(接收数据包)、DROP(丢弃数据包)、REJECT(重定向,一般不使用会带来安全隐患)

三、案例

1、ip过滤

    禁止192.168.200.113 IP地址的所有类型数据接入 
    iptables -A INPUT ! -s 192.168.200.113 -j DROP

 2、开放端口

iptables -A INPUT -P TCP --dport 80 -j ACCEPT #开放80端口

 3、开放端口范围

iptables -I INPUT -p tcp --dport 22:80 -j ACCEPT #开放22-80范围的端口 
service iptables start #再次启动防火墙

4、不允许80端口流出

iptables -I OUTPUT -p tcp --dport 80 -j DROP

Centos7的默认防火墙使用firewalld

一、firewalld的基本使用

启动: systemctl start firewalld 
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用  : systemctl disable firewalld
开机启用  : systemctl enable firewalld

二、systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。

启动一个服务: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

三、.配置firewalld-cmd

查看版本: 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 --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效) 
firewall-cmd --zone=public --add-port=13141/tcp --permanent
重新载入 firewall-cmd --reload
查看 firewall-cmd --zone=public --query-port=80/tcp
删除 firewall-cmd --zone=public --remove-port=80/tcp --permanent