{
  "slug": "Redirect-port-from-one-server-to-port-of-another-server-via-Iptables-7d77f37d7fa0",
  "title": "Redirect port from one server to port of another server via Iptables",
  "subtitle": "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…",
  "excerpt": "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…",
  "date": "2023-01-15",
  "tags": [
    "My Experience"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/redirect-port-from-one-server-to-port-of-another-server-via-iptables-7d77f37d7fa0",
  "hero": "https://cdn-images-1.medium.com/max/800/1*2seMAJoclGqsKHiPMlSImQ.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Redirect port from one server to port of another server via Iptables"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*2seMAJoclGqsKHiPMlSImQ.png",
      "alt": "http://linux-training.be/networking/ch14.html",
      "caption": "http://linux-training.be/networking/ch14.html",
      "width": 1664,
      "height": 676
    },
    {
      "type": "paragraph",
      "html": "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."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"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/\" target=\"_blank\" rel=\"noreferrer noopener\">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/</a>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Forward data from 1524 port to another server 1.1.1.1 on port 3000"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1524 -j DNAT\n--to-destination 1.1.1.1:3000iptables -t nat -A POSTROUTING -j MASQUERADE"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Whenever we want to send back data from a special port and IP use the below command:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1111 -j DNAT\n--to-destination 2.2.2.2:2222iptables -t nat -A POSTROUTING -d 2.2.2.2 -p tcp --dport 2222   -j\nMASQUERADE"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Persistance after reboot the server"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "service iptables save"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "See all rules of Iptables"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo iptables -L -n -t nat\nsudo iptables -L -v"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Also we can pass all traffic from one server to another"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "sysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING -p tcp --dport 1233 -j DNAT\n--to-destination 1.1.1.1iptables -t nat -A PREROUTING -j DNAT --to-destination 2.2.2.2iptables -t\nnat -A POSTROUTING -j MASQUERADE"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "For delete Command just write below commands"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "iptables -t nat -D PREROUTING -p tcp --dport 1233 -j DNAT --to-destination 1.1.1.1iptables -t nat -D\nPREROUTING -j DNAT--to-destination 2.2.2.2iptables -t nat -D POSTROUTING -j MASQUERADE"
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.cyberciti.biz/faq/linux-iptables-delete-prerouting-rule-command/\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.cyberciti.biz/faq/linux-iptables-delete-prerouting-rule-command/</a>"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt install iptables iptables-persistent -ysysctl net.ipv4.ip_forward=1iptables -t nat -A PREROUTING\n-p tcp --dport 2280 -j DNAT --to-destination IRAN-IPiptables -t nat -A PREROUTING -j DNAT\n--to-destination KHAREJ-IPiptables -t nat -A POSTROUTING -j MASQUERADEsystemctl enable\nnetfilter-persistent.service"
    },
    {
      "type": "paragraph",
      "html": "‍‍‍"
    },
    {
      "type": "paragraph",
      "html": "Subscribe to DDIntel <a href=\"https://ddintel.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\">Here</a>."
    },
    {
      "type": "paragraph",
      "html": "Visit our website here: <a href=\"https://www.datadriveninvestor.com/\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.datadriveninvestor.com</a>"
    },
    {
      "type": "paragraph",
      "html": "Join our network here: <a href=\"https://datadriveninvestor.com/collaborate\" target=\"_blank\" rel=\"noreferrer noopener\">https://datadriveninvestor.com/collaborate</a>"
    }
  ]
}