Port forwarding without daemon nor persistent tunnel
I just had this kind of problem. In my situation, I'm migrating a slapd service (LDAP server). All clients are configured to reach the LDAP server using a CNAME (ldap-server.example.com). The correct way (and it will be the final state) will be to fix/update the CNAME. But the CNAME is managed by other people and I would like to redirect incomming ldaps connection from the old server to the new server now.
I just run these three commands on the old server when the network interface is up:
sysctl net.ipv4.ip_forward=1
iptables -t nat -A PREROUTING -p tcp -d 10.0.0.62 --dport 636 -j DNAT --to-destination 10.0.0.35:636
iptables -t nat -A POSTROUTING -o enp3s0 -j MASQUERADE
where 10.0.0.62 is the IP of the old server, 10.0.0.35 is the IP of the new server, 636 is the port I'm forwarding (ldaps) and enp3s0 is the (only one) network interface on the old server.
By default, TCP forwarding is disabled, hence the first line. Without it, no packets are redirected at all.
The second line redirects incoming TCP (ldaps) packets to the new server.
The third line is important: without it, redirected packets still have the LDAP client IP as IP source, hence new server answers are directly sent to the client (that do not understand why it receives packets from a machine it never contacts). With the third line, the source IP of forwarded packets is changed (for the local IP, the old server IP) and the kernel tracks this change. So answers are sent to this (old server) machine and also redirected to the LDAP client adjusting source/dest IP (thanks to the kernel masquerading).
So, I got a port forwarding done at kernel level, without the need of processes (nc, ssh, etc.) nor daemons (sshd, etc.)