19

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.

1

3 Answers 3

33

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.

3
  • 3
    Good catch on the dbus dependency. FYI: I've been learning how to configure systemd systems and discovered that hostnamectl set-hostname myhostname automatically updates the contents of /etc/hostname (the static hostname) so there's no need for the first echo command. Jul 14, 2015 at 12:01
  • Also warning about existing RAIN , may the arrays be adjusted too ? root@debian:~# rm /etc/ssh/ssh_host_* root@debian:~# dpkg-reconfigure openssh-server And maybe regen initrd too
    – rzr
    Nov 23, 2015 at 12:58
  • 1
    @donatello Looks like timedatectl have the same dependency towards dbus too... thanks for sharing your solution! Apr 28, 2017 at 12:49
2

To change the hostanme of your EC2 host, you must follow the steps bellow :

  1. Login as root :$: sudo su -
  2. Install dbus :$: apt-get update && apt-get install -y dbus
  3. Setup hostname : hostnamectl set-hostname <HOSTNAME>

Make sure to change <HOSTNAME> with the hostname you want to set.

0

We don't actually need to install any package, we just need to edit the file as per you distro from the below list.

root@ServerOne:~# ls -l /etc/cloud/templates/
total 32
-rw-r--r-- 1 root root 1487 Nov 18  2015 chef_client.rb.tmpl
-rw-r--r-- 1 root root  941 May  3 23:37 hosts.debian.tmpl
-rw-r--r-- 1 root root  914 Nov 18  2015 hosts.freebsd.tmpl
-rw-r--r-- 1 root root  912 Nov 18  2015 hosts.redhat.tmpl
-rw-r--r-- 1 root root  866 Nov 18  2015 hosts.suse.tmpl
-rw-r--r-- 1 root root  812 Nov 18  2015 resolv.conf.tmpl
-rw-r--r-- 1 root root 1506 Nov 18  2015 sources.list.debian.tmpl
-rw-r--r-- 1 root root 2841 Nov 18  2015 sources.list.ubuntu.tmpl
root@ServerOne:~#

Edit the line as below, (Commented one is the original line), replace {{fqdn}} and {{hostname}} as below.

#127.0.1.1 {{fqdn}} {{hostname}}
127.0.1.1 ServerOne.opsplus.io ServerOne

Alternatively, set "manage_etc_hosts:" to false as below and you are free to modify /etc/hosts file as it will stop overriding the hosts file.

root@ServerOne:~# cat /etc/cloud/cloud.cfg.d/01_debian_cloud.cfg
apt_preserve_sources_list: true
manage_etc_hosts: true
root@ServerOne:~#

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .