Update Host names
  • 10 Oct 2024
  • 1 読む分
  • PDF

Update Host names

  • PDF

The content is currently unavailable in Ja - 日本語. You are viewing the default English version.
記事の要約

Use this script to remotely update the Host names on Linux machines.

  1. Copy and save the contents below to a UTF-8 editor like notepad++ OR Sublime Text in Windows or gedit in Ubuntu.

    1. If you are using notepad++ then use the bottom right panel to change the type to Unix (LF).

  2. Or click here to download the file.

    #!/bin/bash
    
    # Check if the script is run with root privileges
    if [ "$EUID" -ne 0 ]; then
      echo "Please run as root."
      exit 1
    fi
    
    # Get the current hostname
    current_hostname=$(hostname)
    
    # Check if the current hostname is empty or not
    if [ -z "$current_hostname" ]; then
      echo "Error: Unable to retrieve current hostname."
      exit 1
    fi
    
    # Define the new hostname based on the current hostname
    case $current_hostname in
      "please enter the current hostname of the machine 1")
        new_hostname="please enter new hostname you want set"
        ;;
      "please enter the current hostname of the machine 2")
        new_hostname="please enter new hostname you want set"
        ;;
      # Add more cases for additional hostnames
      *)
        # If the current hostname doesn't match any known cases, no changes are made
        echo "Hostname is not in the list. No changes made."
        exit 0
        ;;
    esac
    
    # Change the hostname in /etc/hostname
    echo "$new_hostname" > /etc/hostname
    
    # Change the hostname in /etc/hosts
    sed -i "s/127.0.1.1.*/127.0.1.1\t$new_hostname/" /etc/hosts
    
    # Notify the user to reboot for the changes to take effect
    echo "Hostname changed from $current_hostname to $new_hostname. Please reboot your system for the changes to take effect."
    
  3. In the script, replace the following placeholder:

    1. case $current_hostname in

       "please enter the current hostname of the machine 1")

    2. new_hostname="please enter new hostname you want set"

  4. The device will need to be rebooted once after publishing the script.

  5. Follow our guide to upload & publish the script using Scalefusion Dashboard.

Note:

  1. Some of the scripts and their contents are sourced from internet and yes, our new friend ChatGPT.

  2. Please validate the scripts on a test machine before deploying them on all your managed devices.

  3. Scalefusion has tested these scripts, however Scalefusion will not be responsible for any loss of data or system malfunction that may arise due to the usage of these scripts.


この記事は役に立ちましたか?