[ Lab 1.2 ] Automated VM Renaming - smitja21/group-a-oe2 GitHub Wiki

[!NOTE] Ticket #1: Automated VM Renaming

Conceptual Questions

  • Q1.1: What is a hostname? Why is it important that hostnames are unique within a network?

The hostname is a unique name assigned to your machine on the network. It's important they are unique to avoid conflicts and that each device on the network can be distinctly identified.

  • Q1.2: What is the difference between a static hostname and a transient hostname in Linux systemd?

A static hostname is the user configured hostname which is stored in /etc/hostname. This has the highest priority. While the transient hostname is the fallback value received from network configuration and is used if a static hostname has not been set.

  • Q1.3: Which configuration file stores the system hostname, and what command can be used to display the current hostname without any arguments?

The config is stored in /etc/hostname, the command "hostname" can be used to view the hostname.


  1. Verify Connection

Successfully connected to mgmt-a via ssh

Successfully connected to app-a via ssh

Successfully connected to db-a via ssh

Successfully connected to backup-a via ssh

  • Q1.4: What error message would you see if the SSH service is not running on a target VM? How would you diagnose and resolve it?

If the SSH service wasn't running, you would get a "Connection refused" on port 22. Verify you can ping the server to check the server is online. Then view the ssh status "sudo systemctl status ssh" or "sudo systemctl status sshd" to see if the service is running. If service isn't running run the following commands:

sudo systemctl start ssh sudo systemctl enable ssh


  1. Manually rename VM1 to understand the hostname configuration process.

1.3 Configure Local Hostname Resolution

  • Q1.5: Why is it necessary to also update /etc/hosts after renaming a host? What could break if you skip this step?

To allow the system to resolve its own hostname independently of DNS. If it may lead to incorrect local name resolution.


1.3 - Creating Script:

The following script was helped with genAI - Chatgpt, original prompt.

I want to create a bash script that does something similar to this, below is only psuedo code:

if (ip a) = 52.187.225.139
 set hostname = mgmt-a
 
elif (ip a) = 23.101.222.60
 set hostname = app-a
 
 elif (ip a) = 20.53.246.115
  set hostname = db-a
  
  elif (ip a) = 20.5.77.85
  set hostname = backup-a
  
  Using the command:
  
  
  
  
  using sudo hostnamectl set hostname
  
  
  make sure there is also a entry in /etc/hosts
  

But because the internal ip is different from external we instead went off the current hostname. This is more secure to as we're not exposing the public ip.

to create the script:

nano rename_vms.sh

Then change permissions to be able to run the script

chmod u+x rename_vms.sh

Then run the script:

sudo ./rename_vms.sh

#!/usr/bin/env bash
set -euo pipefail

# Ensure script is run as root
if [ "$EUID" -ne 0 ](/smitja21/group-a-oe2/wiki/-"$EUID"--ne-0-); then
    echo "This script must be run as root (use sudo)."
    exit 1
fi

CURRENT_HOSTNAME="$(hostname)"
NEW_HOSTNAME=""

case "${CURRENT_HOSTNAME}" in
    labvm-1)
        NEW_HOSTNAME="mgmt-a"
        ;;
    labvm-2)
        NEW_HOSTNAME="app-a"
        ;;
    labvm-3)
        NEW_HOSTNAME="db-a"
        ;;
    labvm-4)
        NEW_HOSTNAME="backup-a"
        ;;
    *)
        echo "Hostname ${CURRENT_HOSTNAME} not recognized. Exiting."
        exit 1
        ;;
esac

echo "Current hostname: ${CURRENT_HOSTNAME}"
echo "Setting hostname to: ${NEW_HOSTNAME}"

hostnamectl set-hostname "${NEW_HOSTNAME}"

# Append to /etc/hosts only if entry does not already exist
if ! grep -qE "^[:space:](/smitja21/group-a-oe2/wiki/:space:)*127\.0\.1\.1[:space:](/smitja21/group-a-oe2/wiki/:space:)+${NEW_HOSTNAME}([:space:](/smitja21/group-a-oe2/wiki/:space:)|\$)" /etc/hosts; then
    echo "Adding ${NEW_HOSTNAME} to /etc/hosts"
    echo "127.0.1.1    ${NEW_HOSTNAME}" >> /etc/hosts
else
    echo "/etc/hosts already contains an entry for ${NEW_HOSTNAME}"
fi

echo "Hostname successfully configured."

Q1.6 Document the output of your verification loop here. Include screesnhots. Does every VM show the expected hostname?

This was repeated for the 3 vms which didn't have the hostname manually set

Q1.7 The script uses set -euo pipfail at the top. Explain what each flag (-e, -u, -o pipefail) does and why using them is considered good practice.

-e will exit, -u undefined variables will error and exit the application, -o pipeline will return first failing command error code

Q1.8 What security concern arises from storing a plaintext password in a Bash script? Suggest two alternative, more secure approaches.

Anyone can access the script

Use a ssh keys or ask for the password to be entered during execution because it's not hardcoded.

CMS Puppet Ansible