Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-24-2021, 05:01 AM
|
#1
|
LQ Newbie
Registered: Jul 2018
Posts: 15
Rep: 
|
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
|
|
|
08-24-2021, 08:11 AM
|
#2
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,342
|
WHY do you want to dynamically reset the hostname? What purpose does this serve?
|
|
|
08-24-2021, 08:44 AM
|
#3
|
Moderator
Registered: Aug 2002
Posts: 26,911
|
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.
|
08-24-2021, 08:55 AM
|
#4
|
LQ Guru
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS, Manjaro
Posts: 6,342
|
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.
|
|
|
08-25-2021, 04:25 AM
|
#5
|
LQ Newbie
Registered: Jul 2018
Posts: 15
Original Poster
Rep: 
|
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... 
|
|
|
All times are GMT -5. The time now is 02:33 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|