Debian – How to set the hostname for a Debian Jessie system

debianhostnamesystemd

Debian Jessie comes with systemd. The recommendation to set the hostname is using hostnamectl for systemd. However, this command does not work (even to display the current hostname) on the Debian Jessie image booted on EC2:

sudo hostnamectl
sudo: unable to resolve host ip-172-30-0-17
Failed to create bus connection: No such file or directory

So I tried to go ahead with Debian's recommendation here.

echo "myhostname" > /etc/hostname
echo "127.0.0.1 myhostname" >> /etc/hosts
/etc/init.d/hostname.sh start
/etc/init.d/networking force-reload

However, after logging out and logging in again, the hostname does not change. It does however change after a reboot, but that is not desirable to me.

This method used to work in Debian Wheezy.

Any help with getting this right is appreciated.

Best Answer

Found the problem. The base AMI on EC2 for Debian Jessie, does not have dbus installed. hostnamectl seems to need dbus. So the fix is to:

apt-get update && apt-get install -y dbus

And then:

hostname=myname
echo "127.0.0.1      $hostname" >> /etc/hosts
hostnamectl set-hostname "$hostname"
echo "$hostname" > /etc/hostname # uneeded

This worked.