fedora 28 , firewalld 防火墙控制,firewall-cmd 管理防火墙规则

今天,在使用fedora时,需要修改防火墙规则,一时间忘记了命令是什么,这里进行记录一下。 目前 fedora 28/ centos 7 使用 firewalld 作为防火墙软件;下面我就怎么简单管理防火墙规则进行记录,网上的例子很多,这里我列举我经常使用的命令,来进行备忘:

基本使用(使用这些命令需要管理员权限):

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

firewall-cmd 管理防火墙:

查看版本: firewall-cmd --version
查看帮助: firewall-cmd --help
显示状态: firewall-cmd --state
更新防火墙规则: firewall-cmd --reload  (在每次修改玩规则的时候,一定要使用命令来进行重新加载防火墙规则)
查看区域信息:  firewall-cmd --get-active-zones
拒绝所有包:firewall-cmd --panic-on
取消拒绝状态: firewall-cmd --panic-off
查看是否拒绝: firewall-cmd --query-panic
开启和关闭一个端口:
添加
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效, --zone 此参数也可以不带,这样就是查看所有域, 注意:在修改过规则后,一定要进行重新加载规则)
重新载入
firewall-cmd --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent

打开特定服务:

查看可以打开的服务
# firewall-cmd --get-services
查看服务, 特定域
# firewall-cmd --zone=public --list-services 查看服务 # firewall-cmd --list-services 添加服务 # firewall-cmd --zone=public --add-service=http (可以不限制public域)注意:所有的规则在修改过后,一定要使用 reload 命令进行重新加载 firewall-cmd --reload 删除服务 # firewall-cmd --zone=public --remove-service=http

添加特定端口对应的服务:

自定义的服务,一般添加到 firewalld 的 /etc 中的配置文件夹之中:/etc/firewalld/services

当然也可以添加到 /usr/lib/firewalld/services 之中: 

 我们首先查看原有的服务,可见系统已经事先为我们配置好了很多知名的服务,如http, smtp, ftp 等:

root@yaowenxu /u/l/f/services# ls
amanda-client.xml        elasticsearch.xml        kadmin.xml       nfs.xml                   redis.xml              syslog.xml
amanda-k5-client.xml     freeipa-ldaps.xml        kerberos.xml     nmea-0183.xml             RH-Satellite-6.xml     telnet.xml
bacula-client.xml        freeipa-ldap.xml         kibana.xml       nrpe.xml                  rpc-bind.xml           tftp-client.xml
bacula.xml               freeipa-replication.xml  klogin.xml       ntp.xml                   rsh.xml                tftp.xml
bgp.xml                  freeipa-trust.xml        kpasswd.xml      openvpn.xml               rsyncd.xml             tinc.xml
bitcoin-rpc.xml          ftp.xml                  kprop.xml        ovirt-imageio.xml         samba-client.xml       tor-socks.xml
bitcoin-testnet-rpc.xml  ganglia-client.xml       kshell.xml       ovirt-storageconsole.xml  samba.xml              transmission-client.xml
bitcoin-testnet.xml      ganglia-master.xml       ldaps.xml        ovirt-vmconsole.xml       sane.xml               upnp-client.xml
bitcoin.xml              git.xml                  ldap.xml         pmcd.xml                  sips.xml               vdsm.xml
ceph-mon.xml             gre.xml                  libvirt-tls.xml  pmproxy.xml               sip.xml                vnc-server.xml
ceph.xml                 high-availability.xml    libvirt.xml      pmwebapis.xml             smtp-submission.xml    wbem-https.xml
cfengine.xml             https.xml                managesieve.xml  pmwebapi.xml              smtps.xml              xmpp-bosh.xml
cockpit.xml              http.xml                 mdns.xml         pop3s.xml                 smtp.xml               xmpp-client.xml
condor-collector.xml     imaps.xml                minidlna.xml     pop3.xml                  snmptrap.xml           xmpp-local.xml
ctdb.xml                 imap.xml                 mongodb.xml      postgresql.xml            snmp.xml               xmpp-server.xml
dhcpv6-client.xml        ipp-client.xml           mosh.xml         privoxy.xml               spideroak-lansync.xml  zabbix-agent.xml
dhcpv6.xml               ipp.xml                  mountd.xml       proxy-dhcp.xml            squid.xml              zabbix-server.xml
dhcp.xml                 ipsec.xml                mssql.xml        ptp.xml                   ssh.xml
dns.xml                  ircs.xml                 ms-wbt.xml       pulseaudio.xml            syncthing-gui.xml
docker-registry.xml      irc.xml                  murmur.xml       puppetmaster.xml          syncthing.xml
docker-swarm.xml         iscsi-target.xml         mysql.xml        quassel.xml               synergy.xml
dropbox-lansync.xml      jenkins.xml              nfs3.xml         radius.xml                syslog-tls.xml
View Code

我们查看 http.xml 文件的内容:

root@yaowenxu /u/l/f/services# cat http.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>WWW (HTTP)</short>
  <description>HTTP is the protocol used to serve Web pages. If you plan to make your Web server publicly available, enable this option. This option is not required for viewing pages locally or developing Web pages.</description>
  <port protocol="tcp" port="80"/>
</service>

我们对新增服务进行举例:

就要新建一个服务,在/usr/lib/firewalld/services,随便拷贝一个xml文件到一个新名字,比如myservice.xml,把里面的short改为想要名字(这个名字只是为了人来阅读,没有实际影响。重要的是修改 protocol和port。修改完保存。重启firewalld服务。否则可能提示找不到刚才新建的service。

然后把新建的service添加到firewalld:

firewall-cmd --permanent --add-service=myservice

firewall-cmd --reload 后生效。


 保持更新,转载请注明出处。如果对您有帮助,请点击右下角的推荐进行支持。

 

posted @ 2018-08-20 14:52  Michael-Xu  阅读(1490)  评论(0编辑  收藏  举报