Protect SSH on CentOS 8 with Fail2Ban

Install Fail2Ban

  1. Enable and install the EPEL repository
    # yum -y install epel-release
  2. Install Fail2Ban
    # yum -y install fail2ban
  3. Configure Fail2Ban (See Below)
  4. Enable and start Fail2Ban
    # systemctl enable fail2ban && systemctl start fail2ban

Configure Fail2Ban

  1. Edit /etc/fail2ban/jail.local
    # vim /etc/fail2ban/jail.local
  2. Update/append as follows
    # Ban IP/hosts for 24 hour ( 24h*3600s = 86400s):
    bantime = 86400
    # An ip address/host is banned if it has generated "maxretry" during the last "findtime" seconds.
    findtime = 600
    maxretry = 3
    # "ignoreip" can be a list of IP addresses, CIDR masks or DNS hosts. Fail2ban
    # will not ban a host which matches an address in this list. Several addresses
    # can be defined using space (and/or comma) separator. For example, add your 
    # static IP address that you always use for login such as
    #ignoreip = ::1
    # Call iptables to ban IP address
    banaction = iptables-multiport
    # Enable sshd protection
    enabled = true

View Fail2Ban Status

Run the following commands to view banned IPs

# fail2ban-client status
# fail2ban-client status sshd

Extend XenServer Linux Guest LVM Drive

  1. Create a snapshot of the VM to revert back if needed
  2. Shut down the VM
  3. In XenCenter, select the VM and click the Storage tab
  4. Select the disk and click Properties
  5. Enter the new size under Size and Location
  6. Power on the VM
  7. Verify the additional storage is available
    # fdisk -l
  8. Edit the expanded drive with fdisk
    # fdisk /dev/xvda
  9. View the existing partitions by typing ‘p’
  10. Create a new partition by typing ‘n’
  11. If there are less than 4 partitions, select primary.  Otherwise, select extended.
  12. Press Enter twice to use all of the free space
  13. Type ‘p’ to view the new table
  14. Type ‘t’ to change the partition type of the new partition to type ‘8e’
  15. Once the partition table looks correct, type ‘w’ to save and quit
  16. Reboot the VM
  17. Create a new LVM physical volume on the new partition
    # pvcreate /dev/xvda4
  18. Extend the volume group to include the new PV
    # vgextend vg_system /dev/xvda4
  19. Extend the logical volume to include the remaining free space in the VG
    # lvextend -l+100%FREE /dev/mapper/vg_system-lv_root
  20. Resize the filesystem to fill the free space in the LV
    # resize2fs /dev/mapper/vg_system-lv_root
  21. Verify the additional space is available
    # df -h

Swap File Creation

This page explains how to create and use a file-based swap in Linux.  This will generally be used in VMware and Xen images to simplify the images.

  1. Type the following command to create a 512MB swap file (1024*512 = 524288 block size – multiply to generate a bigger swap file)
    $ sudo dd if=/dev/zero of=/swap.file bs=1024 count=524288
  2. Set up a linux swap area
    $ sudo mkswap /swap.file
  3. Activate the swap space immediately
    $ sudo swapon /swap.file
  4. To active the swap space after a system reboot, and an entry to /etc/fstab.  Open this file using a text editor such as vim.
    $ sudo vim /etc/fstab
  5. Append the following line
    /swap.file              swap                    swap    defaults        0 0
  6. Verify swap is activated
    $ free -m