Overfill3641
V2EX  ›  问与答

iptables 怎么只允许 IP 段列表访问某个端口

  •  
  •   Overfill3641 · Mar 23, 2021 · 1884 views
    This topic created in 1889 days ago, the information mentioned may be changed or developed.
    4 replies    2021-03-23 22:51:32 +08:00
    admonition
        1
    admonition  
       Mar 23, 2021 via Android
    taolu
        2
    taolu  
       Mar 23, 2021
    fxplay
        3
    fxplay  
       Mar 23, 2021   ❤️ 1
    ip 端口白名单嘛。。。
    centos 6
    编辑 iptables 文件 vim /etc/sysconfig/iptables

    -N whitelist
    -A whitelist -s 192.168.1.171 -j ACCEPT
    -A whitelist -s 192.168.1.244 -j ACCEPT
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j whitelist
    -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
    注: 如上 6379 只能是白名单的两个 IP 地址访问,22 可以任意 IP 访问

    执行命令重启防火墙生效,service iptables restart
    indev
        4
    indev  
       Mar 23, 2021   ❤️ 1
    先允许,再阻止其他的,举个栗子:

    sudo iptables -A INPUT -p tcp -s 123.123.0.0/16 --dport 22 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 22 -j DROP

    -p 后也可以指定 udp,-s 后可以是单个 IP 或 IP 段,--dport 就不用说了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3630 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 04:49 · PVG 12:49 · LAX 21:49 · JFK 00:49
    ♥ Do have faith in what you're doing.