2017年9月19日 星期二

用 iptables 在特定 port 做跳板(forwarding)

因為固定public IP 有限,
有時為了安全性,限制服務只開放給特定ip,
為了要給區網內其他電腦也可以直接使用這個服務,
透過區網內有固定 public IP 的主機,
連到該主機特定port 的資料會直接轉給提供服務的電腦,
從提供服務的電腦看就會是從固定 public IP 主機傳出的資料。




先定義電腦主機:
A:提供服務的電腦 (111.222.123.123) port:22222
B:區網內有對外 public IP主機 (區網IP:192.168.0.123 對外public IP:1.2.3.4 )
C:區網內無對外IP主機: 192.168.0.12

要將區網內連到 B 的 port:3333 資料轉傳到 A的 port: 22222

iptables -t nat -A PREROUTING -p tcp -s 192.168.0.0/24 -d 192.168.0.123 --dport 3333 -j DNAT --to-destination 111.222.123.123:22222

iptables -t nat -A POSTROUTING -p tcp -s 192.168.0.0/24 --dport 22222 -j SNAT --to-source 1.2.3.4

沒有留言:

張貼留言