4

I am setting up multiple Raspberry Pi's in one go, flashing the cards and customizing as much as I can in /boot before actually booting the RPI's for the first time. One thing I haven't figured out how to do is to assign a unique hostname via a /boot modification. Anyone have that solved?

3
  • in the raspberry pi image you get from raspberrypi.org, there is nothing you can do in boot to set hostname Commented Jul 16, 2020 at 22:50
  • There seems to be a general consensus that will never be done by "The Organization". Maybe some sort of specialized program could find & re-write the hostname?
    – Seamus
    Commented Jul 17, 2020 at 3:06
  • Is there a specific reason why it must be in /boot? It is relatively easy to do it in the root partition's /etc Commented Jul 17, 2020 at 11:31

3 Answers 3

2

I assume you can only read the fat32 boot partition when configuring the SD-Card in a card reader attached to another non Unix computer (MS Windows, Apple Mac) before booting it in the RasPi. The hostname is defined on the ext4 root partition in the file /etc/hostname containing the name. On the running Raspberry Pi you can just create this file in the boot directory and have a symlink to it:

rpi ~$ sudo bash -c 'echo myhost > /boot/hostname'
rpi ~$ sudo ln -s /boot/hostname /etc/hostname

You also have to do the same with the /etc/hosts file because it also contains the hostname.

Now you can set the hostname in the fat32 boot partition. You have to do it only one time for the image and then clone it for your work.

2
  • You also need to change the name in /etc/hosts
    – Milliways
    Commented Jul 25, 2020 at 3:10
  • @Milliways Correct. Thanks for the hint. Have added it to the answer.
    – Ingo
    Commented Jul 25, 2020 at 9:09
1

In the 'modern' Raspberry Pi Imager using the cog wheel in the first screen allows you to set some presets including the hostname but also wifi configuration, default user name, whether SSH is enabled or not, very useful!

Raspberry Pi Imager > Advanced Options

If you select a new hostname, this will create a file on the 'bootfs' partition of your SD card after writing. So if you do not want to use the Raspberry Pi Imager, you can also do this manually. Create a file called firstrun.sh and add this, for changing the hostname to jackfruit.local:

#!/bin/bash

set +e

CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
if [ -f /usr/lib/raspberrypi-sys-mods/imager_custom ]; then
   /usr/lib/raspberrypi-sys-mods/imager_custom set_hostname jackfruit
else
   echo jackfruit >/etc/hostname
   sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\tjackfruit/g" /etc/hosts
fi
rm -f /boot/firstrun.sh
sed -i 's| systemd.run.*||g' /boot/cmdline.txt
exit 0

and modify cmdline.txt to add systemd.run=/boot/firstrun.sh, for my 'headless raspbian' it currently looks like the below:

console=serial0,115200 console=tty1 root=PARTUUID=544c6228-02 rootfstype=ext4 fsck.repair=yes rootwait quiet init=/usr/lib/raspberrypi-sys-mods/firstboot systemd.run=/boot/firstrun.sh systemd.run_success_action=reboot systemd.unit=kernel-command-line.target%

0

[SOLVED]

Building upon MichielB's answer, the code for changing it dynamically through a new_hostname file after the pi has been set up is here, no modification to the command line is necessary and you will have NOT have to reboot the pi after its done. This code is for the situation where you wanna mass produce an OS image from a base image and your greater shell script modifies the hostname file to create its own unique OS image, thus the whole hostname changing process is automated and no reboot is necessary. The code can be used with custom front-end services to change the hostname dynamically

More details in the github link: https://github.com/techscapades/change-raspberrypi-hostname-from-file-no-reboot

#!/bin/bash

set +e

CURRENT_HOSTNAME=`cat /etc/hostname | tr -d " \t\n\r"`
NEW_HOSTNAME=`cat /path_to_new_hostname_file | tr -d " \t\n\r"`

echo `cat /path_to_new_hostname_file | tr -d "\t\n\r"` >/etc/hostname
sed -i "s/127.0.1.1.*$CURRENT_HOSTNAME/127.0.1.1\t$NEW_HOSTNAME/g" /etc/hosts
echo `cat /etc/new_hostname | tr -d "\t\n\r"` >/proc/sys/kernel/hostname

exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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