From 7b5f72f0bede72627f46218c8574395e53a7ac70 Mon Sep 17 00:00:00 2001 From: David Brown Date: Tue, 28 May 2024 13:41:35 +0100 Subject: [PATCH 1/2] using hostnamectl --- .../scripts/change-hostname | 26 ++++--------------- 1 file changed, 5 insertions(+), 21 deletions(-) diff --git a/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname b/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname index 5ccb8255f..31e77a075 100755 --- a/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname +++ b/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname @@ -8,11 +8,6 @@ set -u # Exit on first error. set -e -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" -readonly SCRIPT_DIR -# shellcheck source=lib/markers.sh -. "${SCRIPT_DIR}/lib/markers.sh" - readonly VALID_HOSTNAME_PATTERN='^[-0-9A-Za-z]+$' print_help() { @@ -56,19 +51,8 @@ if ! [[ "${NEW_HOSTNAME}" =~ $VALID_HOSTNAME_PATTERN ]] \ exit 1 fi -# Remove any existing marker sections from the `/etc/hosts` file. -"${SCRIPT_DIR}/strip-marker-sections" /etc/hosts - -# Populate new entry to `/etc/hosts`. -# Note that the first matching entry takes precedence, which is why we prepend -# our new entry. -OLD_ETC_HOSTS="$( /etc/hosts - -# Populate new hostname to `/etc/hostname`. -# Note that the value must be newline-terminated, see: -# https://manpages.debian.org/stretch/systemd/hostname.5.en.html -printf "%s\n" "${NEW_HOSTNAME}" > /etc/hostname +# We use `hostnamectl set-hostname xyz` instead of `hostnamectl hostname xyz` +# because `set-hostname` is backwards compatible with the Bullseye version of +# `hostnamectl`. `hostnamectl hostname xyz` is only supported on the Bookworm +# version. +hostnamectl set-hostname "${NEW_HOSTNAME}" From 478694541c4fa087854e4c781de49285215e79de Mon Sep 17 00:00:00 2001 From: David Brown Date: Tue, 28 May 2024 15:27:42 +0100 Subject: [PATCH 2/2] replace markers and populating /etc/hosts --- .../scripts/change-hostname | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname b/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname index 31e77a075..834e4d01f 100755 --- a/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname +++ b/debian-pkg/opt/tinypilot-privileged/scripts/change-hostname @@ -8,6 +8,11 @@ set -u # Exit on first error. set -e +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +readonly SCRIPT_DIR +# shellcheck source=lib/markers.sh +. "${SCRIPT_DIR}/lib/markers.sh" + readonly VALID_HOSTNAME_PATTERN='^[-0-9A-Za-z]+$' print_help() { @@ -51,6 +56,18 @@ if ! [[ "${NEW_HOSTNAME}" =~ $VALID_HOSTNAME_PATTERN ]] \ exit 1 fi +# Remove any existing marker sections from the `/etc/hosts` file. +"${SCRIPT_DIR}/strip-marker-sections" /etc/hosts + +# Populate new entry to `/etc/hosts`. +# Note that the first matching entry takes precedence, which is why we prepend +# our new entry. +OLD_ETC_HOSTS="$( /etc/hosts + # We use `hostnamectl set-hostname xyz` instead of `hostnamectl hostname xyz` # because `set-hostname` is backwards compatible with the Bullseye version of # `hostnamectl`. `hostnamectl hostname xyz` is only supported on the Bookworm