3

I have a Raspberry pi 3b that I am trying to change the hostname from the default of raspberry. Every time I change it and then reboot, the hostname is still raspberrypi.

os-release yields:

PRETTY_NAME="Raspbian GNU/Linux 10 (buster)" NAME="Raspbian GNU/Linux" VERSION_ID="10" VERSION="10 (buster)" VERSION_CODENAME=buster

uname -r yields: 5.10.17-v7+

The image on the SD card was created by using rpi-imager, on an RPI4, selecting Raspberry PI OS Lite (32-bit).

I tried changing the host name using raspi-config. I don't take the default reboot now so I can verify the change. If I cat /etc/hostname, I see the new hostname in the hostname file. I reboot and the hostname is raspberrypi again. I check /etc/hostname and see that the hostname is reverted back to raspberrypi.

I've also tried manually editing /etc/hostname, saving the edit, cat-ing /etc/hostname to verify that the change I made is there and it is. Reboot, and the change is lost; once again, /etc/hostname says raspberrypi.

Any ideas why this is happening?

Thanks in advance.

4
  • 1
    If file changes do not survive a reboot your filesystem is corrupt - probably Read Only.
    – Milliways
    Jul 13 '21 at 7:02
  • NOTE manually editing /etc/hostname is NOT recommended; there are other changes required.
    – Milliways
    Jul 13 '21 at 7:03
  • The file system seems to be OK because it changes it before the reboot. I can log off and log on, as long as I don't reboot, and the change shows up if I cat /etc/hostname. The other change required, completed by raspi-config, is /etc/hosts and that change persists the reboot - more indication that the file system is not read-only.
    – Dale
    Jul 13 '21 at 17:22
  • 1
    No; I was mistaken. The hosts change does not persist; you're right that it has to be the SD card; I'll try a new one. Thank you. One weird thing, at least to me, is that I can write to the SD card using rpi-imager - I do it over and over again trying to get a fresh load for other work I am testing - but it is definitely not persisting changes once the file system is loaded in Raspberry PI OS.
    – Dale
    Jul 13 '21 at 17:35
4

First, like @Milliways said, check that your SD card is still OK. Create a file, reboot, and verify that the file is still there. Or try using the script @Ingo wrote.

If your SD card is writable, you might want to try setting the hostname the Debian way:

sudo hostnamectl set-hostname my_name
1
  • 1
    I tested the SD card by entering touch test.txt. The file showed up in my home directory but after rebooting it was gone. I tried another SD card, imaging from scratch, and it worked fine so I tossed the bad SD card.
    – Dale
    Jul 15 '21 at 5:59
1

Using hostnamectl is a good start, but also check your network configuration on your router - sometimes raspbian will pick up the hostname that it reported to the router when it was first assigned an IP address. Most routers drop this table when they reboot, so rebooting your router could be the easiest fix.

One good option is to create a static DHCP entry for the Pi, using the hostname you would like. That way, from other machines on your network you can access it via hostname in addition to the IP address.

1
  • It's indeed a possibility, though routers don't usually assign host names unless the user configured them to do so. A good fix would be to remove the host-name option from the DHCP client request string (was it /etc/dhcp/dhclient.conf?) Jul 13 '21 at 15:10

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.