4

As you might have guessed from the title, I would like to know if it is possible to change a RasPis hostname from the config.txt file on the SD card.

I'm making a lot of SDs from a custom image, and obviously the hostnames collide when booting (they are used from other apps in the network).

  • 3
    What's wrong with just setting /etc/hostname? – goldilocks Mar 30 '16 at 13:24
  • 2
    Depending on the number of machines you are managing, it may be time to look at a configuration management solution (i.e. puppet, ansible, chef). – Steve Robillard Mar 30 '16 at 13:42
  • @goldilocks that I must boot the Pi ^^ and some of the SDs are being sent overseas... I'll check those out! thanks Steve! – Jo Colina Mar 30 '16 at 14:08
  • For folks just trying to change your Pi's hostname (but don't need a custom image) you can run sudo raspi-config and select Advanced Options -> Hostname to update your Pi's name. – dimo414 Apr 4 '16 at 0:30
5

It is not possible to set the device hostname through config.txt. A full list of commands can be found on the Raspberry Pi Foundation's website.

An Alternative:

You mention that you're creating a custom image. In this case, I believe the best practice would be to write the assigned hostname to /etc/hostname during the card creation process. It does require a reboot (assuming the RPi is currently running), but so does changing anything in config.txt.

If you really want to get around the restart thing, it's possible, but can cause some issues. The hostname can be temporarily changed by the command sudo hostname {super_cool_name_here}. Once that's set, restart the network service with sudo service network restart.

Now, if you run hostname you should see your new hostname as active. This change is not permanent. When you restart the device it will go back to whatever is stored in /etc/hostname.

  • 2
    Setting /etc/hostname doesn't require a reboot as far as I'm aware. You should be able to just change it on the card and that's that (I'm presuming the system isn't running, the cards are just being created). – goldilocks Mar 30 '16 at 14:56
  • @goldilocks: (assuming the RPi is currently running) – Jacobm001 Mar 30 '16 at 15:52
  • Thanks! Just to clarify, I made a custom image, installed some packages, made some configs, and now I just used dd from the card to my computer, and then dd again from the computer to another card to create another raspi. So in the best of cases, to be fast, I wanted to just open a file on the pc after ending the dd (this is why i thought of config.txt) and change it. – Jo Colina Mar 30 '16 at 18:09
  • HI @JoColina, did you find a solution? – Pavel Reznikov Oct 4 '16 at 6:13
  • @PavelReznikov no I did not. – Jo Colina Oct 4 '16 at 10:07
2

Okay, from your comments on the other answer, it looks like you want to change the hostname of Raspberry Pi image without actually booting it. This should be trivial. We want to modify the /etc/hostname in the second partition, so we will find its location in the image using fdisk.

fdisk -l image.img

Where image.img is your image file. Look under the start column for the second (Linux ext4) partition to find the block it starts on. Substitute that into the command below. Note that this won't change after this process, so you can change the hostname, send off the image, and then change it again, without needing to run fdisk and use a new value.

sudo mount -o loop,offset=$((TYPE_START_BLOCK_HERE*512)) image.img /mnt
nano /mnt/etc/hostname

Get rid of the default and type in the hostname you want, and then hit Ctrl-x, then y, and then enter. We can now unmount the image like this.

sudo umount /mnt

Done! Using dd to load the image onto the SD card and booting the card in a Pi will set the Pi's hostname to the one you specified above.

To automate this process and change the hostname of an image quickly between dd's, one could write a bash script:

#!/bin/bash

mkdir tst;
echo ${1:?"Set argument one to filename"} > /dev/null;
echo ${2:?"Set argument two to desired hostname"} > /dev/null;
mount -o loop,offset=$(($(fdisk -l $1 |awk '$7=="Linux"{print $2}')*512)) $1 
tst;
echo $2 > tst/etc/hostname;
umount tst;
rmdir tst;
  • The hostname should also be changed in /etc/hosts (/mnt/etc/hosts). – bstipe Jul 30 '17 at 1:30

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.