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.

1 Like

Where do you usually store a script like this on the filesystem?

Thank you!

Hi @jackspace ,

To store a script for dynamic SSH banner , it is common to place it in a directory such as /etc/ssh/ or /usr/local/bin/. Ensure the script is executable (chmod +x generate_banner.sh) and set up a cron job to run it periodically. Update the SSH configuration to reference the banner file generated by the script and restart the SSH service. In my example the script is located in /etc/ssh/ as you can see in the article.

Let me know if you have more questions.