Linux(CentOS / RHEL 7) 防火墙
CentOS / RHEL 7 防火墙
Table of Contents
1 简述
CentOS 7 与 CentOS 6 对于系统功能的管理与维护方式上发生了巨大的变化,当然CentOS 7 仍兼容CentOS 6中的管理方式。
CentOS 7 中使用的是 systemctl 命令来管理与维护系统功能。而CentOS 6 及之前的版本 中使用的是service命令。
CentOS 7 中防火墙的名字是firewalld[.service] . 防火墙本身也提供了管理命令: firewall-cmd. firewalld 是 iptables的前端控制器, 是 iptables 的一个封装,可以让你更容易地管理 iptables 规则,虽然 iptables 命令仍可用于 FirewallD,但建议使用 FirewallD 时仅 使用 FirewallD 命令。
与iptables相比,区别有以下两点:
- FirewallD 使用区域和服务而不是链式规则。
- 它动态管理规则集,允许更新规则而不破坏现有会话和连接。
根据RHEL 官网中的描述,防火墙按照网络的可信任度,分为了几个级别,根据不同的信任级别对 数据包选择不同的处理方式.
- firewalld网络分类(zone)
-
- drop 丢弃所有进入的包,而不给出任何响应
- block 拒绝所有外部发起的连接,允许内部发起的连接
- public 允许指定的进入连接
- external 同上,对伪装的进入连接,一般用于路由转发
- dmz 允许受限制的进入连接
- work/home 允许受信任的计算机被限制的进入连接,类似于工作组
- internal 同上,范围针对所有互联网用户
- trusted 信任所有连接
2 常用基本操作
2.1 查看防火墙状态
firewall-cmd --state
或者
systemctl status firewalld
示例:
[root@localhost ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@localhost ~]# firewall-cmd --state
not running
2.2 开启防火墙
systemctl start firewalld
或者
systemctl start firewalld.service
2.3 关闭防火墙
systemctl stop firewalld
或者
systemctl stop firewalld.service
2.4 开机自动启动防火墙
systemctl enable firewalld
或者
systemctl enable firewalld.service
2.5 开机关闭防火墙
systemctl disenable firewalld
或者
systemctl disenable firewalld.service
3 firewalld配置
关于firewalld 命令的使用非常简单,使用/firewall-cmd –help/ 查看帮助文档。 下面只是列出其中几个常用的功能。
3.1 重新加载配置
firewall-cmd --reload # 更新规则,不重启服务
firewall-cmd --complete-reload # 更新规则,重启服务
3.2 zone
zone的配置文件位于/etc/firewalld/zones中,默认只有public类的配置文件。
3.2.1 默认zone
查看默认zone
firewall-cmd --get-default-zone
设置默认zone
firewall-cmd --set-default-zone=<zone_name>
3.2.2 查看zone信息
zone信息中包含了对应的网卡.
firewall-cmd --get-active-zones
示例:
[root@localhost ~]# firewall-cmd --get-active-zones
public
interfaces: eth0 eth1
3.2.3 查看网卡对应zone
firewall-cmd --get-zone-of-interface=eth0
示例:
[root@localhost ~]# firewall-cmd --get-zone-of-interface=eth0
public
3.2.4 将网卡添加至zone中
示例:
firewall-cmd --zone=public --add-interface=eth0 --permanent
3.2.5 查看指定zone打开的端口
firewall-cmd --zone=public --list-ports
示例:
[root@localhost ~]# firewall-cmd --zone=public --list-ports [root@localhost ~]#
3.3 服务管理
设置zone的服务时,所做的修改最终会保存到/etc/firewalld/zones/<zone_name>.xml 所以,我们可以直接修改zone的配置文件,然后再执行 firewall-cmd –reload 重新加载
查看支持的服务
firewall-cmd --get-service
结果中,不同的服务之间使用空格分隔。实际上,这些服务的名字来源于/usr/lib/firewalld/services/ 和/etc/firewalld/services,firewalld 先在后者中查找服务,然后再在前者中查找服务。 服务的配置文件保存为xml格式。如下:
[root@localhost ~]# ls /usr/lib/firewalld/services/
amanda-client.xml dhcpv6-client.xml ganglia-master.xml jenkins.xml minidlna.xml ovirt-imageio.xml puppetmaster.xml smtps.xml tftp.xml
amanda-k5-client.xml dhcpv6.xml git.xml kadmin.xml mongodb.xml ovirt-storageconsole.xml quassel.xml smtp.xml tinc.xml
bacula-client.xml dhcp.xml gre.xml kerberos.xml mosh.xml ovirt-vmconsole.xml radius.xml snmptrap.xml tor-socks.xml
bacula.xml dns.xml high-availability.xml kibana.xml mountd.xml pmcd.xml redis.xml snmp.xml transmission-client.xml
bgp.xml docker-registry.xml https.xml klogin.xml mssql.xml pmproxy.xml RH-Satellite-6.xml spideroak-lansync.xml upnp-client.xml
bitcoin-rpc.xml docker-swarm.xml http.xml kpasswd.xml ms-wbt.xml pmwebapis.xml rpc-bind.xml squid.xml vdsm.xml
bitcoin-testnet-rpc.xml dropbox-lansync.xml imaps.xml kprop.xml murmur.xml pmwebapi.xml rsh.xml ssh.xml vnc-server.xml
bitcoin-testnet.xml elasticsearch.xml imap.xml kshell.xml mysql.xml pop3s.xml rsyncd.xml syncthing-gui.xml wbem-https.xml
bitcoin.xml freeipa-ldaps.xml ipp-client.xml ldaps.xml nfs3.xml pop3.xml samba-client.xml syncthing.xml xmpp-bosh.xml
ceph-mon.xml freeipa-ldap.xml ipp.xml ldap.xml nfs.xml postgresql.xml samba.xml synergy.xml xmpp-client.xml
ceph.xml freeipa-replication.xml ipsec.xml libvirt-tls.xml nmea-0183.xml privoxy.xml sane.xml syslog-tls.xml xmpp-local.xml
cfengine.xml freeipa-trust.xml ircs.xml libvirt.xml nrpe.xml proxy-dhcp.xml sips.xml syslog.xml xmpp-server.xml
condor-collector.xml ftp.xml irc.xml managesieve.xml ntp.xml ptp.xml sip.xml telnet.xml zabbix-agent.xml
ctdb.xml ganglia-client.xml iscsi-target.xml mdns.xml openvpn.xml pulseaudio.xml smtp-submission.xml tftp-client.xml zabbix-server.xml此时,如果我们在这个路径里添加一个xml文件,再执行/firewall-cmd –get-service/看看,
[root@localhost services]# cd /usr/lib/firewalld/services/
[root@localhost services]# cp ftp.xml ftp1.xml
[root@localhost services]# systemctl stop firewalld
[root@localhost services]# systemctl start firewalld
[root@localhost services]# firewall-cmd --get-service
RH-Satellite-6 amanda-client amanda-k5-client bacula bacula-client bgp bitcoin bitcoin-rpc bitcoin-testnet bitcoin-testnet-rpc ceph ceph-mon cfengine condor-collector ctdb dhcp dhcpv6 dhcpv6-client dns docker-registry docker-swarm dropbox-lansync elasticsearch freeipa-ldap freeipa-ldaps freeipa-replication freeipa-trust ftp ftp1 ganglia-client ganglia-master git gre high-availability http https imap imaps ipp ipp-client ipsec irc ircs iscsi-target jenkins kadmin kerberos kibana klogin kpasswd kprop kshell ldap ldaps libvirt libvirt-tls managesieve mdns minidlna mongodb mosh mountd ms-wbt mssql murmur mysql nfs nfs3 nmea-0183 nrpe ntp openvpn ovirt-imageio ovirt-storageconsole ovirt-vmconsole pmcd pmproxy pmwebapi pmwebapis pop3 pop3s postgresql privoxy proxy-dhcp ptp pulseaudio puppetmaster quassel radius redis rpc-bind rsh rsyncd samba samba-client sane sip sips smtp smtp-submission smtps snmp snmptrap spideroak-lansync squid ssh syncthing syncthing-gui synergy syslog syslog-tls telnet tftp tftp-client tinc tor-socks transmission-client upnp-client vdsm vnc-server wbem-https xmpp-bosh xmpp-client xmpp-local xmpp-server zabbix-agent zabbix-server从结果中看, 我们添加了一个ftp1.xml文件,重启firewalld服务后,该文件被识为一个服务。 因此, 如果有需要,我们可以在这个路径中添加firewalld支持的服务。
修改服务配置后,使用 firewall-cmd –reload 重新加载。
启用某个服务
firewall-cmd --zone=public --add-service=ftp //临时
firewall-cmd --permanent --zone=public --add-service=ftp //永久查看开启的服务
firewall-cmd --permanent --zone=public --list-services
设置IP访问服务
firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="10.235.0.10/24" service name="http" accept"
3.4 端口
添加端口
firewall-cmd --permanent --zone=public --add-port=8080-8081/tcp //永久
firewall-cmd --zone=public --add-port=8080-8081/tcp //临时删除端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
查看开启的端口
firewall-cmd --permanent --zone=public --list-ports
IP伪装 端口转发需要先开启masquerade,可以理解成ip伪装,一般情况下,也就是用于端口转发. 下面是该功能的的基本用法:
# 查看当前是否开启IP伪装功能
firewall-cmd --zone=public --query-masquerade
#打开伪装
firewall-cmd --zone=public --add-masquerade
#关闭伪装
firewall-cmd --zone=public --remove-masquerade示例如下:
[root@localhost zones]# firewall-cmd --get-default-zone
public
[root@localhost zones]# firewall-cmd --zone=public --query-masquerade
no
[root@localhost zones]# firewall-cmd --zone=public --add-masquerade
success
[root@localhost zones]# firewall-cmd --zone=public --query-masquerade
yes
[root@localhost zones]# firewall-cmd --zone=public --remove-masquerade
success端口转发 在进行端口转发前,需要先开启IP伪装功能。然后再配置转发。
# 开启端口转发
firewall-cmd --zone=external --add-masquerade # 转发 tcp 22 端口至 8181端口 firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=8181
# 转发 22 端口数据至另一个ip的相同端口上
firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toaddr=10.10.100.10 # 转发 22 端口数据至另一ip的1919端口上
firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=1919:toaddr=10.10.100.10
3.5 拒绝所有包
紧急情况下才会使用。
查看
firewall-cmd --query-panic
开启
firewall-cmd --panic-on
关闭
firewall-cmd --panic-off
- firewalld 3.0 之前,使用的是–enable-panic 和 –disable-panic。
Linux(CentOS / RHEL 7) 防火墙的更多相关文章
- Linux & CentOS & RHEL
1.修改centos7的系统编码:https://blog.csdn.net/violet_echo_0908/article/details/58063555 2.windows 环境下使用ultr ...
- Linux CentOS 下关闭防火墙
永久关闭(重启后生效) 开启: chkconfig iptables on 关闭: chkconfig iptables off 临时关闭(重启后失效) 开启: service iptables st ...
- 在CentOS/RHEL/Scientific Linux 6下安装 LAMP
LAMP 是服务器系统中开源软件的一个完美组合.它是 Linux .Apache HTTP 服务器.MySQL 数据库.PHP(或者 Perl.Python)的第一个字母的缩写代码.对于很多系统管理员 ...
- redhat linux/CentOS 6/7 如何关闭防火墙?
redhat linux/CentOS 6/7 如何关闭防火墙?关闭防火墙iptables的具体命令如下: 临时性的完全关闭防火墙,可以不重启机器(但是重启服务器后iptables防火墙服务会自动随系 ...
- Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7
Install LAMP Server (Apache, MariaDB, PHP) On CentOS/RHEL/Scientific Linux 7 By SK - August 12, 201 ...
- 【Linux笔记】CentOS&RHEL YUM基础知识
以下内容收集自网络,以作参考. 一.YUM是什么 YUM = Yellow dog Updater, Modified. 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它 ...
- 【Linux】YUM Repositories for CentOS, RHEL & Fedora Systems
这里是官方wiki:https://wiki.centos.org/AdditionalResources/Repositories 一.简介 YUM(Yellowdog Updater Modifi ...
- linux 安装禅道 和 CentOS 7 开放防火墙端口 命令
linux 安装禅道链接: https://www.cnblogs.com/maohuidong/p/9750202.html CentOS 7 开放防火墙端口 命令 链接:https://www. ...
- 在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件。
原因:在新安装的Linux系统中,防火墙默认是被禁掉的,一般也没有配置过任何防火墙的策略,所有不存在/etc/sysconfig/iptables文件. 解决办法: .随便写一条iptables命令配 ...
随机推荐
- CI/CD持续集成小结
一.概念 什么是devops,基于Gitlab从零开始搭建自己的持续集成流水线(Pipeline) https://blog.csdn.net/chengzi_comm/article/details ...
- C++中volatile
volatile只保证其“可见性”,不保证其“原子性”. 执行count++;这条语句由3条指令组成: (1)将 count 的值从内存加载到 cpu 的某个 寄存器r: (2)将 寄存器r 的值 + ...
- mount -t proc none /proc
linux initrd里的init脚本中的第一句为: mount -t proc /proc /proc 作用是把proc这个虚拟文件系统挂载到/proc目录.这说明initrd需要用到/proc, ...
- Delphi 类类型
- 013.子查询和分页子查询(sql实例)
--1 子查询 如果子查询和表连接都实现的时候,推荐用表连接实现( 一般:能用表连接实现的就用表连接,有些情况用表连接不能 或者不易实现的再选择子查询) 系统:缓存,执行计划技术手段 --1 wher ...
- withdraw搭建
<!-- yuan -->首先得安装rabbitmq(由于运行过程中报错)1.安装rabbitmq服务软件包 : apt install rabbitmq-server2.安装完成后在ra ...
- Python MySQL 数据库连接不同方式
PyMySQL 驱动连接 什么是 PyMySQL?PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb. PyMySQL 遵循 P ...
- k8s部署普罗米修斯
参考链接:https://blog.csdn.net/ywq935/article/details/80818390 https://www.jianshu.com/p/ac8853927528 1. ...
- HDU 6068 - Classic Quotation | 2017 Multi-University Training Contest 4
/* HDU 6068 - Classic Quotation [ KMP,DP ] | 2017 Multi-University Training Contest 4 题意: 给出两个字符串 S[ ...
- react-native-page-scrollview 的使用方法(实现酷炫的分页轮播效果,还支持自定义View)
react-native-page-scrollview 对ScrollView的封装,可以很方便的实现水平,垂直分页轮播效果.而且可以自定义分页宽高,和侧边View的旋转,透明度,大小等. 对于原生 ...