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