firewalld的介绍与简单应用
CentOS7的默认防火墙是firewalld,在之前使用iptables时,关闭了firewalld服务,现在反过来关闭iptables服务,打开firewalld服务:
systemctl disable iptables
systemctl stop iptables
systemctl enable firewalld
systemctl start firewalld 打开firewalld服务之后,iptables相关的命令其实也可以继续使用的,但在CentOS7中不用那么操作,因为firewalld有自己的命令。
firewalld有两个基础概念,zone和service,每个zone里面有不同的iptables规则,默认一共有9个zone,CentOS7默认zone为public,查看系统所有zone,使用命令
firewall-cmd --get-zones //查看所有zone
firewall-cmd --get-default-zone //查看默认zone 9个zone的简单介绍: drop(丢弃):任何接受的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
block(限制):任何接受的网络连接被IPv4的 icmp-host-prohibited 信息和 IPv6 的icmp6-adm-prohibited 信息所拒绝。
public (公共) :在公共区域内使用,不能相信网络内的其他计算机不会对你的计算机造成危害,只能接收经过选取的连接。
external (外部):特别是为路由器启用了伪装功能的外部网。你不能信任来自网络的其他计算,不能相信它们不会对你的计算机造成危害,只能接收经过选择的连接。
dmz(非军事区):用于的非军事区内的电脑,此区域内可公开访问,可以有限地进入你的内部网络,仅仅接收经过选择的连接。
work(工作):用于工作区。你可以基本相信网络内的其他电脑不会危害你的电脑。仅仅接收经过选择的连接。
home(家庭):用于家庭网络。你可以基本信任网络内的其他计算机不会危害你的计算机。仅仅接收经过选择的连接。
internal(内部):用于内部网络。你可以基本上信任网络内的其他计算机不会威胁你的计算机。仅仅接收经过选择的连接。
trusted(信任):可接受所有的网络连接 几个关于zone的命令
# firewall-cmd --set-default-zone=work //设定默认zone
# firewall-cmd --get-zone-of-interface=ens33 //查看指定网卡
# firewall-cmd --zone=public --add-interface=lo //给指定网卡设置zone
# firewall-cmd --zone=dmz --change-interface=lo //针对网卡更改zone
# firewall-cmd --zone=dmz --remove-interface=lo //针对网卡删除zone
# firewall-cmd --get-zctive-zones //查看系统所有网卡所在的zone fired中的service service 就是zone下面的一个子单元,每个zone里都使用了不同的service,而service就是针对服务(端口)做的iptables规则。
firewall-cmd --get-services //查看当前系统所有service
这些service都是由一个个配置文件定义的,配置文件的模版子啊/usr/lib/firewalld/services/目录下,真正生效的配置文件则在/etc/firewalld/services/目录下(默认为空) 关于service的一些操作命令:
# firewall-cmd --get-services
//查看所有的servies
# firewall-cmd --list-services
//查看当前zone下有哪些service
# firewall-cmd --zone=public --add-service=http
//把http服务增加到public zone下面
# firewall-cmd --zone=public --remove-service=http
//把http服务移除
# ls /usr/lib/firewalld/zones/
//zone的配置文件模板
# firewall-cmd --zone=public --add-service=http --permanent
//更改配置文件,否则只是保存在内存中,之后会在/etc/firewalld/zones目录下面生成配置文件

实例:
需求:ftp服务自定义端口1121,需要在work zone下面放行ftp。
cp /usr/lib/firewalld/services/ftp.xml /etc/firewalld/services/
vi /etc/firewalld/services/ftp.xml
修改ftp端口号 <?xml version="1.0" encoding="utf-8"?>
<service>
<short>FTP</short>
<description>FTP is a protocol used for remote file transfer. If you plan to make your FTP server publicly available, enable this option. You need the vsftpd package installed for this option to be useful.</description>
<port protocol="tcp" port=""/> ##修改端口为1121
<module name="nf_conntrack_ftp"/>
</service>
[root@zlinux ~]# cp /usr/lib/firewalld/zones/work.xml /etc/firewalld/zones/
[root@zlinux ~]# vi /etc/firewalld/zones/work.xml //增加ftp相关配置 <?xml version="1.0" encoding="utf-8"?>
<zone>
<short>Work</short>
<description>For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
<service name="ssh"/>
<service name="dhcpv6-client"/>
<service name="ftp"/> ##增加ftp
</zone>
[root@zlinux ~]# firewall-cmd --reload //重新加载
success
[root@zlinux ~]# firewall-cmd --zone=work --list-services //查看是否有ftp
ssh dhcpv6-client ftp
firewalld一些常用的语句

查看默认的zone,结果为trusted
firewall-cmd --get-default-zone
public
设置默认zone为trusted
firewall-cmd --set-default-zone=trusted
success
显示当前正在使用的zone消息
firewall-cmd --get-active-zones
trusted
interfaces: eno16777736
显示系统预定义的zone,默认是9个
firewall-cmd --get-zones
block dmz drop external home internal public trusted work
显示系统预定义的服务名称:
firewall-cmd --get-services
amanda-client bacula bacula-client dhcp dhcpv6 dhcpv6-client dns ftp high-availability http https imaps ipp ipp-client ipsec kerberos kpasswd ldap ldaps libvirt libvirt-tls mdns mountd ms-wbt mysql nfs ntp openvpn pmcd pmproxy pmwebapi pmwebapis pop3s postgresql proxy-dhcp radius rpc-bind samba samba-client smtp ssh telnet tftp tftp-client transmission-client vnc-server wbem-https
查询eno16接口和那个zone匹配,网卡与trusted匹配,则该网卡的流量执行eno16中定义的规则,默认允许访问所以的服务:
firewall-cmd --get-zone-of-interface=eno16777736
trusted
显示所以zone及其相对应的规则信息
firewall-cmd --list-all-zone
block
interfaces:
sources:
services:
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules: dmz
interfaces:
sources:
services: ssh
ports:
masquerade: no
forward-ports:
icmp-blocks:
rich rules:
.......
在public这个zone中添加允许访问ftp服务的规则
firewall-cmd --add-service=ftp --zone=public
success
从public这个zone中删除允许访问ftp服务的规则
firewall-cmd --remove-service=ftp --zone=public
success
在public这个zone中添加允许访问3306端口的规则
firewall-cmd --add-port=/tcp --zone=public
从public这个zone中删除允许访问3306端口的规则
firewall-cmd --remove-port=/tcp --zone=public
success
将eno16网卡和public绑定,以后从该接口进入的流量,匹配到public中的规则
firewall-cmd --add-interface=eno16 --zone=public
success
将eno16网卡与public解除绑定
firewall-cmd --remove-interface=eno16 --zone
trusted
将源ip地址1.1.1.1与public绑定,以后该主机访问本机时匹配public中的规则
firewall-cmd --add-source=1.1.1.1 --zone=public
success
在public这个zone中添加一条永久规则(允许访问3306端口),规则重启防火墙后依然生效
firewall-cmd --permanent --add-port=/tcp --zone=public
重新加载读取防火墙规则
firewall-cmd --reload
firewall指定端口和ip访问
、启动防火墙
systemctl start firewalld.service
、指定IP与端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="" accept"
、重新载入,使配置生效
systemctl restart firewalld.service
、查看配置结果
firewall-cmd --list-all
、删除规则
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="" accept"
.对指定的IP开放指定的端口段
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="-" accept"

