I update my BeagleBone images very frequently. As soon as you have more than one beagle running around your house, keeping track of them takes some effort. I like to have unique hostnames set for each device so that I can ssh into the unique device. I also put a little sticker on each one to remind me of the hostname.

I'm a H2G2 fan. I feel somewhat compelled to always have a picture. The use of the filter in this picture is borderline relationship ending.
I’m a H2G2 fan. I feel somewhat compelled to always have a picture. The use of the filter in this picture is borderline relationship ending.

Anyway, it’s pretty easy to change the hostname on Debian. To do it properly you need to edit two files, /etc/hosts and /etc/hostname. It’s an annoying little task that I do often enough I finally decided to script it. I should start making a collection of these scripts as I perform the same tasks each time, but we have to start somewhere.

Behold, the changehostname.sh script:1

#!/bin/bash

if [[ "$#" -ne 1 ]]; then
    echo "$0 usage: newhostname (run as root)"
    exit 1
fi

name=$(hostname)

hostname $1
sed -i "s/$name/$1/" /etc/hosts

Also available via wget:

wget https://gist.githubusercontent.com/jbdatko/d7fd5f4232db99f85e00/raw/636dd36cf34069489cd3988de7381d41c7595469/changehostname.sh
chmod +x changehostname.sh

  1. It’s not that impressive, I know. I need to put all of these scripts in a repo and then somehow automatically download them and run them.2 
  2. There may be a utility that does this already. But it’s fun to re-invent the wheel every so often. 
Advertisements