多年来,我一直使用bash脚本来设置复杂的iptables
规则。然而,在Debian Stretch
上,当我尝试使用一个脚本时,它变得迟钝,使iptables处于糟糕的状态。
当我尝试做一个iptables -L -v
时,它又犯了一个错误.
Another app is currently holding the xtables lock; still -9s 0us time ahead to have a chance to grab the lock...
谷歌把我带到了这只虫子,它建议我使用"-w“开关。手册页并没有真正清除这个开关对问题的影响。
为了便于管理,我的脚本使用了一个循环,这使得它对iptables进行了大量的调用。
# This actually sets the allowed incoming iptables lines
setincoming() {
for port in ${2}; do
for ip in ${1}; do
if [ `echo "$ip" | grep -P "(^|\s)[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]*)*(\s*)$"` ]; then
iptables -I INPUT -p tcp -s $ip --dport $port -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
else
ip6tables -I INPUT -p tcp -s $ip --dport $port -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
fi
done
done
}
#####
# APIs
setincoming "123.123.123.10 123.123.123.11 fe80::xxxx:xxx:xxxx:xxxx" "4200 4300"
Can任何人帮助我理解如何使用"-w“来解决这个问题?
编辑:为了澄清起见,我当然看了手册页--并尝试使用开关-w
并将其合并为-w -W1
,但这对问题没有任何影响(既没有修复它,也没有改变症状)。
所以,我仍然不知道该如何解决。
发布于 2018-10-12 17:22:08
-w
选项只会导致iptable等待xtable锁,而不是在初始失败时退出。该选项可以在多个进程竞相获取和释放xtable锁的竞争条件下工作。如果另一个进程没有释放锁,或者有太多进程在争夺锁,则此选项可能没有帮助。
正如Tomek建议的那样,我将修改您的脚本以利用ipset。它肯定会更优化,因为它依赖一个哈希表,而不是顺序地遍历您的iptables规则。它也可能解决您的锁定问题。与…有关的东西:
# Create ipset and connect it with iptables
ipset create foo hash:ip,port
iptables -I INPUT -m set --match-set foo src -j ACCEPT
# Add allowances as needed
ipset add foo 123.123.123.10,4200
ipset add foo 123.123.123.11,tcp:4300 # Specify protocol
ipset add foo 123.123.123.12,4400 timeout 600 # Set a timeout for this to disappear
ipset add foo 123.123.123.13,4500 -exist # Do not error if this already exists
由于您在示例中同时包含了IPv4和IPv6,所以我还要提到,在创建ipset以支持IPv6时,您可以使用family
参数。
https://serverfault.com/questions/935272
复制相似问题