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

Forwarding traffic between two interface

· 52CE 我爱测

I have a Linux box with two interfaces: ~#ip -4 addr show scope global 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 inet 192.168.45.101/24 brd 192.168.45.255 scope global eth0 8: tu…

问题原文

原帖链接 · azeddine ben omar · CC BY-SA 4.0

I have a Linux box with two interfaces:

~#ip -4 addr show scope global
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
inet 192.168.45.101/24 brd 192.168.45.255 scope global eth0
8: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 100
inet 172.17.1.230 peer 172.17.1.229/32 scope global tun0
10: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 192.168.1.1/24 brd 192.168.1.255 scope global eth1
  • Eth0 is connected to our local network (other devices can be found on this network)
  • Eth1 is connected to one device (let name it BD) which has a static IP 192.168.1.5
  • Tun0 is our VPN network

I have two questions :)

  1. how can I reach the web server of the BD (192.168.1.5:80) from a machine that is on the local network (by taping on the browser 192.168.45.101:8080) or on the VPN network (by taping on the browser 172.17.1.230:8080)? I am really a big beginner on the network configuration :P so I understand from this that I should route the traffic from the port 8080 to the port 80
  2. the BD machine have one other TCP port 5017 that it uses to send an receive some data. How can I listen to this port from a machine that is connected on the local network or the VPN network?

here is what I did try for the first question (and of course didn't works):

~# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.5:80
~# iptables -t nat -A PREROUTING -i tun0 -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.5:80
~#iptables -A FORWARD -i eth0 -p tcp --dport 80 -j ACCEPT
~#iptables -A FORWARD -i tun0 -p tcp --dport 80 -j ACCEPT

and Here is what I tried for the second question (the same thing .. didn't work):

~# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 5017-j DNAT --to-destination 192.168.1.5:5017
~# iptables -t nat -A PREROUTING -i tun0 -p tcp --dport 5017-j DNAT --to-destination 192.168.1.5:5017
~#iptables -A FORWARD -i eth0 -p tcp --dport 5017 -j ACCEPT
~#iptables -A FORWARD -i tun0 -p tcp --dport 5017 -j ACCEPT

can someone help, please ... (I spend to 3 days on this problem and I have a deadline)

Thank you

已采纳解答

原帖链接 · Bilal · CC BY-SA 4.0

There is the precudure to redirect traffic from one server to another :

#enable IP forwarding:
sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g' /etc/sysctl.conf

#then activate the changes
sudo sysctl -p

#Start iptables
sudo systemctl start iptables

#Check iptables rules:
(check that you don't have a deny policy )

#You can flush all rule to start in a clean env:
sudo iptables -F
#+For the nat 
sudo iptables -t nat -F

#Add rules
sudo iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.1.5:80

#To check :
sudo iptables -t nat -L -n

#Save the iptables rule :
sudo iptables-save | sudo tee /etc/iptables.up.rules

I hope it will work for you :)

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

资料来源与适用范围

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

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

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