13

So, What I'm trying to do is simply change the hostname permanently. I can update the hostname but when a reboot is issued the original hostname is used!

I'm aware there is a file hostname located at: /etc/hostname. I update this file and issue a reboot, however the hostname is back to the previous setting!

What I've tried:

  1. Updating /etc/hosts file newhostname
  2. Updating hosts file - 127.0.0.1 newhostname
  3. reboot

I've also tried with sudo user and root!

Why is this happening?!

Thanks for any advice or suggestions, I feel like I've tried every other solution with no avail.

EDIT:

I've just updated the /etc/hostname file on a VM running debian 7 locally and it works as expected. Why doesn't this work when connected to a remote server with SSH?

5
  • Is the server using dhcp?
    – jordanm
    Commented May 4, 2015 at 13:24
  • Not enough rep to add a comment. Have you tried hostname.sh?
    – DCJeff
    Commented May 4, 2015 at 13:29
  • Not using dhcp.
    – dan983
    Commented May 4, 2015 at 13:30
  • How are you checking what the hostname is? Are you just going off of what the prompt says? When you SSH into the server, do echo $PS1. I highly doubt someone has changed it, but you never know ....
    – Safado
    Commented May 4, 2015 at 14:58
  • 1
    I just issue 'hostname' after the reboot and it shows the old host name. It's a bit strange because it doesn't happen when I change the hostname on a VM.
    – dan983
    Commented May 4, 2015 at 15:07

4 Answers 4

10
hostnamectl set-hostname servername.example.com

That's for RHEL 7 that is.

1
  • Same happened to my Debian setups. thx.
    – m3nda
    Commented Feb 25, 2019 at 12:40
5

Another reason why the host name gets reset could be the cloud-init package. This package is used to provision a host and can also be used to set the systems host name.

In the case of my Ubuntu system the preserve_hostname option was set to false in the file /etc/cloud/cloud.cfg. Setting this option to true, will let a newly set host name survive a reboot.

1
  • I deployed a Debian image to OCI and was having this issue. This solutions was the fix to my problem. Thank you!
    – Max Kulik
    Commented Dec 15, 2022 at 22:16
4

/etc/hosts is meant as a first-step for DNS resolution. If you're trying to reach a host that's present there, it will use that record instead of querying DNS.

As you've discovered, /etc/hostname is one option for hostname changes in Debian. In RHEL6, it's /etc/sysconfig/network, and in RHEL7 it's either the hostnamectl command or /etc/hostname.

Changing hostnames on Linux hosts can vary by distribution and release, so it's best to Google the procedure for your particular platform.

3

Try setting runtime kernel parameter

sudo bash -c "echo 'kernel.hostname = YOUR_HOSTNAME' >> /etc/sysctl.conf"

Apply your changes with

sudo sysctl -p

Now simply, logout and log back in, your hostname should be the one you applied.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .