iptables-restore 在 COMMIT 行失败,没有详细描述

iptables-restore 在 COMMIT 行失败,没有详细描述

我正在尝试为新服务器设置 iptables(使用 iptables-restore),并查看了来自利诺德,因此基本上使用了它们的 IPv4 和 IPv6 模板。这两个都在 COMMIT 消息中失败,但我猜错误在其他地方。

alessio@scw-272444:~$ sudo iptables-restore < /tmp/v4
iptables-restore: line 37 failed

IPv4:

*filter

# Allow all loopback (lo0) traffic and reject traffic
# to localhost that does not originate from lo0.
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT

# Allow ping and traceroute.
-A INPUT -p icmp --icmp-type 3 -j ACCEPT
-A INPUT -p icmp --icmp-type 8 -j ACCEPT
-A INPUT -p icmp --icmp-type 11 -j ACCEPT

# Allow SSH connections.
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Allow HTTP and HTTPS connections from anywhere
# (the normal ports for web servers).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Accept inbound traffic from established connections.
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Log what was incoming but denied (optional but useful).
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7

# Reject all other inbound.
-A INPUT -j REJECT

# Log any traffic which was sent to you
# for forwarding (optional but useful).
-A FORWARD -m limit --limit 5/min -j LOG --log-prefix "iptables_FORWARD_denied: " --log-level 7

# Reject all traffic forwarding.
-A FORWARD -j REJECT

COMMIT

IPv6:

*filter

# Allow all loopback (lo0) traffic and reject traffic
# to localhost that does not originate from lo0.
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -s ::1/128 -j REJECT

# Allow ICMP
-A INPUT  -p icmpv6 -j ACCEPT

# Allow HTTP and HTTPS connections from anywhere
# (the normal ports for web servers).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT

# Accept inbound traffic from established connections.
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Log what was incoming but denied (optional but useful).
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "ip6tables_INPUT_denied: " --log-level 7

# Reject all other inbound.
-A INPUT -j REJECT

# Log any traffic which was sent to you
# for forwarding (optional but useful).
-A FORWARD -m limit --limit 5/min -j LOG --log-prefix "ip6tables_FORWARD_denied: " --log-level 7

# Reject all traffic forwarding.
-A FORWARD -j REJECT

COMMIT

答案1

COMMIT请在文件后或文件末尾添加新行。这将解决您的问题。新行必须是最后一个字符。

答案2

COMMIT 行失败,因为此行之前可能存在一些错误。请检查配置中的每一行(将其粘贴为 iptables 的参数)。

或者,您可以将 iptables 配置转换为 bash 脚本,然后运行 ​​bash -x 来查看错误配置:


#!/bin/bash
#
# this file is: iptables_v4.sh

# Allow all loopback (lo0) traffic and reject traffic
# to localhost that does not originate from lo0.
iptables -A  INPUT -i lo -j ACCEPT
iptables -A  INPUT ! -i lo -s 127.0.0.0/8 -j REJECT

# Allow ping and traceroute.
iptables -A  INPUT -p icmp --icmp-type 3 -j ACCEPT
iptables -A  INPUT -p icmp --icmp-type 8 -j ACCEPT
iptables -A  INPUT -p icmp --icmp-type 11 -j ACCEPT

# Allow SSH connections.
iptables -A  INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT

# Allow HTTP and HTTPS connections from anywhere
# (the normal ports for web servers).
iptables -A  INPUT -p tcp --dport 80 -j ACCEPT
iptables -A  INPUT -p tcp --dport 443 -j ACCEPT

# Accept inbound traffic from established connections.
iptables -A  INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Log what was incoming but denied (optional but useful).
iptables -A  INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7

# Reject all other inbound.
iptables -A  INPUT -j REJECT

# Log any traffic which was sent to you
# for forwarding (optional but useful).
iptables -A  FORWARD -m limit --limit 5/min -j LOG --log-prefix "iptables_FORWARD_denied: " --log-level 7

# Reject all traffic forwarding.
iptables -A  FORWARD -j REJECT

调试:

root$bash -x iptables_v4.sh

相关内容