3

I need to change the hostname of a system without rebooting it. Im running CentOS 7 and have the correct hostname in the /etc/hostname file but im still showing the old hostname at prompt. I know that when I reboot the system it will check the hostname file and apply it, but is there anyway for me to update that without rebooting? Here is some info from the command line:

[root@gandalf sysconfig]# cat network
NETWORKING=yes
GATEWAY=192.168.80.1
HOSTNAME="sauron.domain.com"
[root@gandalf sysconfig]# cd ..
[root@gandalf etc]# cat hostname
sauron
[root@gandalf etc]# 

Im unable to reboot this server anytime soon and some of my team is mixing up the server due to the hostname showing an older system name. Simply put: need prompt to show [user@sauron dir]# instead of [user@gandalf dir]#.

Google'd around for this but wasn't able to see a way to do this without the reboot. Thanks for your consideration!

  • Logout and login should work to update this variable. See my answer... – user34720 Jun 20 '17 at 19:49
3

You should be able to do this using the hostname command:

hostname -F /etc/hostname

After this change, the previous hostname will still show at your current prompt. To see the change without rebooting, enter a new shell. If you are using bash, type:

bash

Your new hostname should now be displayed.

  • The question refers to reloading the /etc/hostname file, not modifying the hostname via argument. – marc Jun 20 '17 at 19:12
  • @marc there is no way to "reload hosname" like using source command. the user will have to point to /etc/hostname as argument of hostname command: hostname -F /etc/hostname. There is no magic on changing hostname. Unfortunately to update this at shell prompts, users will have to logout and login again... – user34720 Jun 20 '17 at 19:15
  • @nwildner as far as I know there is no logout and login, and following the source code of hostname GNU which uses sethostname and which doesn't implies any such action. (the http://man7.org/linux/man-pages/man2/sethostname.2.html) – marc Jun 20 '17 at 19:24
  • @marc Not my answer =). I know hostname change will not require a logout+login, but updating this information at the user prompt will require a reload on /etc/profile. Take a look at this file and you will find where HOSTNAME variable is feed with hostname information during login. This is why its better to logout+login everyone connected to that host... There is no easy way to update everyone's HOSTNAME shell variable... – user34720 Jun 20 '17 at 19:38
  • take a look at MY answer ;) – user34720 Jun 20 '17 at 19:47
0

Restart of network service is not required. You can follow any of the below procedure.

hostnamectl set-hostname "new.host.name"

Logout and login the system

OR

hostname "new.host.name"

Logout and Login to the system

OR

echo "new.host.name" > /proc/sys/kernel/hostname

Logout and login the system

But make sure HOSTNAME is set in /etc/sysconfig/network file otherwise it cannot assign hostname during system reboot.

  • The question refers to reloading the /etc/hostname file, not modifying the hostname via argument. – marc Jun 20 '17 at 19:12
  • This will not update PS1 shell variable neither HOSTNAME profile variable on-the-fly – user34720 Jun 20 '17 at 19:56
0

With hostname (busybox & GNU) and editing the desired hostname in /etc/hostname:

hostname -F /etc/hostname
  • This will not update PS1 shell variable neither HOSTNAME profile variable on-the-fly – user34720 Jun 20 '17 at 19:56
  • @nwildner read the question carefully!!! The questioner states: "but im still showing the old hostname at prompt" and also refers to the /etc/hostname files. Sorry mate. – marc Jun 20 '17 at 20:04
  • Showing at prompt, and "showing in a command return" is not the same thing. Look at the rest of que question that makes anyone believe that this is just a matter of updating PS1 and HOSTNAME on bash: "need prompt to show [user@sauron dir]# instead of [user@gandalf dir]#.". He already changed /etc/hostname if you take a look at his cat command, but the prompt will not be updated for the reasons that i have explained at my answer(that got downvoted without even a comment...)... So, editing /etc/hostname or changing on the fly with hostname sauron will not update the USER PROMPT.. – user34720 Jun 20 '17 at 20:28
  • I've tested my answer in an angstrom box and worked flawlessly! And as a matter of fact my answer got also downvoted as well! – marc Jun 20 '17 at 20:34
  • Ahw, i see. You are not using CentOS7 the same way as the question, what makes your answer somehow not valid ;) – user34720 Jun 20 '17 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.