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

启动服务报错

Failed to start firewald.service: Unit not found.

解决方案:

systemctl unmask firewalld.service

----配置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 --reload
查看
firewall-cmd --zone= public --query-port=80/tcp
删除
firewall-cmd --zone= public --remove-port=80/tcp --permanent
---------------------

允许某IP访问某端口

firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.1.1 port port=11211 protocol=tcp accept'

如果禁用了 firewall 启用的iptables

[root@localhost ~]#      systemctl enable iptables.service               #   設定防火牆開機啟動

--------------------- 啟動/關閉/重啟 iptables ------------------------------------

[root@localhost ~]#  systemctl stop iptables      
[root@localhost ~]#  systemctl start iptables 
[root@localhost ~]#  systemctl restart iptables

--------------------------------------------------------------------------------------------
#   @1,手動新增埠號   (方法一)
[root@localhost ~]#  vi /etc/sysconfig/iptables  #配置防火牆埠

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT



[root@localhost ~]#  service iptables save      #儲存防火牆配置

--end

###################################   具體配置說明   ######################################

@ 2,命令 新增埠號 ( 方法二 ):

一,安裝iptable iptable-service

1,先檢查是否安裝了iptables

[root@localhost ~]# service iptables status

2,安裝iptables

[root@localhost ~]# yum install -y iptables

3,升級iptables

[root@localhost ~]# yum update iptables 

4,安裝iptables-services

[root@localhost ~]# yum install iptables-services

二,禁用/停止自帶的firewalld服務

1,停止firewalld服務

[root@localhost ~]# systemctl stop firewalld

2,禁用firewalld服務

[root@localhost ~]# systemctl mask firewalld

三,設定現有規則

  #檢視iptables現有規則
  [root@localhost ~]# iptables -L -n

  #先允許所有,不然有可能會杯具
  [root@localhost ~]# iptables -P INPUT ACCEPT

  #清空所有預設規則
  [root@localhost ~]# iptables -F

  #清空所有自定義規則
  [root@localhost ~]# iptables -X

  #所有計數器歸0
  [root@localhost ~]# iptables -Z

  #允許來自於lo介面的資料包(本地訪問)
  [root@localhost ~]# iptables -A INPUT -i lo -j ACCEPT

  #開放22埠
  iptables -A INPUT -p tcp --dport 22 -j ACCEPT

  #開放21埠(FTP)
  iptables -A INPUT -p tcp --dport 21 -j ACCEPT

  #開放80埠(HTTP)
  iptables -A INPUT -p tcp --dport 80 -j ACCEPT

  #開放443埠(HTTPS)
  iptables -A INPUT -p tcp --dport 443 -j ACCEPT

  ...

   ====>  按照實際要求:是否加下面的:
  ------------------------------------------------------------------- 

  #允許ping
  iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT

  #允許接受本機請求之後的返回資料 RELATED,是為FTP設定的
  iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

  #其他入站一律丟棄
  iptables -P INPUT DROP

  #所有出站一律綠燈
  iptables -P OUTPUT ACCEPT

  #所有轉發一律丟棄
  iptables -P FORWARD DROP

   -------------------------------------------------------------------

四,其他規則設定

 #如果要新增內網ip信任(接受其所有TCP請求)
 iptables -A INPUT -p tcp -s 45.96.174.68 -j ACCEPT

 #過濾所有非以上規則的請求
 iptables -P INPUT DROP

 #要封停一個IP,使用下面這條命令:
 iptables -I INPUT -s ***.***.***.*** -j DROP

 #要解封一個IP,使用下面這條命令:
 iptables -D INPUT -s ***.***.***.*** -j DROP

五,儲存規則設定

#儲存上述規則
service iptables save

六,開啟iptables服務

#註冊iptables服務
#相當於以前的chkconfig iptables on
systemctl enable iptables.service

#開啟服務
systemctl start iptables.service

#檢視狀態
systemctl status iptables.service

----------------------------------------------------------------------------------------------------

                                                     問題

-----------------------------------------------------------------------------------------------------

問題:解決vsftpd在iptables開啟後,無法使用被動模式的問題

1.首先在/etc/sysconfig/iptables-config中修改或者新增以下內容

#新增以下內容,注意順序不能調換

IPTABLES_MODULES="ip_conntrack_ftp"

IPTABLES_MODULES="ip_nat_ftp"

2.重新設定iptables設定

iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT

以下為完整設定指令碼

#!/bin/sh
iptables -P INPUT ACCEPT
iptables -F
iptables -X
iptables -Z
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP
service iptables save
systemctl restart iptables.service

CentOS7

[root@localhost ~]#   cat    /etc/redhat-release                       #  檢視centos版本
      CentOS Linux release 7.2.1511 (Core)

1.關閉firewall

[root@localhost ~]# systemctl stop firewalld.service             #   停止firewall
[root@localhost ~]# systemctl disable firewalld.service        #  禁止firewall開機啟動

2.安裝iptables

[root@localhost ~]#     yum install iptables-services                     #  安裝

[root@localhost ~]#     systemctl restart iptables.service              #  重啟防火牆使配置生效

[root@localhost ~]#     systemctl enable iptables.service             #  設定防火牆開機啟動

[root@localhost ~]#     systemctl disable iptables.service            #  禁止防火牆開機啟動

CentOS6

[root@localhost ~]#    service iptable status          --檢視防火牆狀態

[root@localhost ~]#    servcie iptables stop           --臨時關閉防火牆

[root@localhost ~]#    service iptables start           --臨時啟動防火牆

[root@localhost ~]#   service iptables restart        --重啟防火牆

[root@localhost ~]#   chkconfig iptables off          --永久關閉防火牆

[root@localhost ~]#   chkconfig iptables on          --永久開啟防火牆

整合自:

https://www.itread01.com/content/1548640287.html

https://blog.csdn.net/qq_38295166/article/details/78822598

posted on 2018-11-15 11:14  mrma1989  阅读(159)  评论(0编辑  收藏  举报