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.
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 MASQUERADEWhenever 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
MASQUERADEPersistance after reboot the server
text
service iptables saveSee all rules of Iptables
bash
sudo iptables -L -n -t nat
sudo iptables -L -vAlso 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 MASQUERADEFor 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 MASQUERADEhttps://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
