如何使用iptables进行本地端口转发

时间:2015-01-27 11:59:19

标签: iptables

我有一个应用程序(服务器)正在侦听端口8080.我希望能够将端口80转发给它,这样点击http://localhost就会解析我的应用程序(在localhost:8080上)。

这应该针对任何端口映射进行推广(例如80:8080 => P_src:P_target),并使用现代* nix机器的最佳实践(例如Ubuntu)。

N.B。这都是在本地完成的,因此除了localhost之外,不需要接受来自任何人的连接。

1 个答案:

答案 0 :(得分:18)

所以经过大量搜索后,我发现答案使用了iptables,设置了NAT,并使用了内置的PREROUTING和OUTPUT。

首先,您必须启用端口转发:

echo "1" > /proc/sys/net/ipv4/ip_forward

然后,您必须使用自己的${P_src}${P_target}值将以下规则添加到您的iptables NAT表中:

iptables -t nat -A PREROUTING -s 127.0.0.1 -p tcp --dport ${P_src} -j REDIRECT --to ${P_target}`
iptables -t nat -A OUTPUT -s 127.0.0.1 -p tcp --dport ${P_src} -j REDIRECT --to ${P_target}`

如果您要删除规则,则只需为每个规则使用-D开关而不是-A

我为此构建了一个很好的小脚本,可以添加和删除映射。

#!/bin/bash
#
#   API: ./forwardPorts.sh add|rm p1:p1' p2:p2' ...
#
#   Results in the appending (-A) or deleting (-D) of iptable rule pairs that
#   would otherwise facilitate port forwarding.
#
#   E.g
#   sudo iptables -t nat -A PREROUTING -s 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8080
#   sudo iptables -t nat -A OUTPUT -s 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to 8080
#

if [[ $# -lt 2 ]]; then
    echo "forwardPorts takes a state (i.e. add or rm) and any number port mappings (e.g. 80:8080)";
    exit 1;
fi

case $1 in
    add )
        append_or_delete=A;;
    rm )
        append_or_delete=D;;
    * )
        echo "forwardPorts requires a state (i.e. add or rm) as it's first argument";
        exit 1; ;;
esac

shift 1;

# Do a quick check to make sure all mappings are integers
# Many thanks to S.O. for clever string splitting:
# http://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash
for map in "$@"
do
    IFS=: read -a from_to_array <<< "$map"
    if  [[ ! ${from_to_array[0]} =~ ^-?[0-9]+$ ]] || [[ ! ${from_to_array[1]} =~ ^-?[0-9]+$ ]]; then
        echo "forwardPorts port maps must go from an integer, to an integer (e.g. 443:4443)";
        exit 1;
    fi
    mappings[${#mappings[@]}]=${map}
done

# We're shooting for transactional consistency. Only manipulate iptables if all 
# the rules have a chance to succeed.
for map in "${mappings[@]}"
do
    IFS=: read -a from_to_array <<< "$map" 
    from=${from_to_array[0]}
    to=${from_to_array[1]}

    sudo iptables -t nat -$append_or_delete PREROUTING -s 127.0.0.1 -p tcp --dport $from -j REDIRECT --to $to
    sudo iptables -t nat -$append_or_delete OUTPUT -s 127.0.0.1 -p tcp --dport $from -j REDIRECT --to $to
done

exit 0;