{"id":2489,"date":"2023-09-12T23:28:21","date_gmt":"2023-09-12T22:28:21","guid":{"rendered":"https:\/\/bornoe.org\/blog\/?p=2489"},"modified":"2023-11-30T02:06:21","modified_gmt":"2023-11-30T01:06:21","slug":"basic-fail2ban-commands","status":"publish","type":"post","link":"https:\/\/bornoe.org\/blog\/2023\/09\/basic-fail2ban-commands\/","title":{"rendered":"Basic Fail2ban commands"},"content":{"rendered":"\n<p>Fail2ban provides a command-line interface (CLI) that allows you to perform various tasks related to monitoring and managing banned IP addresses, jails, and the Fail2ban service. Here are some commonly used Fail2ban day-to-day management commands collected in a mini cheat sheet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The &#8220;sudo&#8221; prefix<\/h2>\n\n\n\n<p>Your user must have super user access to run most of the commands. By default, the listed commands include the &#8220;sudo&#8221; prefix (short for \u201csuper user do\u201d,) but this is not always needed depending on your system configuration. If you are logged in as &#8216;root&#8217; you can most likely omit this prefix.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List of commonly used commands<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Check Fail2ban status<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status fail2ban<\/code><\/pre>\n\n\n\n<p>Displays the overall status of Fail2ban.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Start Fail2ban<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start fail2ban<\/code><\/pre>\n\n\n\n<p>No output if successful. Fail2ban is active.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Stop Fail2ban<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl stop fail2ban<\/code><\/pre>\n\n\n\n<p>No output if successful. Fail2ban is stopped.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Restart Fail2ban<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart fail2ban<\/code><\/pre>\n\n\n\n<p>No output if successful. Fail2ban is restarted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reload Fail2ban configuration without restarting<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client reload<\/code><\/pre>\n\n\n\n<p>The Fail2ban configuration is reloaded.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enable Fail2ban to start on boot<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable fail2ban<\/code><\/pre>\n\n\n\n<p>Fail2ban is set to start on boot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Disable Fail2ban from starting on boot<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl disable fail2ban<\/code><\/pre>\n\n\n\n<p>Fail2ban is disabled from starting on boot.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">View a list of all jails<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client status<\/code><\/pre>\n\n\n\n<p>Lists all active Fail2ban jails and their status.<\/p>\n\n\n\n<p>Example output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Status<\/code>\n|- Number of jail: 1\n`- Jail list: sshd<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">List all banned IP addresses in all jails<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client banned<\/code><\/pre>\n\n\n\n<p>Lists all IP addresses banned for each existing jail.<\/p>\n\n\n\n<p>Example output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[{'sshd': ['192.0.2.1', '198.51.100.1']}]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Check the status of a specific jail (e.g., sshd)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client status &lt;JAIL&gt;<\/code><\/pre>\n\n\n\n<p>Shows the status of a specific Fail2ban jail, such as SSH:<\/p>\n\n\n\n<p><code>sudo fail2ban-client status sshd<\/code><\/p>\n\n\n\n<p>Example output for the jail sshd<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Status for the jail: sshd\n|- Filter\n|  |- Currently failed:\t1\n|  |- Total failed:\t23829\n|  `- File list:\t\/var\/log\/auth.log\n`- Actions\n   |- Currently banned:\t2\n   |- Total banned:\t2569\n   `- Banned IP list:\t192.0.2.1 198.51.100.1<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Manually ban an IP address<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client set &lt;JAIL&gt; banip &lt;IP&gt;<\/code><\/pre>\n\n\n\n<p>The specified IP is banned and included in the specified jail. E.g., if you want to ban an IP from connect through SSH:<\/p>\n\n\n\n<p><code>sudo fail2ban-client set sshd banip 192.0.2.1<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Manually unban an IP address<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client set &lt;JAIL&gt; unbanip &lt;IP&gt;<\/code><\/pre>\n\n\n\n<p>The specified IP in the specified jail is unbanned. E.g., if you want to unban an IP and allow it to connect through SSH:<\/p>\n\n\n\n<p><code>sudo fail2ban-client set sshd unbanip 192.0.2.1<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Display the current Fail2ban version<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client version<\/code><\/pre>\n\n\n\n<p>Displays the installed Fail2ban version.<\/p>\n\n\n\n<p>Example output<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0.11.2<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Check fail2ban.log<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo tail \/var\/log\/fail2ban.log<\/code><\/pre>\n\n\n\n<p>Displays the 10 latest entries in the log file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Get help and information about all Fail2ban commands<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo fail2ban-client --help<\/code><\/pre>\n\n\n\n<p>Displays the man page for Fail2ban with details about all Fail2ban commands and options.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fail2ban provides a command-line interface (CLI) that allows you to perform various tasks related to monitoring and managing banned IP addresses, jails, and the Fail2ban service. Here are some commonly used Fail2ban day-to-day management commands collected in a mini cheat sheet. The &#8220;sudo&#8221; prefix Your user must have super user access to run most of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2499,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,17],"tags":[],"class_list":["post-2489","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-gnu-linux","category-linux"],"_links":{"self":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/comments?post=2489"}],"version-history":[{"count":15,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2489\/revisions"}],"predecessor-version":[{"id":2593,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/posts\/2489\/revisions\/2593"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/media\/2499"}],"wp:attachment":[{"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/media?parent=2489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/categories?post=2489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bornoe.org\/blog\/wp-json\/wp\/v2\/tags?post=2489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}