centos 7 firewalld 防火墙 添加,修改 使用方法

官方文档介绍地址:

添加链接描述
一、firewall介绍
CentOS 7中防火墙是一个非常的强大的功能,在CentOS 6.5中在iptables防火墙中进行了升级了。

1、官方介绍

The dynamic firewall daemon firewalld provides a dynamically managed firewall with support for network “zones” to assign a level of trust to a network and its associated connections and interfaces. It has support for IPv4 and IPv6 firewall settings. It supports Ethernet bridges and has a separation of runtime and permanent configuration options. It also has an interface for services or applications to add firewall rules directly.

2、什么是区域Zone:

网络区域定义了网络连接的可信等级。这是一个 一对多的关系,这意味着一次连接可以仅仅是一个区域的一部分,而一个区域可以用于很多连接。

3、哪个区域可用?

由firewalld 提供的区域按照从不信任到信任的顺序排序。

4、区域的分类?

Firewalls can be used to separate networks into different zones based on the level of trust the user has decided to place on the devices and traffic within that network. NetworkManager informs firewalld to which zone an interface belongs. An interface’s assigned zone can be changed by NetworkManager or via the firewall-config tool which can open the relevant NetworkManager window for you.

The zone settings in /etc/firewalld/ are a range of preset settings which can be quickly applied to a network interface. They are listed here with a brief explanation:

drop
Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible.

block
Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated from within the system are possible.

public
For use in public areas. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.

external
For use on external networks with masquerading enabled especially for routers. You do not trust the other computers on the network to not harm your computer. Only selected incoming connections are accepted.

dmz
For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted.

work
For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

home
For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

internal
For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted.

trusted
All network connections are accepted.
It is possible to designate one of these zones to be the default zone. When interface connections are added to NetworkManager, they are assigned to the default zone. On installation, the default zone in firewalld is set to be the public zone.

注:具体内容,请参见官方文档介绍!
二、firewall配置
The configuration for firewalld is stored in various XML files in /usr/lib/firewalld/ and /etc/firewalld/.

This allows a great deal of flexibility as the files can be edited, written to, backed up, used as templates for other installations and so on.

注意:以下firewalld 的操作只有重启之后才有效:service firewalld restart 重启

1、系统配置目录

/usr/lib/firewalld/services
目录中存放定义好的网络服务和端口参数,系统参数,不能修改。

[root@iZ2zedh5vfhwgym3140hxeZ ~] cd /usr/lib/firewalld/services/
[root@iZ2zedh5vfhwgym3140hxeZ services] ls
amanda-client.xml        jenkins.xml               puppetmaster.xml
amanda-k5-client.xml     kadmin.xml                quassel.xml
bacula-client.xml        kerberos.xml              radius.xml
bacula.xml               kibana.xml                redis.xml
bgp.xml                  klogin.xml                RH-Satellite-6.xml
bitcoin-rpc.xml          kpasswd.xml               rpc-bind.xml
bitcoin-testnet-rpc.xml  kprop.xml                 rsh.xml

2、用户配置目录

/etc/firewalld/

[root@iZ2zedh5vfhwgym3140hxeZ firewalld] ls
helpers  icmptypes  ipsets  services  xmlschema  zones

firewalld使用:
1、firewalld的基本使用

启动:
systemctl start firewalld
查看状态:
systemctl status firewalld
停止:
systemctl disable firewalld
禁用:
systemctl stop firewalld
2.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
3.配置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
4:开启端口

firewalld添加使用

firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效) #开启80端口
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone=public --query-port=80/tcp #查看80端口是否开放
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent #删除80端口配置
2.配置firewalld-cmd
查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
查看所有打开的端口: firewall-cmd --zone=public --list-ports
更新防火墙规则: firewall-cmd --reload
更新防火墙规则,重启服务: firewall-cmd --completely-reload
查看已激活的Zone信息: firewall-cmd --get-active-zones
查看指定接口所属区域: firewall-cmd --get-zone-of-interface=eth0
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic

3.信任级别,通过Zone的值指定
drop: 丢弃所有进入的包,而不给出任何响应
block: 拒绝所有外部发起的连接,允许内部发起的连接
public: 允许指定的进入连接
external: 同上,对伪装的进入连接,一般用于路由转发
dmz: 允许受限制的进入连接
work: 允许受信任的计算机被限制的进入连接,类似 workgroup
home: 同上,类似 homegroup
internal: 同上,范围针对所有互联网用户
trusted: 信任所有连接

4.firewall开启和关闭端口
以下都是指在public的zone下的操作,不同的Zone只要改变Zone后面的值就可以
添加:
firewall-cmd --zone=public --add-port=80/tcp --permanent (–permanent永久生效,没有此参数重启后失效)
重新载入:
firewall-cmd --reload
查看:
firewall-cmd --zone=public --query-port=80/tcp
删除:
firewall-cmd --zone=public --remove-port=80/tcp --permanent

5.管理服务
以smtp服务为例, 添加到work zone
添加:
firewall-cmd --zone=work --add-service=smtp
查看:
firewall-cmd --zone=work --query-service=smtp
删除:
firewall-cmd --zone=work --remove-service=smtp

5.配置 IP 地址伪装
查看:
firewall-cmd --zone=external --query-masquerade
打开:
firewall-cmd --zone=external --add-masquerade
关闭:
firewall-cmd --zone=external --remove-masquerade

6.端口转发
打开端口转发,首先需要打开IP地址伪装
  firewall-cmd --zone=external --add-masquerade

转发 tcp 22 端口至 3753:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp:toport=3753
转发端口数据至另一个IP的相同端口:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp:toaddr=192.168.1.112
转发端口数据至另一个IP的 3753 端口:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp::toport=3753:toaddr=192.168.1.112

6.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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值