Unable to set the hostname in RockyLinux?

Please forgive this possibly newbie question. This is perhaps trivial, but I’d like to fix it.

I have an AWS EC2 instance built from the standard RockyLinux vmi.

When I connect to the system with an SSH client, the command line prompt looks like this (private IP address changed for privacy):

[tms@ip-111-22-33-44 ~]$ 

I want it to look like it does on my CentOS 7 instances:

[tms@subdomain ~]$ 

I have a valid domain name (subdomain.domain.com)

The contents of /etc/sysconfig/network and /etc/hosts are the same as in my CentOS 7 systems.

/etc/sysconfig/network contains just this:

# Automatically generated by the vm import process
NETWORKING=yes

/etc/hosts contains this:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

# Use the private, rather than public, IP address locally
111.22.33.44 subdomain.domain.com

This is the same (adjusting for domain name and IP address) as on my CentOS 7 instances.

I tried adding the following line to /etc/sysconfig/network following guidance from RedHat:

HOSTNAME=subdomain.domain.com

I’ve rebooted the system. I get the same unwanted shell prompt.

Here’s what I get from hostname (running as root) on the two systems:

On RockyLinux instance:

# hostname
ip-111-22-33-44.ec2.internal

On a CentOS 7 instance:

# hostname
subdomain.domain.com

I have not attempted to explicitly set the hostname using the hostname command – I didn’t do that on my CentOS 7 instances, and Redhat says that this will be reverted at each reboot.

What do I do in order to make the hostname (and therefore shell prompt) of my RockyLinux EC2 instance be subdomain.domain.com rather than ip-111-22-33-44.ec2.internal?

1 Like

Hi,

Have you tried hostnamectl:

hostnamectl set-hostname **your-new-hostname**

Thanks Tom.

1 Like

That worked.

I have never heard of the hostnamectl command before now. I greatly appreciate this immediate and successful suggestion.

1 Like

Sometimes what you might also have to do, is edit /etc/cloud/cloud.cfg and make sure this value is set:

preserve_hostname: true

as on reboot cloud-init can run and reset your hostname again - the config file will probably already have an entry so you can search for it and edit the existing value and change from false to true. A lot of VPS/instance providers are using cloud-init. By setting that value, it stops cloud-init from resetting the hostname, meaning what you set before with hostnamectl, etc will not get overwritten/reset.

I see no existing entry for preserve_hostname. This looks like a yaml file with some structure.

Should this be at the top level of the file?

Here are the current contents of that file on my system:

users:
 - default

disable_root: 1
ssh_pwauth:   0

mount_default_fields: [~, ~, 'auto', 'defaults,nofail,x-systemd.requires=cloud-init.service', '0', '2']
resize_rootfs_tmp: /dev
ssh_deletekeys:   1
ssh_genkeytypes:  ~
syslog_fix_perms: ~
disable_vmware_customization: false

cloud_init_modules:
 - disk_setup
 - migrator
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - update_etc_hosts
 - rsyslog
 - users-groups
 - ssh

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - rh_subscription
 - yum-add-repo
 - package-update-upgrade-install
 - timezone
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd

cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

system_info:
  default_user:
    name: rocky
    lock_passwd: true
    gecos: Cloud User
    groups: [adm, systemd-journal]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

# vim:syntax=yaml

I also grepped the several files in /etc/cloud/cloud.cfg.d and find no entries for preserve_hostname.

Easiest way is reboot and see if the hostname resets or not. If not then no need to edit it.

Ah, thanks. I rebooted before posting my “success” reply.

I think I’m all set then.

3 Likes