{"id":60595,"date":"2023-10-30T19:12:19","date_gmt":"2023-10-30T18:12:19","guid":{"rendered":"https:\/\/ekiwi-blog.de\/60595\/linux-create-local-port-redirection\/"},"modified":"2023-10-30T19:12:19","modified_gmt":"2023-10-30T18:12:19","slug":"linux-create-local-port-redirection","status":"publish","type":"post","link":"https:\/\/ekiwi-blog.de\/en\/60595\/linux-create-local-port-redirection\/","title":{"rendered":"Linux, create local port redirection"},"content":{"rendered":"<p>The Fritz Box interface opens on the port forwarding.<\/p>\n<p><!--more--><\/p>\n<p>The following situation: Nextcloud or an Apache web server is running on my home server. Port forwarding is set up. The external domain points to the address of the Fritzbox or the router with a CNAME.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of content<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/ekiwi-blog.de\/en\/60595\/linux-create-local-port-redirection\/#The_problem_Port_forwarding_Fritzbox_reports\" >The problem: Port forwarding, Fritzbox reports<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/ekiwi-blog.de\/en\/60595\/linux-create-local-port-redirection\/#Redirect_local_port\" >Redirect local port<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/ekiwi-blog.de\/en\/60595\/linux-create-local-port-redirection\/#Create_systemd_service\" >Create systemd service<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"The_problem_Port_forwarding_Fritzbox_reports\"><\/span>The problem: Port forwarding, Fritzbox reports<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>On the road everything works. I can access it externally with &#8220;subdomain.domain.de&#8221;. Only in the home network does the Fritzbox user interface appear. Of course I can use the IP address in the internal network, but it&#8217;s stupid for the Nextcloud client on the notebook, for example, because I then have a different server address internally than externally.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_1.png\" alt=\"\" width=\"750\" height=\"346\" class=\"aligncenter size-full wp-image-60583\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_1.png 750w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_1-300x138.png 300w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>One possibility is to reconfigure the Apache server so that it does not run on the same port as the Fritzbox. But I didn&#8217;t want to do that, so I looked for a way to redirect a local port. I.e. in the example I release port 8443 via the Fritzbox and forward this to the server. However, the server works on port 443 (https).<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Redirect_local_port\"><\/span>Redirect local port<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The solution is &#8220;socat&#8221;, the software allows to redirect a local port to another local port. First we install the software:<\/p>\n<pre>\r\nsudo apt update\r\nsudo apt install socat\r\n<\/pre>\n<p>We then redirect port 8443 to 443:<\/p>\n<pre>\r\nsocat tcp-listen:8443,reuseaddr,fork tcp:localhost:443\r\n<\/pre>\n<p>So far so good, however the redirection is gone again as soon as the command finishes. We can note a &#038; at the end, then a process is started, but only as long as the computer does not restart.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Create_systemd_service\"><\/span>Create systemd service<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now to run the script to start the system, we create a script. It has the following content, important that &#038; at the end.<\/p>\n<pre>\r\n#!\/bin\/bash\r\nsocat tcp-listen:8443,reuseaddr,fork tcp:localhost:443 &\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_2.png\" alt=\"\" width=\"542\" height=\"112\" class=\"aligncenter size-full wp-image-60587\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_2.png 542w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_2-300x62.png 300w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/p>\n<p>We can create the script under \/root\/socat.sh, for example, and then make it executable:<\/p>\n<pre>\r\nsudo vim \/root\/socat.sh\r\nsudo chmod +x \/root\/socat.sh\r\n<\/pre>\n<p>Now we create a service. There we create a new text file:<\/p>\n<pre>\r\nsudo vim \/etc\/system\/system\/socat.service\r\n<\/pre>\n<p>The content looks like this, adapt if necessary:<\/p>\n<pre>\r\n[Unit]\r\nDescription=socat script\r\n\r\n[Service.]\r\nExecStart=\/root\/socat.sh\r\nType=oneshot\r\nRemainAfterExit=yes\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_3.png\" alt=\"\" width=\"351\" height=\"263\" class=\"aligncenter size-full wp-image-60591\" srcset=\"https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_3.png 351w, https:\/\/ekiwi-blog.de\/wp-content\/uploads\/2023\/10\/socat_3-300x225.png 300w\" sizes=\"auto, (max-width: 351px) 100vw, 351px\" \/><\/p>\n<p>We can then activate the service:<\/p>\n<pre>\r\nsudo systemctl daemon-reload\r\nsudo systemctl enable socat.service\r\n<\/pre>\n<p>Done! After the restart, the service is now available. If not, then we can check this with the following command:<\/p>\n<pre>\r\nsudo systemctl status socat.service\r\n<\/pre>","protected":false},"excerpt":{"rendered":"<p>The Fritz Box interface opens on the port forwarding.<\/p>\n","protected":false},"author":1,"featured_media":18426,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1555],"tags":[2443,1682,2444,2073],"class_list":["post-60595","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-en","tag-linux-en-2","tag-linux-en","tag-ubuntu-en-2","tag-ubuntu-en"],"_links":{"self":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/60595","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/comments?post=60595"}],"version-history":[{"count":0,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/posts\/60595\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media\/18426"}],"wp:attachment":[{"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/media?parent=60595"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/categories?post=60595"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ekiwi-blog.de\/en\/wp-json\/wp\/v2\/tags?post=60595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}