我需要将某些端口临时重定向到另一台计算机。我试过了:
iptables -t nat -A PREROUTING -p tcp --dport 4321 -j DNAT --to-destination 192.168.121.4:80
但海岸墙阻止了所有包裹。所以我尝试过:
iptables -A FORWARD -d 192.168.121.4 -p tcp --dport 80 -j ACCEPT
这也不起作用。那么如何将端口 4321 重定向/转发到计算机 192.168.121.4 上的端口 80 呢?
之后我怎样才能取消重定向/取消转发呢?我还设置了:
sysctl net.ipv4.ip_forward=1
我使用的是 Linux/Ubuntu 12.04
iptables-保存:
# Generated by iptables-save v1.4.12 on Mon Jun 23 12:53:49 2014
*raw
:PREROUTING ACCEPT [5287371:7459454675]
:OUTPUT ACCEPT [4550885:6177804756]
COMMIT
# Completed on Mon Jun 23 12:53:49 2014
# Generated by iptables-save v1.4.12 on Mon Jun 23 12:53:49 2014
*nat
:PREROUTING ACCEPT [227:17856]
:INPUT ACCEPT [33:2188]
:OUTPUT ACCEPT [53:3832]
:POSTROUTING ACCEPT [53:3832]
:eth0_masq - [0:0]
-A PREROUTING -p tcp -m tcp --dport 4321 -j DNAT --to-destination 192.168.121.4:80
-A PREROUTING -p tcp -m tcp --dport 4321 -j DNAT --to-destination 192.168.121.4:80
-A POSTROUTING -o eth0 -j eth0_masq
-A eth0_masq -s 169.254.0.0/16 -j MASQUERADE
-A eth0_masq -s 192.168.121.0/24 -j MASQUERADE
COMMIT
# Completed on Mon Jun 23 12:53:49 2014
# Generated by iptables-save v1.4.12 on Mon Jun 23 12:53:49 2014
*mangle
:PREROUTING ACCEPT [5287371:7459454675]
:INPUT ACCEPT [5081977:7360915410]
:FORWARD ACCEPT [205394:98539265]
:OUTPUT ACCEPT [4550885:6177804756]
:POSTROUTING ACCEPT [4756960:6276393466]
:tcfor - [0:0]
:tcin - [0:0]
:tcout - [0:0]
:tcpost - [0:0]
:tcpre - [0:0]
-A PREROUTING -j tcpre
-A INPUT -j tcin
-A FORWARD -j MARK --set-xmark 0x0/0xff
-A FORWARD -j tcfor
-A OUTPUT -j tcout
-A POSTROUTING -j tcpost
COMMIT
# Completed on Mon Jun 23 12:53:49 2014
# Generated by iptables-save v1.4.12 on Mon Jun 23 12:53:49 2014
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT DROP [0:0]
:%Invalid - [0:0]
:Broadcast - [0:0]
:Drop - [0:0]
:Invalid - [0:0]
:NotSyn - [0:0]
:Reject - [0:0]
:dynamic - [0:0]
:eth0_fwd - [0:0]
:eth0_in - [0:0]
:fw2net - [0:0]
:fw2sky - [0:0]
:logdrop - [0:0]
:logflags - [0:0]
:logreject - [0:0]
:net2fw - [0:0]
:net2sky - [0:0]
:net_frwd - [0:0]
:reject - [0:0]
:shorewall - [0:0]
:sky2fw - [0:0]
:sky2net - [0:0]
:smurflog - [0:0]
:smurfs - [0:0]
:tcpflags - [0:0]
:wlan0_fwd - [0:0]
:wlan0_in - [0:0]
-A INPUT -m conntrack --ctstate INVALID,NEW -j dynamic
-A INPUT -i eth0 -j eth0_in
-A INPUT -i wlan0 -j wlan0_in
-A INPUT -i eth1 -j sky2fw
-A INPUT -i lo -j ACCEPT
-A INPUT -j Reject
-A INPUT -j LOG --log-prefix "Shorewall:INPUT:REJECT:" --log-level 6
-A INPUT -g reject
-A FORWARD -i eth0 -j eth0_fwd
-A FORWARD -i wlan0 -j wlan0_fwd
-A FORWARD -i eth1 -o wlan0 -j sky2net
-A FORWARD -i eth1 -o eth0 -j sky2net
-A FORWARD -j Reject
-A FORWARD -j LOG --log-prefix "Shorewall:FORWARD:REJECT:" --log-level 6
-A FORWARD -g reject
-A FORWARD -d 192.168.121.4/32 -p tcp -m tcp --dport 4242 -j ACCEPT
-A FORWARD -d 192.168.121.4/32 -p tcp -m tcp --dport 80 -j ACCEPT
-A OUTPUT -o eth0 -j fw2net
-A OUTPUT -o wlan0 -j fw2net
-A OUTPUT -o eth1 -j fw2sky
-A OUTPUT -o lo -j ACCEPT
-A OUTPUT -j ACCEPT
-A %Invalid -m conntrack --ctstate INVALID -j DROP
-A Broadcast -m addrtype --dst-type BROADCAST -j DROP
-A Broadcast -m addrtype --dst-type MULTICAST -j DROP
-A Broadcast -m addrtype --dst-type ANYCAST -j DROP
-A Broadcast -d 224.0.0.0/4 -j DROP
-A Drop
-A Drop -p tcp -m tcp --dport 113 -m comment --comment Auth -j reject
-A Drop -j Broadcast
-A Drop -p icmp -m icmp --icmp-type 3/4 -m comment --comment "Needed ICMP types" -j ACCEPT
-A Drop -p icmp -m icmp --icmp-type 11 -m comment --comment "Needed ICMP types" -j ACCEPT
-A Drop -j Invalid
-A Drop -p udp -m multiport --dports 135,445 -m comment --comment SMB -j DROP
-A Drop -p udp -m udp --dport 137:139 -m comment --comment SMB -j DROP
-A Drop -p udp -m udp --sport 137 --dport 1024:65535 -m comment --comment SMB -j DROP
-A Drop -p tcp -m multiport --dports 135,139,445 -m comment --comment SMB -j DROP
-A Drop -p udp -m udp --dport 1900 -m comment --comment UPnP -j DROP
-A Drop -p tcp -j NotSyn
-A Drop -p udp -m udp --sport 53 -m comment --comment "Late DNS Replies" -j DROP
-A Invalid -m conntrack --ctstate INVALID -j DROP
-A NotSyn -p tcp -m tcp ! --tcp-flags FIN,SYN,RST,ACK SYN -j DROP
-A Reject
-A Reject -p tcp -m tcp --dport 113 -m comment --comment Auth -j reject
-A Reject -j Broadcast
-A Reject -p icmp -m icmp --icmp-type 3/4 -m comment --comment "Needed ICMP types" -j ACCEPT
-A Reject -p icmp -m icmp --icmp-type 11 -m comment --comment "Needed ICMP types" -j ACCEPT
-A Reject -j Invalid
-A Reject -p udp -m multiport --dports 135,445 -m comment --comment SMB -j reject
-A Reject -p udp -m udp --dport 137:139 -m comment --comment SMB -j reject
-A Reject -p udp -m udp --sport 137 --dport 1024:65535 -m comment --comment SMB -j reject
-A Reject -p tcp -m multiport --dports 135,139,445 -m comment --comment SMB -j reject
-A Reject -p udp -m udp --dport 1900 -m comment --comment UPnP -j DROP
-A Reject -p tcp -j NotSyn
-A Reject -p udp -m udp --sport 53 -m comment --comment "Late DNS Replies" -j DROP
-A eth0_fwd -m conntrack --ctstate INVALID,NEW -j dynamic
-A eth0_fwd -m conntrack --ctstate INVALID,NEW -j smurfs
-A eth0_fwd -p tcp -j tcpflags
-A eth0_fwd -j net_frwd
-A eth0_in -m conntrack --ctstate INVALID,NEW -j dynamic
-A eth0_in -m conntrack --ctstate INVALID,NEW -j smurfs
-A eth0_in -p udp -m udp --dport 67:68 -j ACCEPT
-A eth0_in -p tcp -j tcpflags
-A eth0_in -j net2fw
-A fw2net -p udp -m udp --dport 67:68 -j ACCEPT
-A fw2net -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A fw2net -p udp -m udp --dport 53 -m comment --comment DNS -j ACCEPT
-A fw2net -p tcp -m tcp --dport 53 -m comment --comment DNS -j ACCEPT
-A fw2net -p icmp -j ACCEPT
-A fw2net -j ACCEPT
-A fw2sky -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A fw2sky -j ACCEPT
-A logdrop -j DROP
-A logflags -j LOG --log-prefix "Shorewall:logflags:DROP:" --log-level 6 --log-ip-options
-A logflags -j DROP
-A logreject -j reject
-A net2fw -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A net2fw -j %Invalid
-A net2fw -p tcp -m tcp --dport 22 -m comment --comment SSH -j ACCEPT
-A net2fw -p tcp -m multiport --dports 4242,4243,24800 -j ACCEPT
-A net2fw -p icmp -m icmp --icmp-type 8 -m comment --comment Ping -j ACCEPT
-A net2fw -p icmp -m icmp --icmp-type 8 -m comment --comment Ping -j DROP
-A net2fw -p tcp -m tcp --dport 80 -j ACCEPT
-A net2fw -j Drop
-A net2fw -j LOG --log-prefix "Shorewall:net2fw:DROP:" --log-level 6
-A net2fw -j DROP
-A net2sky -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A net2sky -j %Invalid
-A net2sky -j Drop
-A net2sky -j LOG --log-prefix "Shorewall:net2sky:DROP:" --log-level 6
-A net2sky -j DROP
-A net_frwd -o eth0 -j ACCEPT
-A net_frwd -o wlan0 -j ACCEPT
-A net_frwd -o eth1 -j net2sky
-A reject -m addrtype --src-type BROADCAST -j DROP
-A reject -s 224.0.0.0/4 -j DROP
-A reject -p igmp -j DROP
-A reject -p tcp -j REJECT --reject-with tcp-reset
-A reject -p udp -j REJECT --reject-with icmp-port-unreachable
-A reject -p icmp -j REJECT --reject-with icmp-host-unreachable
-A reject -j REJECT --reject-with icmp-host-prohibited
-A sky2fw -m conntrack --ctstate INVALID,NEW -j dynamic
-A sky2fw -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A sky2fw -j ACCEPT
-A sky2net -m conntrack --ctstate INVALID,NEW -j dynamic
-A sky2net -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A sky2net -j ACCEPT
-A smurflog -j LOG --log-prefix "Shorewall:smurfs:DROP:" --log-level 6
-A smurflog -j DROP
-A smurfs -s 0.0.0.0/32 -j RETURN
-A smurfs -m addrtype --src-type BROADCAST -g smurflog
-A smurfs -s 224.0.0.0/4 -g smurflog
-A tcpflags -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG FIN,PSH,URG -g logflags
-A tcpflags -p tcp -m tcp --tcp-flags FIN,SYN,RST,PSH,ACK,URG NONE -g logflags
-A tcpflags -p tcp -m tcp --tcp-flags SYN,RST SYN,RST -g logflags
-A tcpflags -p tcp -m tcp --tcp-flags FIN,SYN FIN,SYN -g logflags
-A tcpflags -p tcp -m tcp --sport 0 --tcp-flags FIN,SYN,RST,ACK SYN -g logflags
-A wlan0_fwd -m conntrack --ctstate INVALID,NEW -j dynamic
-A wlan0_fwd -m conntrack --ctstate INVALID,NEW -j smurfs
-A wlan0_fwd -p tcp -j tcpflags
-A wlan0_fwd -j net_frwd
-A wlan0_in -m conntrack --ctstate INVALID,NEW -j dynamic
-A wlan0_in -m conntrack --ctstate INVALID,NEW -j smurfs
-A wlan0_in -p udp -m udp --dport 67:68 -j ACCEPT
-A wlan0_in -p tcp -j tcpflags
-A wlan0_in -j net2fw
COMMIT
# Completed on Mon Jun 23 12:53:49 2014
答案1
你至少需要
iptables -I FORWARD 0 -d 192.168.77.4 -p tcp --dport 80 -j ACCEPT
您可以-I FORWARD 0
在 shorewall 防火墙规则之前插入自己的规则,这些规则在 FORWARD 链中具有通用拒绝规则。
另外,目标端口被重写前FORWARD 链已处理。
NAT 系统会自动处理回传流量。
更好的方法是配置 shorewall,使其自动包含适当的 FORWARD 规则。
答案2
现在我意识到这并不完全是您想要的解决方案,但我想向您和未来有类似问题的人指出rinetd(8)
,TCP 流量的“重定向服务器”。
请注意,这不需要任何额外的转发/路由或 DNAT(nat
表中的iptables
)魔法,前提是允许转发到服务器和端口的出站流量以及rinetd
侦听端口的入站流量(转发流量)也是允许的。
这只适用于 TCP。但这是一个轻量级的解决方案,帮助我在预算有限的情况下迁移服务器,而没有任何明显的停机时间(是的,我知道还有其他更无缝的解决方案可以满足更多的需求)。
答案3
您可以使用 rein 将 TCP 连接从一个 IP 地址和端口重定向到另一个 IP 地址和端口。非常直观,使用简单。
https://github.com/firstboot/rein
linux:
cd ~
wget http://note.youdao.com/yws/public/resource/b1e1ad270ba1b1af97ebdf3e2c8b7403/xmlnote/82E2CC3FF2744238B6AF36346298E5E5/27082 -O rein.zip
unzip rein.zip
mv rein-*-linux rein
chmod +x rein
./rein -e > rein.json
# modify rein.json for you
./rein -c rein.json
视窗:
# unzip package, and rename
ren rein-1.0.3-amd64-win.exe rein.exe
# generate default conf 'rein.json'
# windows cmd
./rein.exe -e > rein.json
# windows powershell
./rein.exe -e | out-file -encoding ascii rein.json