I currently block all ssh logins using root. But I wanted to go the extra mile and block the ip address of the client who tried to login as root. I currently have denyhosts and fail2ban setup and working, can I use denyhosts and or fail2ban to block the ip addresses of those who try to login as root?
Copy this code into a new file /etc/fail2ban/filter.d/sshd-root.conf:
PLEASE BE AWARE that you may have to edit the failregex to accurately identify failing root login attempts - use:
to test that it identifies the correct log entries.
Then you need to edit your jail.local to make use of the new filter - add something like:
Obviously you should adjust these values according to your needs. The settings above will drop all incoming packets from the offending IP address after three attempts to log on as root, and will release the IP again after one week.
Depending on your distribution, edit
/etc/fail2ban/jail.conf
Update the[ssh]
section to show something like thisChange the parameters as required. It won't specifically block root, but every attempt that fails. Be careful with
maxretry
and thebantime
. If you fail with your own password, whilemaxtretry
set to low, you block yourself for thebantime
. Restart fail2ban.I wouldn't try to block the IP forever as a lot of attempts come from dynamic IPs which could block some legitim users at a later point of time.
(Some distributions offer a jail.options file for your modifications. This is the preferred place to put your changes to as it shouldn't be affected by updates overwriting the conf.)
Since the default
/etc/fail2ban/filter.d/sshd.conf
already has a regex for AllowUsers and DenyUsers...The following will:
exampleusername
from external IPsroot
or any connections on local network (192.168.0.*)The line `/etc/ssh/sshd_config':
And in
/etc/fail2ban/jail.conf
:How did you block ssh logins? /bin/false or the sshd_config DenyUsers option?
I can't think of an answer out of my head, but IIRC denyhosts parses the log file, so just see if you get a failed entry in the log file after some one tries to log in to root with it disabled