我正在尝试在我的 Ubuntu 12.04 LTS 服务器上配置 iptables 以将端口 443 转发到 8443。
但是当我运行这个命令时:
sudo iptables -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
我收到以下错误:
iptables: No chain/target/match by that name.
我的 iptables 当前配置:
$ sudo iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp dpt:https
DROP tcp -- anywhere anywhere tcp dpt:http
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
我遗漏了什么或者做错了什么?
答案1
因为PREROUTING
链属于NAT
表,而不是FILTER
表本身。如果您没有通过-t
选项明确提及任何表,则FILTER
假定为。
因此,您需要提及表类型-t nat
:
sudo iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
请注意,MANGLE
和RAW
表也有PREROUTING
链,但由于您仅重定向端口,因此您大概正在寻找NAT
表。
答案2
PREROUTING 链仅适用于 nat、mangle 和 raw 表。iptables
假定过滤表,因此您必须指定其中一个,例如。iptables -t nat ...
答案3
当我运行 docker 命令时,我收到类似的错误
docker run -d -p 8084:8080 knockdata/zeppelin-highcharts
d9c5d34f500d621585470b0e70b915395fcb6b3437859e0f610dbb58d51faf25
docker: Error response from daemon: driver failed programming external connectivity on endpoint elegant_jang
(7ca0f5ad689f5443ce7533f66b4a86c34d2dbd9d076bac4812288dd3f6a76698):
iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8084 -j DNAT --to-destination 172.17.0.2:8080
! -i docker0: iptables: No chain/target/match by that name.
(exit status 1).
我可以通过重新安装 docker-engine 来修复它
apt-get remove docker-engine
apt-get install docker-engine
答案4
您可以安装(配置服务器安全和防火墙)并使用以下设置。
nano /etc/csf/csf.conf
SYNFLOOD = "" => SYNFLOOD = "1"
CONNLIMIT = "" => CONNLIMIT = "80;75,443;75,21;50”
PORTFLOOD = "" => PORTFLOOD = "80;tcp;5;250"
SYSLOG = “0” => SYSLOG = "1"
DOCKER = “0” => DOCKER = "1"
nano /etc/csf/csfpost.sh
#!/bin/sh
echo "[DOCKER] Setting up FW rules."
iptables -N DOCKER
iptables -t nat -N DOCKER
iptables -t nat -A PREROUTING -m addrtype --dst-type LOCAL -j DOCKER
iptables -t nat -A OUTPUT ! -d 127.0.0.0/8 -m addrtype --dst-type LOCAL -j DOCKER
# Masquerade outbound connections from containers
iptables -t nat -A POSTROUTING -s 172.17.0.0/16 ! -o docker0 -j MASQUERADE
# Accept established connections to the docker containers
iptables -t filter -N DOCKER
iptables -t filter -A FORWARD -o docker0 -j DOCKER
iptables -t filter -A FORWARD -o docker0 -m conntrack --ctstate RELATED,ESTABLISHED -j
ACCEPT
# Allow docker containers to communicate with themselves & outside world
iptables -t filter -A FORWARD -i docker0 ! -o docker0 -j ACCEPT
iptables -t filter -A FORWARD -i docker0 -o docker0 -j ACCEPT
echo "[DOCKER] Done."
注意:此配置还可以防止您遭受基本的 DDOS 攻击。