Dynamic SSH Banner Generation on CentOS

Creating a dynamic SSH banner can provide users with useful information about the system they are accessing. This article explains how to set up a script on a CentOS server that automatically updates the SSH banner with current system information, including hostname, CPU, RAM, and OS version, etc…

Step-by-Step:

  1. Script Creation:

    • Create a script named generate_banner.sh. This script will gather system information and write it to a file used as the SSH banner.
  2. Script Content:

 #!/bin/bash
{
  format="|%-20s %s\n"
  printf "$format" "Host:" "$(hostname)"
  printf "$format" "CPU:" "$(nproc) cores"
  printf "$format" "RAM:" "$(free -h | awk '/^Mem:/ {print $2}')"
  location=$(curl -s http://ip-api.com/line?fields=city,country | tr '\n' ' ')
  printf "$format" "Location:" "$location"
  printf "$format" "OS:" "$(cat /etc/redhat-release)"
  internal_ip=$(hostname -I | cut -d' ' -f1)
  printf "$format" "Internal IP:" "$internal_ip"
  external_ip=$(curl -s https://vm4it.com/ip.php)
  printf "$format" "External IP:" "$external_ip"

} > /etc/ssh/dynamic_banner.txt
  • This script uses printf for formatted output, ensuring a neat tabular structure.
  1. Permissions and Execution:

    • Make the script executable: chmod +x generate_banner.sh.
    • Set up a cron job to run the script regularly: 0 * * * * /etc/ssh/generate_banner.sh.
  2. SSH Configuration:

    • Update the SSH configuration to use the generated banner: Banner /etc/ssh/dynamic_banner.txt.
    • Restart the SSH service.
  3. Testing:

    • After the script runs, connect to the server via SSH to view the updated banner.

This approach simplifies system administration by providing essential information directly in the SSH banner. It enhances the user experience and can be customized further based on specific needs.