Overly broad permissions can turn one compromised account into a much larger security problem. Learn how to reduce unnecessary access, review privileges, and apply least privilege across modern Linux systems. Review Linux Privileges×

Alerts This Week
Warning Icon 1 492
Alerts This Week
Warning Icon 1 492

How To Block IP Addresses In Linux With Hosts.allow And Hosts.deny

20.Lock AbstractDigital Circular Esm H446

In this How to Make Tech Work tutorial, Jack Wallen shows how to add another layer of security to your Linux machines with just two files.

Did you know there’s a very easy way to block or allow IP addresses in Linux using two simple files? Those files are hosts.allow and hosts.deny, and they make it such that you can block or allow IP addresses on the fly without having to deal with more complicated firewall rules. 

With this ability, you could quickly block a suspicious IP address to avoid possible malicious activity; or, you could limit, say, SSH connections to only specific addresses so you don’t have to worry about third parties gaining access to your servers or desktops. Let me show you how it’s done by demonstrating SSH access to a machine. The only things you’ll need for this are a running instance of Linux and a user with sudo privileges.

Log in to your Linux machine and open a terminal window. The first thing we’re going to do is deny all access to the SSH daemon. Open hosts.deny with the command sudo nano /etc/hosts.deny. At the bottom of that file, add sshd: ALL. Save and close the file.