Fra*_*ter 24 linux iptables blacklist
我有几个 ip 范围,我希望我的服务器能够连接和用户连接。其他一切都应该被阻止。
我应该如何使用 iptables 做到这一点?
我的操作系统是基于 Debian 的 linux 发行版。
Zen*_*ham 30
我建议使用防火墙配置工具,例如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
Run Code Online (Sandbox Code Playgroud)
Kev*_*n M 17
iptables -I INPUT -s <allowed_ip> -j ACCEPT #(repeat this line as needed)
iptables -P INPUT DROP
Run Code Online (Sandbox Code Playgroud)
这会将您的系统变成一个不存在的系统,供不允许使用的计算机使用。
小智 7
如果你想允许任意范围而不是整个子网,你可以使用 'iprange' iptables 模块:
iptables -P INPUT DROP
iptables -A INPUT -m iprange --src-range 192.168.1.30-50 -j ACCEPT
例如,将允许来自地址在 192.168.1.30 和 192.168.1.50 之间的所有机器的流量。
如果您想允许传入和传出流量到达相同的 IP 范围,我建议您创建一个特定的链,允许该 IP 并将所有输入和输出目标定位到它:
--定义删除所有内容的默认策略:
iptables -P INPUT DROP
iptables -P OUTPUT DROP
--创建新链:
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
--让所有进出机器的流量都经过我们的新链
iptables -A INPUT -j allowed_ips
iptables -A OUTPUT -j allowed_ips
就是这样!当然,您可能需要其他规则,例如允许所有来自/流向 lo 接口的流量等。