iptables Allow SSH
Get the full rule set in the correct order, not just one line. Order is what keeps you from locking yourself out.
Your rule set, run in this order
The first rule keeps your current session alive, the second allows local traffic, the third lets SSH in. In normal mode the default policy is switched to DROP last, because setting it earlier cuts you off at once. To see why order matters, read iptables tables and chains explained.
Optional safety net before you test
Back up the current rules and schedule an automatic restore in 5 minutes, then run the rule set. If you are still connected and everything works, cancel the restore. If you get locked out, the backup returns after five minutes.
Emergency undo
From the provider's web console or a second session. Restore the backup. The last two lines are a fallback that removes all INPUT rules.
Keep it after a reboot
Once it works, save with sudo netfilter-persistent save. Details in saving iptables rules. On a cloud server the provider firewall must allow the same port, see Oracle Cloud.
FAQ
Which port does SSH use?
SSH listens on TCP port 22 by default. If you changed it in sshd_config, use that port in the rules.
Why did I get locked out after changing iptables?
Usually the default policy was set to DROP before an SSH allow rule existed, or the allow rule sits after a REJECT rule. Add the allow rules first and test in a second session.
Is it safe to allow SSH only from my IP?
It is stronger, but if your IP changes you lose access. Keep a second way in, such as the provider web console.