How to set the hostname for a Debian Jessie system?

22,359

Solution 1

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.

Solution 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.

Share:
22,359

Related videos on Youtube

donatello
Author by

donatello

I am currently a Masters student in Databases at the Indian Institute of Science, Bangalore. I love programming and Linux.

Updated on September 18, 2022

Comments

  • donatello
    donatello over 1 year

    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.

  • Anthony Geoghegan
    Anthony Geoghegan almost 9 years
    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.
  • rzr
    rzr over 8 years
    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
  • Gergely Lukacsy
    Gergely Lukacsy about 7 years
    @donatello Looks like timedatectl have the same dependency towards dbus too... thanks for sharing your solution!