From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Sshguard and the translation is 30% complete.
Other languages:
English • ‎español • ‎italiano • ‎русский • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

Warning: Display title "sshguard" overrides earlier display title "Sshguard".


sshguard è un sistema di prevenzione delle intrusioni che analizza i log del server, determina le attività dannose e utilizza il firewall di sistema per bloccare gli indirizzi IP delle connessioni malevole. sshguard è scritto in C quindi non necessita di un interprete.

Come funziona

sshguard è un semplice demone che setaccia in continuazione uno o più file di log. Esso analizza i log che i demoni inviano in caso di tentativi di accesso non riusciti e poi blocca ogni ulteriore tentativo da quelle connessioni aggiornando il firewall del sistema.

Rispetto a quello che può suggerire il nome, sshguard non funziona solo con log di SSH. Esso supporta anche molti sistemi di mail nonché alcuni FTP. Una lista completa di servizi supportati è disponibile sul sito



installa app-admin/sshguard:

root #emerge --ask app-admin/sshguard

Also make sure that net-firewall/iptables is installed and used as the system firewall. At the time of writing, sshguard does not yet support net-firewall/nftables.

root #emerge --ask net-firewall/iptables

Maggiori informazioni riguardo l'uso e la configurazione di IPtables possono essere trovati nel IPtables article.


Preparare il firewall

When sshguard blocks any malicious users (by blocking their IP addresses), it will use the sshguard chain.

Prepare the chain, and make sure it is also triggered when new incoming connections are detected:

root #iptables -N sshguard
root #iptables -A INPUT -j sshguard

Osservare i file log

The basic idea behind sshguard is that the administrator passes on the log file(s) to watch as options to the application - there is no native sshguard configuration file.

On Gentoo, the options can be best configured in the /etc/sshguard.conf file:

FILE /etc/sshguard.confConfiguring sshguard to read /var/log/messages and /var/log/auth.log
# Full path to backend executable (required, no default)

# Space-separated list of log files to monitor. (optional, no default)
FILES="/var/log/messages /var/log/auth.log" 

# How many problematic attempts trigger a block
# Blocks last at least 24 hours (60480 seconds)
# Track IP addresses for 24 hours (60480 seconds)

# IPv6 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 128)
# IPv4 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 32)

# Full path to PID file (optional, no default)

Make sure that the log files are accessible for the runtime user that sshguard uses.



Have sshguard be started by default by adding it to the default runlevel, and then start it:

root #rc-update add sshguard default
root #rc-service sshguard start

Blacklisting hosts

With the blacklisting option after a number of abuses the IP address of the attacker or a IP subnet will be blocked permanently. The blacklist will be loaded at each startup and extended with new entries during operation. sshguard inserts a new address after it exceeded a threshold of abuses.

Blacklisted addresses are never scheduled to be released (allowed) again.

To enable blacklisting, create an appropriate directory and file:

root #mkdir -p /var/lib/sshguard
root #touch /var/lib/sshguard/blacklist.db

While defining a blacklist it is important to exclude trusted IP networks and hosts in a whitelist.

To enable whitelisting, create an appropriate directory and file:

root #mkdir -p /etc/sshguard
root #touch /etc/sshguard/whitelist

The whitelist has to include the loopback interface, and should have at least 1 IP trusted network f.e.

FILE /etc/sshguard/whitelistWhitelisting trusted networks
The entry has to be adjusted to fit the own needs.

Add the BLACKLIST_FILE and WHITELIST_FILE file to the configuration:

FILE /etc/conf.d/sshguardConfiguring sshguard to blacklist abusers




# Add following lines

Restart the sshguard daemon to have the changes take effect:

root #/etc/init.d/sshguard restart

Risoluzione dei problemi

File '/var/log/auth.log' vanished while adding!

When starting up, sshguard reports the following error:

CODE Error message when trying to add a monitor for /var/log/auth.log
Sep 23 03:39:11 sshguard[64933]: File '/var/log/auth.log' vanished while adding!

Such an error (the file path itself can be different) occurs when the target file is not available on the system. Make sure that it is created, or update the sshguard configuration to not add it for monitoring.

On a syslog-ng system with OpenRC, the following addition to syslog-ng.conf can suffice:

FILE /etc/syslog-ng/syslog-ng.confcreating auth.log file
log { source(src); destination(messages); };
log { source(src); destination(console_all); };
destination authlog {file("/var/log/auth.log"); };
filter f_auth { facility(auth); };
filter f_authpriv { facility(auth, authpriv); };
log { source(src);  filter(f_authpriv);  destination(authlog);  };

Reload the configuration for the changes to take effect:

root #rc-service syslog-ng reload

Vedi anche

  • Iptables, per installare e configurare iptables su Gentoo

Risorse esterne

La documentazione sshguard fornisce tutte le informazioni necessarie per ottimizzare ulteriormente l'applicazione.