How to disable SSH access from everywhere except for certain IPs in CentOS 7

Here’s how to do it:

This declares an internal zone with two IPs (add as many or as few as you like) and subsequently removes the SSH service from the public zone altogether. As a result, any other IP gets a message such as “Connection refused” when trying to connect via SSH.

The “–permanent” switch saves the changes. Remove it for testing or if you don’t want this change to be permanent.

The last line reloads the current firewall rules (thanks, CertDepot).

  • http://serverfault.com/questions/680780/block-all-but-a-few-ips-with-firewalld

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

6 thoughts on “How to disable SSH access from everywhere except for certain IPs in CentOS 7

      1. The basics could be read here: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Security_Guide/sect-Security_Guide-TCP_Wrappers_and_xinetd-TCP_Wrappers_Configuration_Files.html

        Following your example:

        /etc/hosts.allow:
        sshd : 1.2.3.4/32, 5.6.7.8/32

        /etc/hosts.deny:
        sshd: ALL

        —————–

        You can restrict access to a wide variety of daemons. You can know whether a daemon supports or not tcp wrappers with the following command:

        $ ldd /usr/sbin/sshd | grep wrap
        libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fe777cbf000)

        If libwrap is a dynamic dependency, tcp wrappers are supported.

Add your voice!