雲服務器 ECS Linux Centos 7 系統 下iptables 服務的使用概述

說明:Centos 7 默認的防火牆是 firewall ,鑑於 iptables 使用的比較廣,本文簡要介紹在 CentOS7.0 系統下 iptables 服務的安裝以及相關的使用方法。

禁用 firewall 開機啓動


爲了防止與 iptables 服務衝突,先禁用 firewall 開機啓動:

  • 使用 systemctl status firewalld 查看服務狀態,active/inactive表明服務是運行/關閉狀態(如下圖)


  1. systemctl status firewalld

1.JPG

  • 如果服務是運行狀態,先關閉 firewall  服務,命令如下:



    停止以後然後執行下面命令禁用


  1. systemctl disable firewalld

  2. systemctl stop firewalld

安裝 iptables


yum install -y iptables-services

出現 complete! 表示安裝成功。

2.JPG

啓動 iptables


systemctl start iptables

3.JPG

查看 iptables 默認規則


iptables -L

4.JPG

在默認規則下,INTPUT 鏈中 ACCEPT 來自任何主機的訪問。 

需要對規則進行修改,步驟如下:

  • 如之前已經設置過規則策略的,將原有的 iptables 文件保存一份,,避免之前設置的策略丟失,命令如下;


     

  1. cp -a /etc/sysconfig/iptables /etc/sysconfig/iptables.bak

  • 清空服務器上所有的規則


    1. iptables -F

  • 設置 INPUT 方向所有的請求都拒絕,這條策略加上以後所有訪問服務器的請求全都會被拒絕掉,如果是線上業務請勿直接操作,會導致業務直接中斷:


    1. iptables -P INPUT DROP

  • 放行系統常用的端口,比如 80 和 22 端口,實際的業務使用端口可以根據自己的需求來加,更多的協議放行可以自行再研究:


    1. iptables -I INPUT -p tcp dport 80 -m state state NEW -j ACCEPT


    1. iptables -I INPUT -p tcp dport 22 -m state state NEW -j ACCEPT

  • 然後使用 iptables -L 查看一下添加的規則是否生效


    5.JPG
     

    1. iptables -L

  • 保存添加的規則


     

    1. iptables-save > /etc/sysconfig/iptables

    設置 iptables 開機啓動


    systemctl enable iptables.service

    操作完成後,重啓服務器進行配置驗證:

    systemctl reboot


    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章