我想将 postgres 的端口重定向到另一个正在运行 postgres 的地址。postgres 的实例托管在具有仅主机网络配置的 virtualbox 机器上。不幸的是,以下规则似乎无法胜任
echo "rdr pass inet proto tcp from any to 127.0.0.1 port 5432 ->
172.16.0.2 port 5432" | sudo pfctl -ef -
pfctl: Use of -f option, could result in flushing of rules
present in the main ruleset added by the system at startup.
See /etc/pf.conf for further details.
No ALTQ support in kernel
ALTQ related functions disabled
pfctl: pf already enabled
$ psql -h 127.0.0.1 -p 5432 -U em3local
psql: could not connect to server: Network is unreachable
它出什么问题了?
当然,如果我尝试连接到 172.16.0.2 我可以正确访问它:
$psql -h 172.16.0.2 -p 5432 -U em3local
Password for user em3local:
这些是我当前的接口(注意最后一个vboxnet0
):
$ ifconfig
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128
inet 127.0.0.1 netmask 0xff000000
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1
inet 172.16.0.9 netmask 0xffff0000
nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
ether 38:c9:86:4f:bd:74
inet6 fe80::3ac9:86ff:fe4f:bd74%en0 prefixlen 64 scopeid 0x4
inet 10.0.0.2 netmask 0xffff0000 broadcast 10.0.255.255
nd6 options=1<PERFORMNUD>
media: autoselect (1000baseT <full-duplex,flow-control,energy-efficient-ethernet>)
status: active
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 08:6d:41:e9:79:7e
inet6 fe80::a6d:41ff:fee9:797e%en1 prefixlen 64 scopeid 0x5
inet 10.0.1.2 netmask 0xffff0000 broadcast 10.0.255.255
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
en2: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500
options=60<TSO4,TSO6>
ether 2a:00:01:66:53:30
media: autoselect <full-duplex>
status: inactive
en3: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500
options=60<TSO4,TSO6>
ether 2a:00:01:66:53:31
media: autoselect <full-duplex>
status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 0a:6d:41:e9:79:7e
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether 4a:00:05:4b:ef:d9
inet6 fe80::4800:5ff:fe4b:efd9%awdl0 prefixlen 64 scopeid 0x9
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether 3a:c9:86:f4:af:00
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en2 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 6 priority 0 path cost 0
member: en3 flags=3<LEARNING,DISCOVER>
ifmaxaddr 0 port 7 priority 0 path cost 0
nd6 options=1<PERFORMNUD>
media: <unknown type>
status: inactive
ppp0: flags=8051<UP,POINTOPOINT,RUNNING,MULTICAST> mtu 1280
inet 10.0.0.2 --> 10.0.2.3 netmask 0xffff0000
vboxnet0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1500
ether 0a:00:27:00:00:00
inet 172.16.0.1 netmask 0xfff00000 broadcast 172.31.255.255
当您将端口从环回设备转发到其他主机(虚拟主机)时,转发数据包的源地址不会更改。但它具有相同的环回 IP 地址
127.0.0.1
。当其他主机(虚拟主机)收到转发的数据包时,它会回复自己的环回设备。这就是你没有连接的原因。在您的主机服务器上使用其他 ip(不是来自环回的 127.0.0.0/8 网络)进行端口转发。