白名单允许使用iptables的IP(input/输出)

我有几个IP范围,我希望我的服务器能够连接和用户连接。 其他一切都应该被阻止。

我应该如何做到这一点与iptables

我的操作系统是基于Debian的Linux发行版。

    我build议抓住一个防火墙configuration工具,如Firestarter ,并从那里。 不过,这里有一些基本的东西。

    #Flush existing rules iptables -F # Set up default DROP rule for eth0 iptables -P INPUT DROP # Allow existing connections to continue iptables -A INPUT -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT # Accept everything from the 192.168.1.x network iptables -A INPUT -i eth0 -s 192.168.1.0/24 -j ACCEPT # Allow connections from this host to 192.168.2.10 iptables -A OUTPUT -o eth0 -d 192.168.2.10 -j ACCEPT 
     iptables -I INPUT -s <allowed_ip> -j ACCEPT #(repeat this line as needed) iptables -P INPUT DROP 

    这将使您的系统变成一个不存在的系统,用于不允许的电脑。

    如果你想允许任意范围而不是整个子网,你可以使用'iprange'iptables模块:

    iptables -P INPUT DROP

    iptables -A INPUT -m iprange --src-range 192.168.1.30-50 -j ACCEPT

    例如,将允许来自所有机器的stream量在地址192.168.1.30和192.168.1.50之间。

    如果你想允许传入和传出的stream量到相同的IP范围,我build议你创build一个特定的链,允许这些IPs和所有的input和输出目标为目标:

    – 定义默认的策略,以放弃永恒:

    iptables -P INPUT DROP

    iptables -P OUTPUT DROP

    – 创build新的链条:

    iptables -N allowed_ips

    – 如果源是允许范围的一部分,则接受

    iptables -A allowed_ips -m iprange --src-range 192.168.1.30-50 -j ACCEPT

    – 如果没有,返回到调用者链接继续处理

    iptables -A allowed_ips -j RETURN

    – 使进出机器的所有stream量都通过我们的新链条

    iptables -A INPUT -j allowed_ips

    iptables -A OUTPUT -j allowed_ips

    就是这样! 当然你可能需要一些辅助规则,比如允许所有来自/接口的stream量等等。

    一旦你满意你的规则, 你可能想要保存它们 。 在这个链接的评论有几个选项如何做到这一点。

    一个易于使用的iptables规则发生器为简单的需要是ufw 。 该软件包在debian unstable中可用。

    也试试Firestarter 。 在lenny中可用。

    您也可以使用我过去一年也使用的ferm ,并帮助我处理有条件的防火墙规则等情况。