Software & AppsOperating SystemLinux

How To Modify SSH Welcome Message to Include System IP Address

Ubuntu 5

When you log into a Linux server via SSH, you’re typically greeted with a welcome message. This message, known as the Message of the Day (MOTD), can be customized to display a variety of information. In this article, we’ll guide you through the process of modifying this message to include the system’s IP address.

Quick Answer

To modify the SSH welcome message to include the system’s IP address, you can either create a new script using the update-motd utility, install the landscape-common package, or create a custom MOTD file and configure SSH to display it upon login.

Understanding the SSH Welcome Message

The SSH welcome message is generated by a utility called update-motd in Linux. By default, it displays general information about the system, such as the Ubuntu version and the last login details. However, you can customize this message by adding scripts to the /etc/update-motd.d/ directory.

Modifying the SSH Welcome Message

Using the update-motd Utility

The easiest way to include the IP address in the SSH welcome message is to create a new script in the /etc/update-motd.d/ directory. Here are the steps:

  1. Create a new script file. You can name it 50-ip-address for example. The number at the beginning is used to determine the order in which the scripts are executed.
    sudo nano /etc/update-motd.d/50-ip-address
  2. Add the following content to the script file. This script uses the ifconfig command to display the IP address.
    #!/bin/bash
    echo "Your IP address is: $(ifconfig | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}')"
    The grep command is used to filter the output of ifconfig and the cut and awk commands are used to extract the IP address.
  3. Save the file and make it executable. This can be done with the chmod command:
    sudo chmod +x /etc/update-motd.d/50-ip-address
  4. Log out and log back in. The IP address should now be displayed in the SSH welcome message.

Using the landscape-common Package

Another way to include the IP address in the SSH welcome message is to install the landscape-common package. This package automatically adds system information, including IP addresses, to the MOTD.

To install the package, use the following command:

sudo apt-get install landscape-common

After installation, the IP address will be displayed in the SSH welcome message upon login.

Creating a Custom MOTD File

If you want more control over the SSH welcome message, you can create a custom MOTD file and configure SSH to display it upon login.

  1. Create a new file. You can name it /etc/motd-custom for example.
    sudo nano /etc/motd-custom
  2. Add the desired welcome message along with the IP address to the file. You can use the ifconfig or ip a command to display the IP address.
  3. Edit the SSH configuration file.
    sudo vi /etc/ssh/sshd_config
  4. Modify the configuration file. Uncomment or add the line PrintMotd yes to enable the display of the MOTD. Uncomment or add the line Banner /etc/motd-custom and set the path to your custom MOTD file.
  5. Save the file and restart the SSH service.
    sudo service ssh restart

The custom MOTD file will now be displayed upon SSH login, including the IP address.

Conclusion

By following the steps outlined in this article, you can easily modify the SSH welcome message to include the system’s IP address. Whether you choose to use the update-motd utility, the landscape-common package, or a custom MOTD file, you’ll be able to provide users with useful information upon login.

How can I check the current SSH welcome message on my Linux server?

To check the current SSH welcome message, you can open the /etc/motd file using a text editor such as nano or vi. Simply run the command sudo nano /etc/motd or sudo vi /etc/motd and the file will open, displaying the current message.

Can I customize the SSH welcome message with colors and formatting?

Yes, you can customize the SSH welcome message with colors and formatting. To do this, you can use ANSI escape codes in your script or custom MOTD file. For example, you can use \e[1;31m to set the text color to red, or \e[4m to underline the text. Make sure to reset the formatting at the end of your custom message using \e[0m.

How can I remove the IP address from the SSH welcome message?

If you no longer want to display the IP address in the SSH welcome message, you can simply delete or comment out the relevant script in the /etc/update-motd.d/ directory. Alternatively, if you are using a custom MOTD file, you can remove the line that displays the IP address.

Can I include additional system information in the SSH welcome message?

Yes, you can include additional system information in the SSH welcome message. If you are using the update-motd utility, you can create additional scripts in the /etc/update-motd.d/ directory to display the desired information. If you are using a custom MOTD file, you can add the necessary commands to retrieve the information and include it in the file.

How can I change the order of the scripts executed by `update-motd`?

The scripts in the /etc/update-motd.d/ directory are executed in alphabetical order. To change the order, you can rename the scripts by adding or modifying the numbers at the beginning of their filenames. Scripts with lower numbers will be executed first. Remember to update the numbers accordingly in the scripts themselves to maintain consistency.

Leave a Comment

Your email address will not be published. Required fields are marked *