Redirect port from one server to port of another server via Iptables

In this article, I want to redirect traffic from one server to another server. The main reason is I want to fetch data from a newspaper in another country and need to connect all my microservices in the easiest way to each other.

https://my.esecuredata.com/index.php?/knowledgebase/article/49/how-to-redirect-an-incoming-connection-to-a-different-ip-address-on-a-specific-port-using-iptables/

Forward data from 1524 port to another server 1.1.1.1 on port 3000

text
sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1524 -j DNAT
--to-destination 1.1.1.1:3000iptables -t nat -A POSTROUTING -j MASQUERADE

Whenever we want to send back data from a special port and IP use the below command:

text
sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1111 -j DNAT
--to-destination 2.2.2.2:2222iptables -t nat -A POSTROUTING -d 2.2.2.2 -p tcp --dport 2222   -j
MASQUERADE

Persistance after reboot the server

text
service iptables save

See all rules of Iptables

bash
sudo iptables -L -n -t nat
sudo iptables -L -v

Also we can pass all traffic from one server to another

text
sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1233 -j DNAT
--to-destination 1.1.1.1iptables -t nat -A PREROUTING -j DNAT --to-destination 2.2.2.2iptables -t
nat -A POSTROUTING -j MASQUERADE

For delete Command just write below commands

text
iptables -t nat -D PREROUTING -p tcp --dport 1233 -j DNAT --to-destination 1.1.1.1iptables -t nat -D
PREROUTING -j DNAT--to-destination 2.2.2.2iptables -t nat -D POSTROUTING -j MASQUERADE

https://www.cyberciti.biz/faq/linux-iptables-delete-prerouting-rule-command/

bash
sudo
apt install iptables iptables-persistent -ysysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING
-p tcp --dport 2280 -j DNAT --to-destination IRAN-IPiptables -t nat -A PREROUTING -j DNAT
--to-destination KHAREJ-IPiptables -t nat -A POSTROUTING -j MASQUERADEsystemctl enable
netfilter-persistent.service

‍‍‍

Subscribe to DDIntel Here.

Visit our website here: https://www.datadriveninvestor.com

Join our network here: https://datadriveninvestor.com/collaborate