大网数据广告活动
服务器运维问答

Is it possible to translate a public IP to private IP using iptables?

· 52CE 我爱测

I a bunch of hosts in the cloud with private and public IPs. Until now, I could access all the hosts publicly and any way I want. Now, I want to disable some access to all of these hosts from the outside and set up a VPN. I have …

问题原文

原帖链接 · Gasim · CC BY-SA 3.0

I a bunch of hosts in the cloud with private and public IPs. Until now, I could access all the hosts publicly and any way I want. Now, I want to disable some access to all of these hosts from the outside and set up a VPN.

I have created a VPN server and everything works fine -- I can see my internal network easily. There is this one problem that I am facing right now. All the hosts are being accessed via public DNS and this DNS have public IPs for all the hosts.

I want a way to translate all the public IPs to private IPs. For example, let's say I have 2 hosts with the following IPs:

Host 1:
Private IP: 10.1.0.5
Public IP: 1.2.3.4

Host 2:
Private IP: 10.1.0.6
Public IP: 5.6.7.8

If I access 5.6.7.8 (1.2.3.4), I want the server to translate it to 10.1.0.6 (10.1.0.5).

Is it possible via iptables? I have tried the following so far but nothing changed:

iptables -t nat -A OUTPUT -d 5.6.7.8 -j DNAT --to-destination 10.1.0.6

已采纳解答

原帖链接 · Aaron · CC BY-SA 3.0

Intercept traffic and route to an IP using iptables

iptables -t nat -I PREROUTING -d 5.6.7.8 -j DNAT --to-destination 10.1.0.6
iptables -t nat -I PREROUTING -d 1.2.3.4 -j DNAT --to-destination 10.1.0.5

iptables -t nat -I POSTROUTING -o eth1 -j MASQUERADE

In your example, you were trying to route the traffic on the OUTPUT rule and it is too late in the routing decision. In this example, I am assuming your outbound interface is eth1. Ensure you have console access to this device in the event you lock yourself out via a misconfiguration or based on a lack of information exchanged between us.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-07-04)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。