Many times, hackers and bots try to gain access to your system via SSH by using brute force attacks and other methods. This can compromise the security of your system. So it is important to restrict SSH access to specific IP addresses that you trust. In this article, we will look at how to restrict SSH access to specific IP addresses. You can use these steps on almost every Linux distribution.
How to Restrict SSH Access to Specific IP Addresses
Linux provides two files host.allow and host.deny to allow and deny access to SSH port. You simply need to add your trusted IP addresses to host.allow file, and add suspicious IP addresses in host.deny file.
1. Allow SSH Access
Open terminal and run the following command to open host.allow file.
$ sudo vi /etc/host.allow
Add the following line to allow access from ip 220.127.116.11
If you want to allow access from multiple IP addresses, add them in a comma-separated manner
sshd: 18.104.22.168, 22.214.171.124, 126.96.36.199
If you want to allow access from range of IP addresses use the CIDR notation to allow an IP address range. Here is an example to allow ip address 188.8.131.52-184.108.40.206 and 220.127.116.11-18.104.22.168
sshd: 22.214.171.124/24, 126.96.36.199/24
2. Restrict SSH Access
Similarly, open terminal and run the following command to open host.deny file.
$ sudo vi /etc/host.deny
Add the following line to deny access from ip 188.8.131.52
If you want to restrict access from multiple IP addresses, add them in a comma-separated manner
sshd: 184.108.40.206, 220.127.116.11, 18.104.22.168
If you want to restrict access from range of IP addresses use the CIDR notation to allow an IP address range. Here is an example to restrict ip address 22.214.171.124-126.96.36.199 and 188.8.131.52-184.108.40.206
sshd: 220.127.116.11/24, 18.104.22.168/24
If you want to block all addresses other than the ones mentioned host.allow file, then add the following line in host.deny file.
This will protect your website brute force attacks and other malicious methods used by hackers.
How to Find Users Currently Logged in Linux
How to Check Uptime of System in Linux
How to Resize Partition in Ubuntu
How to Update Key in Python Dictionary
How to Search Item in List of Dictionaries in Python