Ask Your Question
5

Correctly setting the hostname - Fedora 20 on Amazon EC2

asked 2013-12-20 08:24:59 -0600

dtonhofer gravatar image

updated 2014-07-05 04:50:12 -0600

(Crossposting from Server Fault with updated question ... and the answer!)

Context

I have a Fedora 20 cloud image running on Amazon EC2 (henceforth called the "instance"). And I have some uncertainty about persistently setting its hostname.

Goal

In this case, let's say I want to set the instance's hostname to penpen.homelinux.org. (This name will also be registered at DynDNS using ddclient, but that is another aspect which we are not interested in here.)

The hostname can of course be set manually after boot completes (using hostnamectl among others). But we want to have the correct hostname set before the first login.

Traditionally, to persistently configure the hostname, one would modify the contents of /etc/hostname. Unfortunately this does not work here.

Default system behaviour

By default, the instance sets its hostname to an internal EC2 name. After boot, we can look at all the little different places that yield the hostname, and we find:

Kernel hostname via 'sysctl'                      : ip-10-164-65-105.ec2.internal
Kernel domainname via 'sysctl'                    : (none)
File '/etc/hostname'                              : contains 'ip-10-164-65-105.ec2.internal'
File '/etc/sysconfig/network'                     : exists but has no 'HOSTNAME' line
According to the shell                            : HOSTNAME = ip-10-164-65-105.ec2.internal
Nodename given by 'uname --nodename'              : ip-10-164-65-105.ec2.internal
Hostname ('hostname')                             : ip-10-164-65-105.ec2.internal
Short hostname ('hostname --short')               : ip-10-164-65-105
NIS domain name ('domainname')                    : (none)
YP default domain ('hostname --yp')               : [hostname --yp failed]
DNS domain name ('hostname --domain')             : ec2.internal
Fully qualified hostname ('hostname --fqdn')      : ip-10-164-65-105.ec2.internal
Hostname alias ('hostname --alias')               : 
By IP address ('hostname --ip-address')           : 10.164.65.105
All IPs ('hostname --all-ip-addresses')           : 10.164.65.105 
All FQHNs via IPs ('hostname --all-ip-addresses') : ip-10-164-65-105.ec2.internal 
Static hostname via 'hostnamectl'                 : ip-10-164-65-105.ec2.internal
Transient hostname via 'hostnamectl'              : ip-10-164-65-105.ec2.internal
Pretty hostname via 'hostnamectl'                 :

So let's try to write to /etc/hostname ...

If one writes the desired hostname to /etc/hostname, this change is lost again at the next boot. Let's examine the boot process, which is performed by systemd.

Example run

Write rorororoor.homelinux.org to /etc/hostname, then reboot.

Using journald we find (Note that the log lines are not entirely orderd by time):

The boot process starts off with hostname as localhost then switches root, at which point the hostname becomes rorororoor.homelinux.org.

Dec 26 15:12:08 localhost systemd[1]: Starting Cleanup udevd DB...
Dec 26 15:12:08 localhost systemd[1]: Started Cleanup udevd DB.
Dec 26 15:12:08 localhost systemd[1]: Starting Switch Root.
Dec 26 15:12:08 localhost systemd[1]: Reached target Switch Root.
Dec 26 15:12:08 localhost systemd[1]: Starting Switch Root...
Dec 26 15:12:08 localhost systemd[1]: Switching root.
Dec 26 15:12:08 localhost systemd-journal[67]: Journal stopped
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journal[155]: Runtime journal is using 8.0M
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journal[155]: Runtime journal is using 8.0M
Dec 26 15:12:12 rorororoor.homelinux.org systemd-journald[67]: Received SIGTERM
...........
Dec 26 15:12 ...
(more)
edit retag flag offensive close merge delete

Comments

3

Setting/changing the computer hostname in Fedora 20 (where systemd is in charge), you can do it with one command in terminal (as @randomuser said). sudo hostnamectl set-hostname --static "thehostnameyouwanthere" . I have posted this sometime in Fedora Project G+ Community

