• iptables服务简单使用


    安装服务

    查看是否安装服务:systemctl status iptables

    # 安装服务
    sudo yum install iptables -y
    sudo yum install iptables-services -y
    
    # 启动服务
    sudo service iptables start
    sudo service iptables status
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    启动后iptables文件内容如下

    # sample configuration for iptables service
    # you can edit this manually or use system-config-firewall
    # please do not ask us to add additional ports/services to this default configuration
    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
    -A INPUT -p icmp -j ACCEPT
    -A INPUT -i lo -j ACCEPT
    -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
    -A INPUT -j REJECT --reject-with icmp-host-prohibited
    -A FORWARD -j REJECT --reject-with icmp-host-prohibited
    COMMIT
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    1、编辑iptables文件

    以配置9999端口访问白名单为例sudo vi /etc/sysconfig/iptables

    *filter
    :INPUT ACCEPT [0:0]
    :FORWARD ACCEPT [0:0]
    :OUTPUT ACCEPT [0:0]
    -N whitelist 创建白名单whitelist 
    -A whitelist -s 10.7.48.46 -j ACCEPT 白名单whitelist 中添加客户端地址,实行规则为ACCEPT放行
    -A whitelist -s 10.7.20.139 -j ACCEPT 白名单whitelist 中添加客户端地址,实行规则为ACCEPT放行
    -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 输入过滤中状态已连接的实行规则为ACCEPT放行
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 9999 -j whitelist 输入过滤中状态为新建连接,协议为tcp,端口为9999,实行规则为白名单whitelist中配置 。
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 9999 -j DROP 输入过滤中状态为新建连接,协议为tcp,端口为9999的规则为拒绝。由于规则配置顺序为从上至下执行,满足即进入FORWARD过滤。因此在白名单中的地址只会执行到上一行,不会执行到这一步。即只有不在白名单中的客户端执行这一步。规则为删除输入。
    -A INPUT -j ACCEPT 其他地址的所有端口一律放行
    COMMIT 提交
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、保存配置-重启服务

    sudo iptables-save
    sudo service iptables restart
    sudo service iptables status
    sudo service iptables stop
    
    • 1
    • 2
    • 3
    • 4

    小案例:只允许特定ip访问80端口

    iptables -I INPUT -p TCP --dport 80 -j DROP
    iptables -I INPUT -s 10.28.143.18 -p TCP --dport 80 -j ACCEPT
    iptables -I INPUT -s 10.28.186.173 -p TCP --dport 80 -j ACCEPT
    iptables -I INPUT -s 10.28.139.123 -p TCP --dport 80 -j ACCEPT
    iptables -I INPUT -s 10.28.186.166 -p TCP --dport 80 -j ACCEPT
    iptables -I INPUT -s 10.28.143.7 -p TCP --dport 80 -j ACCEPT
    iptables --line -nvL INPUT
    service iptables save
    sudo service iptables restart
    sudo service iptables status
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    【物理应用】GPS信号捕获跟踪附MATLAB代码
    nRF5340(入门篇)之1.1 nrf5340芯片简介
    未来世界:16项改变人类社会的新技术
    ruoyi实现富文本生成网页功能,vue实现wangEditro功能,网页静态化,二维码预览网页
    RSE2021/云检测:基于小波变换和连续多尺度空间注意的上下块深度网络云检测
    2分钟快速批量部署node_exporter
    spring security 使用示例
    一个域名可以对应多个IP吗?如何通过DNS实现?
    记一堂公开课《前端架构的设计与进化》思考总结
    基于Unity3D实现的HitUFO鼠标打飞碟游戏
  • 原文地址:https://blog.csdn.net/wsp_1138886114/article/details/125674383