Make systemd change hostname without logging out
by JuventusFC from LinuxQuestions.org on (#5NPVD)
I have a systemd service that reads some environment values (that may change at startup) and set the hostname accordingly.
The service at the moment is a mix of several tries
Code:[Unit]
Description=Hostname Setting Service
After= read_variables.service
Requires= read_variables.service network-pre.service
Before= systemd-hostnamed.service network-pre.service
DefaultDependencies=false
[Service]
ExecStart=/bin/bash -c "echo name$VARIABLE > /etc/hostname; systemctl start systemd-hostnamed"
StandardOutput=journal+console
EnvironmentFile=/folder/environment_output.txt
Type=oneshot
[Install]
WantedBy=multi-user.targetThe problem is this one:
Code:at n-1-th startup, environment variables are set at n-1-th value
reboot
n-th startup
n-1-th values are read in /etc/hostname and then hostname is set to name@n-1
correct n-th values are read with read_variables.service and put in environment_output
correct hostname is set in /etc/hostname
But hostname is not modified on the fly so I will have hostname@n-1 instead of hostname@nSo my question is how to run these scripts before systemd prints the line:
systemd[1]: Set hostname to XYZ
The service at the moment is a mix of several tries
Code:[Unit]
Description=Hostname Setting Service
After= read_variables.service
Requires= read_variables.service network-pre.service
Before= systemd-hostnamed.service network-pre.service
DefaultDependencies=false
[Service]
ExecStart=/bin/bash -c "echo name$VARIABLE > /etc/hostname; systemctl start systemd-hostnamed"
StandardOutput=journal+console
EnvironmentFile=/folder/environment_output.txt
Type=oneshot
[Install]
WantedBy=multi-user.targetThe problem is this one:
Code:at n-1-th startup, environment variables are set at n-1-th value
reboot
n-th startup
n-1-th values are read in /etc/hostname and then hostname is set to name@n-1
correct n-th values are read with read_variables.service and put in environment_output
correct hostname is set in /etc/hostname
But hostname is not modified on the fly so I will have hostname@n-1 instead of hostname@nSo my question is how to run these scripts before systemd prints the line:
systemd[1]: Set hostname to XYZ