• 转载 阿里云centOS防火墙配置


    虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT。OUTPUT和FORWORD都是ACCEPT的规则

    一、检查iptables服务状态

    首先检查iptables服务的状态

    1
    2
    [root@woxplife ~]# service iptables status
    iptables: Firewall is not running.

    说明iptables服务是有安装的,但是没有启动服务。
    如果没有安装的话可以直接yum安装

    1
    yum install -y iptables

    启动iptables

    1
    2
    [root@woxplife ~]# service iptables start
    iptables: Applying firewall rules:                         [  OK  ]

    看一下当前iptables的配置情况

    1
    [root@woxplife ~]# iptables -L -n

    二、清除默认的防火墙规则

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #首先在清除前要将policy INPUT改成ACCEPT,表示接受一切请求。
    #这个一定要先做,不然清空后可能会悲剧
    iptables -P INPUT ACCEPT
     
    #清空默认所有规则
    iptables -F
     
    #清空自定义的所有规则
    iptables -X
     
    #计数器置0
    iptables -Z

    三、配置规则

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    #允许来自于lo接口的数据包
    #如果没有此规则,你将不能通过127.0.0.1访问本地服务,例如ping 127.0.0.1
    iptables -A INPUT -i lo -j ACCEPT
     
    #ssh端口22
    iptables -A INPUT -p tcp --dport 22 -j ACCEPT
     
    #FTP端口21
    iptables -A INPUT -p tcp --dport 21 -j ACCEPT
     
    #web服务端口80
    iptables -A INPUT -p tcp --dport 80 -j ACCEPT
     
    #tomcat
    iptables -A INPUT -p tcp --dport xxxx -j ACCEPT
     
    #mysql
    iptables -A INPUT -p tcp --dport xxxx -j ACCEPT
     
    #允许icmp包通过,也就是允许ping
    iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
     
    #允许所有对外请求的返回包
    #本机对外请求相当于OUTPUT,对于返回数据包必须接收啊,这相当于INPUT了
    iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
     
    #如果要添加内网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

    四、保存

    首先iptables -L -n看一下配置是否正确。
    没问题后,先不要急着保存,因为没保存只是当前有效,重启后就不生效,这样万一有什么问题,可以后台强制重启服务器恢复设置。
    另外开一个ssh连接,确保可以登陆。

    确保没问题之后保存

    1
    2
    3
    4
    5
    #保存
    [root@woxplife ~]# service iptables save
     
    #添加到自启动chkconfig
    [root@woxplife ~]# chkconfig iptables on

    修改防火墙端口:修改/etc/sysconfig/iptables 文件

  • 相关阅读:
    操作系统的概念
    流量监听的基础利用
    后门的学习与清理
    关于phpstudy打开phpmyadmin无法打开
    SSRF服务器请求伪造
    javascript基础流程控制
    Repository中自己写修改、删除语句是需要添加的注解
    SpringBoot链接数据库
    SpringBoot项目实战:企业项目管理系统
    ErrorUtils
  • 原文地址:https://www.cnblogs.com/ggds/p/8649438.html
Copyright © 2020-2023  润新知