I'm trying to learn Linux and get familiar with it. How do I block all IPs except two certain IPs on different subnets for SSH/SSHD? I've been trying to Google for an answer but nothing seem to work out for me!
To your main question,
How do I block all IPs except two certain IPs on different subnets for SSH/SSHD?
Using iptables simply replace the 192.168.0.1/24 with whatever IP/Subnet you need to allow. Make sure to append the last rule. As rules are read from top to bottom, adding it first will lock you out if you are remoting in to your system
iptables -I INPUT -s 192.168.0.1/24 -p tcp --dport 22 -j ACCEPT iptables -I INPUT -s 192.168.0.1/24 -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j DROP
In regards to your second question,
how to restrict SSH access after 5 attempt
Append or modify the existing MaxAuthTries parameter in your ssh config:
echo "MaxAuthTries 5" >> /etc/ssh/sshd_config
This will deny login after 5 failed attempts and any attempts after half the value (3 attempts in this case) will be logged.