SSH Attack

How To Install DenyHosts on Ubuntu 24.04: A Comprehensive Guide

In today’s digital landscape, ensuring the security of your server is paramount. One of the common threats faced by server administrators is the illicit attempts to access your server through brute-force attacks. Fortunately, tools like DenyHosts can help mitigate these risks by monitoring and blocking suspicious login attempts. In this blog post, we’ll walk you through the steps to install and configure DenyHosts on Ubuntu 24.04, so you can enhance your server’s security with ease.

What is DenyHosts?

DenyHosts is a Python application that acts as an SSH attacker blocker. It detects and blocks hosts that are attempting to gain unauthorized access to your server via SSH. By automatically updating the /etc/hosts.deny file, DenyHosts can help you keep your server secure without constant manual intervention.

Prerequisites

Before we get started with the installation, ensure that you have:

  • An Ubuntu 24.04 server up and running.
  • Administrative access (root or sudo privileges).
  • SSH access to your server.

Step 1: Update Your System

Before any installation, it’s a good practice to update your package list to ensure that you’re installing the latest version of software available. Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Install DenyHosts

DenyHosts is not included in the default repositories for Ubuntu 24.04, but you can install it via the apt command. Execute the following command to install DenyHosts:

sudo apt install denyhosts -y

Step 3: Configure DenyHosts

After installation, you’ll need to configure DenyHosts to ensure it works optimally for your server’s needs. The main configuration file is located at /etc/denyhosts.conf. You can open this file using a text editor of your choice, such as nano:

sudo nano /etc/denyhosts.conf

Key Configuration Settings

  1. HOSTS_DENY: By default, DenyHosts adds IP addresses to /etc/hosts.deny. Ensure this is set correctly.
   HOSTS_DENY = /etc/hosts.deny
  1. MAX_RETRIES: This sets the number of failed login attempts allowed before an IP address gets blocked. A typical setting is three.
   MAX_RETRIES = 3
  1. SECURITY: This setting enables or disables the protection features. Make sure it is set to 1 to enable protection.
   SECURE = 1
  1. ADMIN_EMAIL: Specify an email address where DenyHosts will send alerts regarding blocked IPs.
   ADMIN_EMAIL = [email protected]

After making any changes, save the file (if using nano, press CTRL + X, then Y, and Enter to save).

Step 4: Starting DenyHosts

To start the DenyHosts service, use the following command:

sudo systemctl start denyhosts

To ensure that DenyHosts starts automatically on system boot, run:

sudo systemctl enable denyhosts

Step 5: Monitor DenyHosts Logs

DenyHosts logs all its activity, which can be useful for monitoring who is being denied access and the reason for their blockage. You can find the log file at /var/log/denyhosts.log. To view the logs, run:

cat /var/log/denyhosts.log

For continuous monitoring, you can use:

tail -f /var/log/denyhosts.log

Step 6: Unblocking IPs

If you need to unblock any IP address that was mistakenly added to the deny list, you can do so by editing the /etc/hosts.deny file:

sudo nano /etc/hosts.deny

Locate the specific IP address you wish to unblock and remove the line containing it. Save and exit.

Conclusion

With the steps above, you have successfully installed and configured DenyHosts on your Ubuntu 24.04 server. By actively monitoring and blocking unwanted access attempts, DenyHosts helps keep your server secure from brute-force attacks. Regularly check the logs and stay informed about potential threats to ensure the continued safety of your server environment.

As with any security tool, combine DenyHosts with strong passwords, SSH key authentication, and firewall settings for a robust security posture.

Feel free to reach out in the comments if you have any questions or need further assistance with your server security. Happy securing!


About the Author

At Greenhost.Cloud, we’re dedicated to empowering users with the tools and knowledge to enhance their web hosting experience. Our team specializes in cloud solutions, server management, and cybersecurity. Follow our blog for more tips and tutorials!