If you need to set up port forwarding from one IPv4 port to another IPv6 port, we will use socat
.
It is assumed that IPv6 is already configured and working. You can check this with the commands
ping6 <domain>
in Linux orping -6 <domain>
in Windows.
In rare cases, ping may work, but actual traffic does not flow. You can further verify connectivity using the command
curl --ipv6 -k -I https://ipv6.google.com
for both Linux and Windows (ifcurl
is not available in cmd, open PowerShell).
Suppose we have a task to forward port 443 from IPv4 to port 4445 on IPv6.
1. Installing socat
apt install socat
2. Port Forwarding
socat TCP6-LISTEN:4445,fork,reuseaddr TCP4:127.0.0.1:443
Ensure that port 443 is accessible locally, or change it to an external IP.
telnet 127.0.0.1 443
2.1 Verification
telnet <ipv6> 4445
3. Creating a Service
Now, let’s create a service file so that the forwarding resumes automatically after a reboot without needing to run it manually. We’ll also ensure that the nginx
service starts before our command.
nano /etc/systemd/system/socat-portforward.service
[Unit]
Description=Socat IPv6 to IPv4 Port Forwarding
After=network.target
After=nginx.service
Requires=nginx.service
[Service]
ExecStart=/usr/bin/socat TCP6-LISTEN:4445,fork,reuseaddr TCP4:127.0.0.1:443
Restart=always
RestartSec=3
User=root
Group=root
KillMode=process
[Install]
WantedBy=multi-user.target
3.1 Stopping the Service
Press Ctrl + C
or, if you ran it with &
:
killall socat
3.2 Starting as a Service
systemctl daemon-reload
systemctl enable --now socat-portforward
systemctl status socat-portforward
3.3 Startup Sequence
We added lines to the [Unit]
section to ensure the correct startup sequence:
After=nginx.service
Requires=nginx.service
Done! This command can be used to configure port forwarding in various setups.