LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 08-24-2021, 05:01 AM   #1
JuventusFC
LQ Newbie
 
Registered: Jul 2018
Posts: 15

Rep: Reputation: Disabled
Make systemd change hostname without logging out


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.target
The 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@n
So my question is how to run these scripts before systemd prints the line:

systemd[1]: Set hostname to XYZ
 
Old 08-24-2021, 08:11 AM   #2
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,767

Rep: Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765
WHY do you want to dynamically reset the hostname? What purpose does this serve?
 
Old 08-24-2021, 08:44 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
You can change hostname on the fly just not from editing /etc/hostname.
hostnamectl can change hostname on the fly.

As posted why do you want to change hostnames but I would guess this is for DHCP / DNS.
 
1 members found this post helpful.
Old 08-24-2021, 08:55 AM   #4
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 5,767

Rep: Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765Reputation: 2765
At this point I have to point out that on the server the hostname serves several purposes and the host can have only one. On the network the host name can be totally different, or it can even have more than one, and represents the interface it has onto the network. DHCP clients allow you to specify the hostname to be used on the network, and it need not be the same as the name by which the host knows itself.

I would really like to understand the purpose, so we can address the need instead of only the literal question.
 
Old 08-25-2021, 04:25 AM   #5
JuventusFC
LQ Newbie
 
Registered: Jul 2018
Posts: 15

Original Poster
Rep: Reputation: Disabled
Thank you, I have resolved using hostnamectl set-hostname script launched on a script referred by a systemd service, and adding some SELinux rules.

The need for such a task was due to a job assignment in order to know on ssh prompt the IP of the macchine connected to, even if It was a bit redundant with the fact that the IP can be known with ifconfig, but I had to do it so...
 
  


Reply

Tags
hostname change, services, systemd



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
systemd hostnamectl changes hostname without sudo genogebot Linux - Software 15 12-17-2016 04:07 AM
LXer: Is systemd as bad as boycott systemd is trying to make it? LXer Syndicated Linux News 0 09-03-2014 05:50 PM
HOSTNAME = hostname -> HOSTNAME: command not found ? thomas2004ch Linux - Software 2 08-26-2013 08:25 PM
after hostname change, syslogd still output previous hostname to messages file tiger2000 Linux - General 3 07-22-2009 09:36 PM
using red-carpet without logging out and logging as root. packman Linux - Software 1 12-09-2002 02:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 02:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration