{"id":17856,"date":"2024-11-28T07:21:36","date_gmt":"2024-11-28T07:21:36","guid":{"rendered":"https:\/\/truehost.com\/support\/?post_type=docs&#038;p=17856"},"modified":"2024-11-28T07:21:37","modified_gmt":"2024-11-28T07:21:37","password":"","slug":"how-to-forward-ports-with-iptables-on-a-linux-server","status":"publish","type":"docs","link":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/","title":{"rendered":"How to Forward Ports With Iptables On A Linux Server"},"content":{"rendered":"\n<p><a href=\"https:\/\/ultahost.com\/knowledge-base\/2024\/09\/16\/\"><\/a><a href=\"https:\/\/ultahost.com\/knowledge-base\/2024\/09\/16\/\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to Set Up NAT and Forward Ports on Linux Servers<\/h3>\n\n\n\n<p>Port forwarding is an essential tool for network admins, enabling internet traffic redirection from one IP address and port to another. It\u2019s widely used to access services behind firewalls or NAT, such as hosting web servers or connecting to private network resources. Here&#8217;s a concise guide on using\u00a0<code>iptables<\/code>\u00a0to set up NAT and port forwarding in Linux.<a href=\"https:\/\/ultahost.com\/knowledge-base\/2024\/09\/16\/\"><\/a><a href=\"https:\/\/ultahost.com\/knowledge-base\/2024\/09\/16\/\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Understanding iptables and Port Forwarding<\/strong><\/h3>\n\n\n\n<p><strong>iptables<\/strong>&nbsp;is a command-line utility for managing traffic rules in Linux. It operates on chains and tables to allow or block network packets.<\/p>\n\n\n\n<p><strong>Port forwarding<\/strong>&nbsp;is a NAT technique that redirects traffic from one IP:Port&nbsp;combination to another, making private services accessible externally.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step-by-Step Guide to Set Up Port Forwarding<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: Install iptables<\/strong><\/h4>\n\n\n\n<p>First, ensure&nbsp;<code>iptables<\/code>&nbsp;is installed. On most systems, it&#8217;s pre-installed. If not, use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install iptables<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: Enable IP Forwarding<\/strong><\/h4>\n\n\n\n<p>Enable packet forwarding by editing the&nbsp;<code>sysctl.conf<\/code>&nbsp;file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/sysctl.conf<\/code><\/pre>\n\n\n\n<p>Add the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>net.ipv4.ip_forward=1<\/code><\/pre>\n\n\n\n<p>Apply the changes:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo sysctl -p<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: Set Up Port Forwarding Rules<\/strong><\/h4>\n\n\n\n<p>Use the&nbsp;<code>PREROUTING<\/code>&nbsp;chain in the&nbsp;<code>nat<\/code>&nbsp;table to redirect traffic. For example, to forward traffic from external port 80 to internal IP&nbsp;<code>192.168.0.121<\/code>&nbsp;on port 8080:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 192.168.0.121:8080<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: Make Rules Persistent<\/strong><\/h4>\n\n\n\n<p>By default,&nbsp;<code>iptables<\/code>&nbsp;rules are lost after a reboot. Save them using&nbsp;<code>netfilter-persistent<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install iptables-persistent\n\nsudo netfilter-persistent save &amp;&amp; sudo netfilter-persistent reload<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 5: Verify Rules<\/strong><\/h4>\n\n\n\n<p>Check the active&nbsp;<code>iptables<\/code>&nbsp;rules with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -t nat -L -n -v<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 6: Test Port Forwarding<\/strong><\/h4>\n\n\n\n<p>Test the forwarding by accessing the service externally:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>curl http:\/\/&lt;external-io>:80<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h3>\n\n\n\n<p>Port forwarding with&nbsp;<code>iptables<\/code>&nbsp;provides a robust way to manage traffic in Linux. By following these steps, you can efficiently configure NAT and forward ports to make internal services accessible while maintaining security.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to Set Up NAT and Forward Ports on Linux Servers Port forwarding is an essential tool for network admins, enabling internet traffic redirection from one IP address and port to another. It\u2019s widely used to access services behind firewalls or NAT, such as hosting web servers or connecting to private network resources. Here&#8217;s a [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_eb_attr":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"doc_category":[1820,1824,1879,2128],"doc_tag":[],"class_list":["post-17856","docs","type-docs","status-publish","hentry","doc_category-cloud-servers-in-kenya","doc_category-dedicated-servers","doc_category-servers","doc_category-vps-servers"],"year_month":"2026-07","word_count":335,"total_views":0,"reactions":{"happy":0,"normal":0,"sad":0},"author_info":{"name":"Eugene","author_nicename":"eugene","author_url":"https:\/\/truehost.com\/support\/author\/eugene\/"},"doc_category_info":[{"term_name":"Cloud servers in Kenya","term_url":"https:\/\/truehost.com\/support\/docs-category\/cloud-servers-in-kenya\/"},{"term_name":"dedicated servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/dedicated-servers\/"},{"term_name":"Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/servers\/"},{"term_name":"VPS-Servers","term_url":"https:\/\/truehost.com\/support\/docs-category\/vps-servers\/"}],"doc_tag_info":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Forward Ports With Iptables On A Linux Server -<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Forward Ports With Iptables On A Linux Server -\" \/>\n<meta property=\"og:description\" content=\"How to Set Up NAT and Forward Ports on Linux Servers Port forwarding is an essential tool for network admins, enabling internet traffic redirection from one IP address and port to another. It\u2019s widely used to access services behind firewalls or NAT, such as hosting web servers or connecting to private network resources. Here&#8217;s a [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-28T07:21:37+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-forward-ports-with-iptables-on-a-linux-server\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-forward-ports-with-iptables-on-a-linux-server\\\/\",\"name\":\"How to Forward Ports With Iptables On A Linux Server -\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\"},\"datePublished\":\"2024-11-28T07:21:36+00:00\",\"dateModified\":\"2024-11-28T07:21:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-forward-ports-with-iptables-on-a-linux-server\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-forward-ports-with-iptables-on-a-linux-server\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/knowledge-base\\\/how-to-forward-ports-with-iptables-on-a-linux-server\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/truehost.com\\\/support\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Forward Ports With Iptables On A Linux Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#website\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"name\":\"\",\"description\":\"Help In a Click\",\"publisher\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/truehost.com\\\/support\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#organization\",\"name\":\"Truehost Kenya\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"contentUrl\":\"https:\\\/\\\/truehost.com\\\/support\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/cropped-image_2026-04-16_174808866.png\",\"width\":240,\"height\":48,\"caption\":\"Truehost Kenya\"},\"image\":{\"@id\":\"https:\\\/\\\/truehost.com\\\/support\\\/#\\\/schema\\\/logo\\\/image\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Forward Ports With Iptables On A Linux Server -","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/","og_locale":"en_US","og_type":"article","og_title":"How to Forward Ports With Iptables On A Linux Server -","og_description":"How to Set Up NAT and Forward Ports on Linux Servers Port forwarding is an essential tool for network admins, enabling internet traffic redirection from one IP address and port to another. It\u2019s widely used to access services behind firewalls or NAT, such as hosting web servers or connecting to private network resources. Here&#8217;s a [&hellip;]","og_url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/","article_modified_time":"2024-11-28T07:21:37+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/","url":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/","name":"How to Forward Ports With Iptables On A Linux Server -","isPartOf":{"@id":"https:\/\/truehost.com\/support\/#website"},"datePublished":"2024-11-28T07:21:36+00:00","dateModified":"2024-11-28T07:21:37+00:00","breadcrumb":{"@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/truehost.com\/support\/knowledge-base\/how-to-forward-ports-with-iptables-on-a-linux-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/truehost.com\/support\/"},{"@type":"ListItem","position":2,"name":"How to Forward Ports With Iptables On A Linux Server"}]},{"@type":"WebSite","@id":"https:\/\/truehost.com\/support\/#website","url":"https:\/\/truehost.com\/support\/","name":"","description":"Help In a Click","publisher":{"@id":"https:\/\/truehost.com\/support\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/truehost.com\/support\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/truehost.com\/support\/#organization","name":"Truehost Kenya","url":"https:\/\/truehost.com\/support\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/","url":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","contentUrl":"https:\/\/truehost.com\/support\/wp-content\/uploads\/2026\/04\/cropped-image_2026-04-16_174808866.png","width":240,"height":48,"caption":"Truehost Kenya"},"image":{"@id":"https:\/\/truehost.com\/support\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/17856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/comments?post=17856"}],"version-history":[{"count":1,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/17856\/revisions"}],"predecessor-version":[{"id":17857,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/docs\/17856\/revisions\/17857"}],"wp:attachment":[{"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/media?parent=17856"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_category?post=17856"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/truehost.com\/support\/wp-json\/wp\/v2\/doc_tag?post=17856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}