Ssh – allow ssh outgoing with iptables default policy as drop

iptablessshvps

Good day all

My VPS allows incoming ssh connections as required (see rule),

however attempting outgoing ssh connections fails to connect with

$ ssh xyz.ddns.net
ssh: connect to host xyz.ddns.net port 22: Connection timed out 

I have found setting the default policy to accept allows outgoing ssh connections

Chain INPUT (policy DROP)

ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh /* SSH-SECURE IN (iface:ALL) */ state NEW,RELATED,ESTABLISHED

NEW – The connection has not yet been seen.

RELATED – The connection is new, but is related to another connection already > permitted.

should allow these outgoing connections?

Best Answer

No. This rule allows INCOMING packets which have destination port 22.

When you establish outgoing connection to remote port 22, your local port is selected randomly. You should have rule

-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

otherwise remote server's answers can not reach your server.