I have configured my server's sshd to listen on a non-standard port 42.
However, at work I am behind a firewall/proxy, which only allow outgoing connections to ports 21, 22, 80 and 443. Consequently, I cannot ssh to my server from work, which is bad. I do not want to return sshd to port 22.
The idea is this: on my server, locally forward port 22 to port 42 if source IP is matching the external IP of my work's network. For clarity, let us assume that my server's IP is 188.8.131.52 (on eth1), and my work external IP is 184.108.40.206. For all IPs different from 220.127.116.11, my server should respond with an expected 'connection refused', as it does for a non-listening port.
I'm very new to iptables. I have briefly looked through the long iptables manual and these related / relevant questions:
- iptables question: forwarding port x to an ssh port of different machine on the network
- How can I port forward with iptables?
However, those questions deal with more complicated several-host scenarios, and it is not clear to me which tables and chains I should use for local port-forwarding, and if I should have 2 rules (for "question" and "answer" packets), or only 1 rule for "question" packets.
So far I have only enabled forwarding via sysctl. I will start testing solutions tomorrow, and will appreciate pointers or maybe case-specific examples for implementing my simple scenario.
Is the draft solution below correct?
iptables -A INPUT [-m state] [-i eth1] --source 18.104.22.168 -p tcp --destination 22.214.171.124:42 --dport 22 --state NEW,ESTABLISHED,RELATED -j ACCEPT
Should I use the
mangle table instead of
FORWARD chain instead of