{"id":348,"date":"2020-05-28T18:09:51","date_gmt":"2020-05-29T01:09:51","guid":{"rendered":"https:\/\/SUMMALAI.COM\/?p=348"},"modified":"2020-05-28T18:09:51","modified_gmt":"2020-05-29T01:09:51","slug":"allow-ssh-for-specific-ip-address-and-block-all-others","status":"publish","type":"post","link":"https:\/\/SUMMALAI.COM\/?p=348","title":{"rendered":"Allow SSH for specific IP Address and block all others"},"content":{"rendered":"\n<p>Allow SSH for specific IP Address and block all others<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Solution 1:<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">Using \/etc\/hosts.allow And \/etc\/hosts.deny File<\/h1>\n\n\n\n<p>SSHD will first check \/etc\/hosts.allow for entries. If there is no rule in \/etc\/hosts.allow which fits SSHD will go on checking \/etc\/hosts.deny for rules.<\/p>\n\n\n\n<p>So you need to add:<\/p>\n\n\n\n<p>vim \/etc\/hosts.deny<\/p>\n\n\n\n<p><em>sshd: ALL EXCEPT LOCAL<\/em><\/p>\n\n\n\n<p>To block every SSH connection, except localhost, which is not in \/etc\/hosts.allow.<\/p>\n\n\n\n<p>vim \/etc\/hosts.allow<\/p>\n\n\n\n<p><em>sshd: 192.168.178.10<\/em><\/p>\n\n\n\n<p><em>sshd: 192.168.178.11<\/em><\/p>\n\n\n\n<p><em>sshd: 192.168.178.10\/255.255.255.0<\/em><\/p>\n\n\n\n<p>To allow specific IPs. Last rule is an example for an IP range.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Solution 2:<\/h1>\n\n\n\n<h1 class=\"wp-block-heading\">Using iptables<\/h1>\n\n\n\n<p>However using \/etc\/hosts.allow and \/etc\/hosts.deny is not the recommended method to allow SSH only for a few IPs. You should consider using iptables for that job.<\/p>\n\n\n\n<p>You could allow SSH for a specific IP by using a rule like:<\/p>\n\n\n\n<p><em>iptables -A INPUT -m state &#8211;state NEW,ESTABLISHED,RELATED &#8211;source x.x.x.x -p tcp &#8211;dport 22 -j ACCEPT<\/em><\/p>\n\n\n\n<p><em>iptables -A INPUT -m state &#8211;state NEW,ESTABLISHED,RELATED -p tcp &#8211;dport 22 -j DROP<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Allow SSH for specific IP Address and block all others Solution 1: Using \/etc\/hosts.allow And \/etc\/hosts.deny File SSHD will first check \/etc\/hosts.allow for entries. If there is no rule in \/etc\/hosts.allow which fits SSHD will go on checking \/etc\/hosts.deny for rules. So you need to add: vim \/etc\/hosts.deny sshd: ALL EXCEPT LOCAL To block every <a class=\"read-more\" href=\"https:\/\/SUMMALAI.COM\/?p=348\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-linux"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=348"}],"version-history":[{"count":0,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=\/wp\/v2\/posts\/348\/revisions"}],"wp:attachment":[{"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/SUMMALAI.COM\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}