Set a hostname in image mode for RHEL

You can set a custom hostname for your image mode for RHEL system by modifying the /etc/hostname file. Setting a hostname identifies the system on the network and simplifies management in multi-host environments.

Before you begin

  • You have a base bootc container image to extend, for example, registry.redhat.io/rhel10/rhel-bootc:latest.
  • The container-tools meta-package is installed if you are building or running containers locally.

Procedure

Set the hostname by choosing one of the following methods:
At build time in a Containerfile
  1. Add the following instruction to your Containerfile to embed the hostname in the image:
    FROM registry.redhat.io/rhel10/rhel-bootc:latest
    
    RUN echo "<hostname>" > /etc/hostname

    Replace myhostname with the required hostname for the system.

By using Anaconda with a Kickstart file
  1. Add the following directive to your Kickstart file to set the hostname during installation:
    network --hostname=<hostname>

    Replace myhostname with the required hostname for the system.

At runtime with a privileged container or on the booted system
  1. On the running system, write the required hostname to the /etc/hostname file:
    # echo "<hostname>" > /etc/hostname

    Replace myhostname with the required hostname for the system.

    Note

    Because /etc is persistent and mutable by default in image mode for RHEL, changes to /etc/hostname on a running system persist across reboots. However, if /etc is configured as transient, the hostname reverts on reboot.

Results

  1. Change the hostname:
    # hostnamectl set-hostname <hostname>
  2. Reboot the system:
    # reboot
  3. Verify the hostname by running the following command:
    $ hostnamectl

    The output displays the configured hostname under Static hostname.