setup_hostname


"/home/yossef/notes/git/scripts//setup_hostname.md"

path: git/scripts//setup_hostname.md

- **fileName**: setup_hostname
- **Created on**: 2025-03-31 00:44:55

this script for channging hostname for user for slove problem if device

is slove or want to update the host for problems in commends

#!/usr/bin/env bash

ch_hostname() {
    HOSTNAME=$(uname -n) # getting the hostname
    cecho "your current hostname is ${HOSTNAME}" blue
    cecho "starting to change hostname to: $1" yellow
    sudo hostnamectl set-hostname $1 # changing hostname value
    if [[ $? != 0 ]]; then # check fi error happend
        cecho "error happend in changing hostname" 
        exit 0
    fi
    cecho "hostname changed to $1" green
    cecho "starting change /etc/hosts file" yellow

    # change the old hostname with the new hostname from user $1
    sudo sed -i "s/$HOSTNAME/$1/g" /etc/host 
    if [[ $? != 0 ]]; then
        cecho "error happend in changing /etc/hosts file" 
        exit 0
    fi

    cecho "/etc/hosts file changed" green
    cecho "hostname changed successfully" green
    # must restart your device after this operation 
    cecho "please restart your system to apply changes" green
}
ch_hostname $1

reference

Important

it's not for playing arround be carefully in using this

continue:./_install_yazi.md
before:./_install_virtualbox.md