From 0a77bc00037c8a2a6b00eb9f807a0bda1f3f2346 Mon Sep 17 00:00:00 2001 From: Gabriel Carneiro Date: Mon, 18 Nov 2024 10:32:21 -0300 Subject: [PATCH] add ldap-hostname script and service --- usr/bin/ldap-hostname | 11 +++++++++++ usr/lib/systemd/system/ldap-hostname.service | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100755 usr/bin/ldap-hostname create mode 100644 usr/lib/systemd/system/ldap-hostname.service diff --git a/usr/bin/ldap-hostname b/usr/bin/ldap-hostname new file mode 100755 index 0000000..016de8b --- /dev/null +++ b/usr/bin/ldap-hostname @@ -0,0 +1,11 @@ +#!/bin/sh + +IP=$(hostname -I | awk '{print $1}') +HOSTNAME=$(getent hosts | grep $IP | awk '{print $2}') + +if [ -n "$HOSTNAME" ]; then + hostnamectl set-hostname "$HOSTNAME" + echo "Hostname set to $HOSTNAME" +else + echo "Hostname not found for IP $IP" +fi diff --git a/usr/lib/systemd/system/ldap-hostname.service b/usr/lib/systemd/system/ldap-hostname.service new file mode 100644 index 0000000..143f656 --- /dev/null +++ b/usr/lib/systemd/system/ldap-hostname.service @@ -0,0 +1,10 @@ +[Unit] +Description=Set hostname based on IP from LDAP +After=network.target + +[Service] +Type=oneshot +ExecStart=/usr/bin/ldap-hostname + +[Install] +WantedBy=multi-user.target