NickTux gravatar imageNickTux ( 2014-06-27 16:28:07 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
6

answered 2013-12-20 17:09:37 -0600

updated 2016-08-02 22:51:51 -0600

deadrat gravatar image

# hostnamectl set-hostname --static fqdn.example.com

edit flag offensive delete link more

Comments

Despite all the context, I'm having trouble identifying if you actually tried this....

randomuser gravatar imagerandomuser ( 2013-12-20 17:11:51 -0600 )edit
2

I think it needs the --static parameter for the change to be persistent.

NickTux gravatar imageNickTux ( 2014-06-27 16:29:04 -0600 )edit
4

answered 2013-12-26 12:27:44 -0600

dtonhofer gravatar image

updated 2014-07-05 04:53:38 -0600

Answer thanks to Michael Hampton (answered on Server Fault):

One needs to do two things:

  1. Set the hostname in /etc/hostname
  2. Edit /etc/cloud/cloud.cfg (which is a YAML file) and set preserve_hostname: true at the toplevel (one can also pass this option in with the user data)

The second step is required because Fedora uses cloud-init to bring in user data from the EC2 environment to provision the instance, and cloud-init needs to be told that the hostname should persist.

This concludes the lesson.

When unsure how to set the hostname in the first place:

Here is how I set it in Fedora 20. I think it's just writing to /etc/hostname which does it. But setting the kernel values and calling hostnamectl (since systemd) for good measure cannot hurt.

OUGHT_HOSTNAME=sigyn
OUGHT_DOMAINNAME=homelinux.org
TARGET=${OUGHT_HOSTNAME}.${OUGHT_DOMAINNAME}
HOSTFILE=/etc/hostname
echo $TARGET > $HOSTFILE
/bin/hostnamectl set-hostname --static $TARGET
/bin/hostnamectl set-hostname --transient $TARGET
/sbin/sysctl -q -w kernel.hostname=$OUGHT_HOSTNAME
/sbin/sysctl -q -w kernel.domainname=$OUGHT_DOMAINNAME

Then check what happens at reboot.

(Grab a litle script that prints all the locations where the hostname may have been configured from here; I used it when wrestling with the EC2 instance)

edit flag offensive delete link more

Comments

I can get cloud-init to use the value provided in /etc/hostname with preserve_hostname: True, but what about the domain? I keep ending up with mirkwood.us-west-2.compute.internal

aluchko gravatar imagealuchko ( 2014-06-27 15:50:59 -0600 )edit

Sorry for the late reply. Here is how I set it in fedora 20. I think it's just writing to /etc/hostname which does it. But setting the kernel values and calling hostnamectl (since systemd) for good measure cannot hurt.

OUGHT_HOSTNAME=sigyn
OUGHT_DOMAINNAME=homelinux.org
TARGET=${OUGHT_HOSTNAME}.${OUGHT_DOMAINNAME}
HOSTFILE=/etc/hostname
echo $TARGET > $HOSTFILE
/bin/hostnamectl set-hostname --static $TARGET
/bin/hostnamectl set-hostname --transient $TARGET
/sbin/sysctl -q -w kernel.hostname=$OUGHT_HOSTNAME
/sbin/sysctl -q -w kernel.domainname=$OUGHT_DOMAINNAME

Then check what happens at reboot.

dtonhofer gravatar imagedtonhofer ( 2014-07-01 15:01:00 -0600 )edit

Edited answer accordingly.

dtonhofer gravatar imagedtonhofer ( 2014-07-01 15:04:33 -0600 )edit

Answer thanks to Michael Hampton (answered on Server Fault):

One needs to do two things:

Set the hostname in /etc/hostname Edit /etc/cloud/cloud.cfg (which is a YAML file) and set preserve_hostname: true at the toplevel (one can also pass this > option in with the user data) The second step is required because Fedora uses cloud-init to bring in user data from the EC2 environment to provision > the instance, and cloud-init needs to be told that the hostname should persist.

This worked for me as well on a Centos 7 ami. Thanks!

vanderbily666 gravatar imagevanderbily666 ( 2015-02-02 16:29:04 -0600 )edit

Question Tools

Stats

Asked: 2013-12-20 08:24:59 -0600

Seen: 27,929 times

Last updated: Aug 02 '16