TransWikia.com

Revert/Prevent hostname change

Ask Ubuntu Asked by user3240383 on December 23, 2021

This is a shot in the dark here …

I’m using a service called Strato-Server-Cloud which is basicaly a rudimentary version of AWS or Google-Compute. They changed their interface and/or engine recently. Ever since, all new Ubuntu 16 instances I create have the problem of thier hostname being reverted on reboot.

Here is what I usually do:

/etc/hostname

www.example.com

/etc/hosts

127.0.0.1 www.example.com www
[...]

On Ubuntu 16 additionally:

hostnamectl set-hostname www

After doing the steps above:

hostname -> www
hostname -f -> www.example.com

hostnamectl

Static hostname: www
Icon name: computer-vm
Chassis: vm
Machine ID: [ID]
Boot ID: [ID]
Virtualization: microsoft
Operating System: Ubuntu 16.04.1 LTS
Kernel: Linux 4.4.0-59-generic
Architecture: x86-64

After reboot rebooting

hostname -> XXX
hostname -f -> XXX.strato.net

hostname-ctl

Static hostname: www
Transient hostname: XXX
Icon name: computer-vm
Chassis: vm
Machine ID: [ID]
Boot ID: [ID]
Virtualization: microsoft
Operating System: Ubuntu 16.04.1 LTS
Kernel: Linux 4.4.0-59-generic
Architecture: x86-64

Note the transient hostname

Oddly enough the hosts and hostname files are intact.

I Also tried changing /etc/init/hostname.conf to the following

description     "set system hostname"

start on startup

task
exec hostname -b -F /etc/hostname.mod; full=$(cat /etc/hostname.mod);short=(${full//./ }); hostnamectl set-hostname $short

I put the hostname also in /etc/hostname.mod to ensure there is no overwriting going on. Executing the above exec-statement manually fixes everything but it doesn’t seem to be effective during the boot-sequence …

Unfortunately I’m pretty lost here … I would assume the /etc/init/hostname.conf gets executed during boot but then the hostname gets overriden by another script? Has anyone ever had such an issue and knows how to fix it?
I guess I could look into some linux version of a file-spy that documents what processes touch which files and find the offending script that way … This does sound like a lot of work however …

If someone could at least point out the livecycle of the hostname, that is the steps of how it is determined and where one could hook into this would already help me a lot.

Thanks in advance for your time and effort

Edit

I guess it would also help to explain why this is bad 🙂 OpenLDAP refuses to work correctly in STARTTLS mode if the hostname isn’t properly configured. Since I never had the problem of not being able to change the hostname I don’t know what other service might malefunction as well. A minimal solution to the problem would therefor have to become effective before service like OpenLDAP are started.

3 Answers

This answer may help those who are trying to make a liveCD.

If you are making a liveCD which is booted by isolinux+casper, you need to be careful. Installing casper on your liveCD will change the content of initrd. As the result, when the system boots, it will primarily use configuration of casper to generate hostname (or something else) rather than read /etc/hostname.

I refer to casper's source code and find following lines

# Allow to override USERNAME and HOST based on media information
# make it skipable by setting FLAVOUR= in casper.conf
if [ -f /cdrom/.disk/info ] && [ -z "$FLAVOUR" ]; then
    FLAVOUR="$(cut -d' ' -f1 "/cdrom/.disk/info" 2>/dev/null | tr '[A-Z]' '[a-z]')" || FLAVOUR=
    if [ -n "$FLAVOUR" ]; then
        HOST=$FLAVOUR
        USERNAME=$FLAVOUR
        export HOST USERNAME
        sed -i "s,USERNAME=.*,USERNAME="$FLAVOUR",g; s,HOST=.*,HOST="$FLAVOUR",g" /etc/casper.conf
    fi
fi

So, you can make a ".disk" folder or directly change /etc/casper.conf to change hostname.

vim gen-image.sh
================
#!/bin/bash
cd IMAGE_DIR
rm -rf .disk
mkdir .disk
touch base_installable
echo "full_cd/single" >cd_type
echo "YOUR_CUSTOM_HOST_NAME" >info
echo "YOUR_CUSTOM_URL" >release_nots_url
cd ..
# GENERATE IMAGE HERE

Answered by CarryOrange on December 23, 2021

Old post, I know, but this helped me solve the same problem. I'm updating this for future search engine users experiencing the same problem.

It wasn't hostnamectl's fault this time.

If you're trying to change the hostname on a VPS server and it just doesn't want to change - your VM may be getting its hostname from the host machine/hypervisor.

In my case, I had to log into the VPS provider dashboard and change the hostname there.

Answered by Taco Bob on December 23, 2021

For computers that get their IP address via dchp, the default client file, /etc/dhcp/dhclient.conf, includes asking for what the host-name should be in the request:

request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

You can remove that from the request:

request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

I am not sure if: There might be unintended side effects; The domain-name should also be deleted from the request.

Answered by Doug Smythies on December 23, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP