我有一个在SLES上运行的postgresql实例。
我想设置它来侦听本地主机,并启用iptables来执行端口转发。
postgresql.conf中:
listen_addresses = 'localhost' port = 5432
pg_hba.conf的:
local all all md5 host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5
iptables(通过iptables -t nat -I PREROUTING -p tcp --dport 5432 -j REDIRECT添加的规则iptables -t nat -I PREROUTING -p tcp --dport 5432 -j REDIRECT ):
Chain PREROUTING (policy ACCEPT 441 packets, 54049 bytes) pkts bytes target prot opt in out source destination 6 420 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:5432
像这样configuration,我无法打开到DB的psql连接。
没有iptables规则,我得到一个Connection refused错误。
随着iptables规则,我得到一个Connection timed out错误。
您可能需要检查filter表。 例如,INPUT的默认策略是什么? 如果CHAIN策略不允许,你有明确的规则允许端口5432。
另外,写出规则可能不够,如你的问题所示。 从man iptables读取这个:
REDIRECT This target is only valid in the nat table, in the PREROUTING and OUTPUT chains, and user-defined chains which are only called from those chains. It redirects the packet to the machine itself by changing the destination IP to the primary address of the incoming interface (locally-generated packets are mapped to the 127.0.0.1 address).
所以,它会将请求redirect到一个不一定是localhost或127.0.0.1的IP地址。 您可能需要在NAT规则中明确添加127.0.0.1 IP地址。 你可以试试:
iptables -t nat -I PREROUTING -p tcp --dport 5432 -j DNAT --to-destination 127.0.0.1