#查看目前端口轉發規則
iptables --line-numbers --list PREROUTING -t nat
#從上面語句輸出的列表中找到自己之前綁定轉發的端口的記錄的行號,然后用下面這句刪掉這條記錄來刪除轉發,而且最蛋疼的是你不能指定轉發規則的特征來刪除記錄,你只能告訴它“刪除第幾行”。
iptables -t nat -D PREROUTING 行號
3.使用ArchLinux的童鞋們需要注意了,可能由于內核缺少模塊(是iptables還是nat來著?)而不能用上述語句來設置綁定轉發端口,需要重新編譯內核或者模塊。
#!/bin/bash
if [ $# = 1 ]; then
if [ $1 -ge 0 ]; then
if [ $1 -le 65535 ]; then
#Delete all old bindings
for line_num in $(iptables --line-numbers --list PREROUTING -t nat|grep dpt:http|awk '{print $1}')
do
LINES="$line_num $LINES"
done
for line in $LINES
do
iptables -t nat -D PREROUTING $line
done
unset LINES
#Make a new binding
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port $1
echo "Port 80 is bound with $1 !"
exit 1
fi
fi
fi
echo "Please input ONE PORT NUMBER!"