firewalld的防火墙的更多相关文章

  1. CentOS 使用firewalld打开防火墙与端口

    CentOS 使用firewalld打开防火墙与端口 LinuxCentOS 基本使用 启动 : systemctl start firewalld 关闭 : systemctl stop firew ...

  2. centos7 && centos6.5部KVM使用NAT联网并为虚拟机配置firewalld && iptables防火墙端口转发

    centos7 && centos6.5 部KVM使用NAT联网并为虚拟机配置firewalld && iptables防火墙端口转发 一.准备工作: 1: 检查kvm ...

  3. firewalld管理防火墙常用命令

    1.查看防火墙的状态 [root@localhost HMK]# firewall-cmd --state 查看防火墙的运行状态 not running [root@localhost HMK]# s ...

  4. Centos7管理selinux及使用firewalld管理防火墙

    CentOS 7.0默认使用的是firewall作为防火墙 1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status ...

  5. Linux学习笔记之CentOS 7系统使用firewalld管理防火墙端口

    0x00 firewalld的基本使用 # 启动: systemctl start firewalld # 查看状态: systemctl status firewalld # 停止: systemc ...

  6. CentOS7使用firewalld管理防火墙与端口

    firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status fir ...

  7. centos8用firewalld搭建防火墙

    一,firewalld的systemd管理命令 启动:systemctl start firewalld 关闭:systemctl stop firewalld 查看状态:systemctl stat ...

  8. 第二十三章 Firewalld的防火墙

    一.防火墙基本概述 在CentOS7系统中集成了多款防火墙管理工具,默认启用的是firewalld(动态防火墙管理器)防火墙管理工具,Firewalld支持CLI(命令行)以及GUI(图形)的两种管理 ...

  9. CentOS7使用firewalld管理防火墙

    firewalld的基本使用 #启动 systemctl start firewalld #关闭 systemctl stop firewalld #查看状态 systemctl status fir ...

随机推荐

  1. UVA Live Archive 4394 String painter(区间dp)

    区间dp,两个str一起考虑很难转移. 看了别人题解以后才知道是做两次dp. dp1.str1最坏情况下和str2完全不相同,相当于从空白串开始刷. 对于一个区间,有两种刷法,一起刷,或者分开来刷. ...

  2. Android(java)学习笔记82:利用SpannableString设置复合文本

    1. SpannableString设置复合文本: TextView通常用来显示普通文本,但是有时候需要对其中某些文本进行样式.事件方面的设置.Android系统通过SpannableString类来 ...

  3. 全面了解linux情况常用命令

    查看linux服务器CPU详细情况1. 显示CPU个数命令 # cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc ...

  4. vue:vue router学习小结

    序:本篇内容主要侧重对前端路由的理解,以vue的官方路由作为载体,进行一个简单介绍. 一.路由历史: 最早开始的时候,项目开发使用的是SSR,即服务端渲染.这个时候刷新页面,服务器返回的是全部的htm ...

  5. 问题006:为什么用java.exe执行编译的类文件的时候,不这样写java Welcome.class

    为什么用java.exe执行编译的类文件的时候,不这样写java Welcome.class 是因为java虚拟机调用Welcome的时候,已经替我们增减了.class,如果你还要写java Welc ...

  6. FILE对象线程安全

    根据apue讲述: 标准的IO例程可能从它们各自的内部数据结构的角度出发,是以线程安全的方式实现的!但在线程中,如果标准 IO例程都获取它们各自的锁,那么在做一次一个字符的IO时就会出现严重的性能下降 ...

  7. 十五、MySQL DELETE 语句

    MySQL DELETE 语句 你可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 你可以在 mysql> 命令提示符或 PHP 脚本中执行该命令. 语法 ...

  8. php获取随机字符串

    获取随机字符串 /** * 获取随机字符串 * @param int $randLength 长度 * @param int $addtime 是否加入当前时间戳 * @param int $incl ...

  9. 课时21.img标签(掌握)

    1.img标签中的img其实是英文image的缩写,所以img标签的作用,就是告诉浏览器我们需要显示一张图片 2.img标签格式:<img src=" ">  img是 ...

  10. 【PHP】$_SERVER整理

    PHP变成中经常需要用到服务器的一些资料,我在这里整理一下,方便查找.第一部分为比较常用的$_SERVER $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERV ...