Smart Doorkeeper

Charly's Column – DenyHosts

Article from Issue 256/2022

When it comes to warding off unwanted login tests on SSH port 22 and others, Charly likes to keep an ace or two up his sleeve by relying on DenyHosts instead of Fail2ban.

Many sys admins use Fail2ban to keep unwanted login tests on SSH port 22 and others at bay. However, there are alternatives to Fail2ban. Today, I would like to introduce you to DenyHosts, which does basically the same thing as Fail2ban but has one or two aces up its sleeve.

You can find DenyHosts as part of the standard package on all popular distributions; my lab system is an up-to-date Debian. To install DenyHosts, type:

sudo apt install denyhosts

This is followed by the most important step of the configuration: You need to add the IP addresses of all the systems from which you want to log in to the protected system without being blocked to the /etc/hosts.allow file (Listing 1). If you forget this step, there is a danger that you will be locked out of the system permanently – so it might be better to take another sip of coffee and check again that there are no typos.

Listing 1



DenyHosts is controlled by the /etc/denyhosts.conf file, which contains sensible defaults, but I'll take a closer look at some of them anyway. The most important lines in this longish configuration file are the ones where I define the number of failed attempts before the system locks out a host by applying an iptables rule. In doing so, DenyHosts takes into account whether you log in with a username that actually exists in /etc/passwd (Listing 2). After adjusting the values to your satisfaction, it's time to put DenyHosts into operation (Listing 3).

Listing 2


# Admissible failed attempts:
# Username does not exist
# Username exists
# Username is "root"

Listing 3

Starting DenyHosts

$ sudo systemctl restart denyhosts
$ sudo systemctl enable denyhosts

DenyHosts writes all activities to the /var/log/auth.log file (Figure 1). If you exceed one of the thresholds, the IP address of the knocking system is entered in the /etc/hosts.deny file and blocked by iptables (Figure 2). As a highlight, DenyHosts can synchronize with other servers worldwide – the settings for this can be found a bit further down in the Sync section of the configuration file.

Figure 1: DenyHosts logs its actions in the /var/log/auth.log file.
Figure 2: Game over: Iptables has blocked the IP address.

The fact that DenyHosts, unlike Fail2ban, does not release a blocked IP address after a certain time is seen as a disadvantage by some admins. So far, however, none of my users has managed to enter their password incorrectly more than 10 times. In the end, it depends on your personal preferences as to which of the two tools you use. I tend cautiously towards DenyHosts.

The Author

Charly Kühnast manages Unix systems in a data center in the Lower Rhine region of Germany. His responsibilities include ensuring the security and availability of firewalls and the DMZ.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Workspace: Raspberry Pi Server

    With the help of the applications we describe, Raspberry Pi can perform a wide range of server duties.

  • FOSSPicks

    Graham checks out Sigil, Dragonfly Reverb, LabPlot 2.8, Node-RED, batufo, rg3d, and much more.

  • Securing Your SSH Server

    An SSH server facing the Internet will almost certainly be under attack, but a few proactive steps will help to keep the intruders away.

  • Squid proxy server

    A proxy server provides safer and more efficient surfing. Although commercial proxy solutions are available, all you really need is Linux and an old PC in the attic.

  • Charly's Column

    If protocols were human beings, NNTP would be a kind and slightly confused person that always believes the best of other people – even if they drop trash in the mailbox. Postfilter gives NNTP a watchdog.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More