# Unable to set the hostname in RockyLinux?

**URL:** <https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541>\
**Category:** Rocky Linux Help & Support\
**Created:** [November 12, 2021, 6:29pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541 "2021-11-12T18:29:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SomervilleTom](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@SomervilleTom](https://forums.rockylinux.org/u/SomervilleTom)\
**Post date:** [November 12, 2021, 6:29pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/1 "2021-11-12T18:29:44Z")

</div>

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):

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

```

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

```auto
[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:

```auto
# Automatically generated by the vm import process
NETWORKING=yes

```

`/etc/hosts` contains this:

```auto
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:

```auto
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:

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

```

On a CentOS 7 instance:

```auto
# 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`?

---

<div class="post-metadata">

**Author:** ![tjdoyle](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/tjdoyle/32/500_2.png) [@tjdoyle](https://forums.rockylinux.org/u/tjdoyle)\
**Post date:** [November 12, 2021, 6:49pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/2 "2021-11-12T18:49:13Z")

</div>

Hi,

Have you tried hostnamectl:

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

Thanks Tom.

---

<div class="post-metadata">

**Author:** ![SomervilleTom](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@SomervilleTom](https://forums.rockylinux.org/u/SomervilleTom)\
**Post date:** [November 12, 2021, 7:20pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/3 "2021-11-12T19:20:36Z")

</div>

That worked.

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

---

<div class="post-metadata">

**Author:** ![iwalker](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/iwalker/32/2599_2.png) [@iwalker](https://forums.rockylinux.org/u/iwalker)\
**Post date:** [November 12, 2021, 7:40pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/4 "2021-11-12T19:40:35Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![SomervilleTom](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@SomervilleTom](https://forums.rockylinux.org/u/SomervilleTom)\
**Post date:** [November 12, 2021, 7:59pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/5 "2021-11-12T19:59:36Z")

</div>

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:

```auto
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`.

---

<div class="post-metadata">

**Author:** ![iwalker](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/iwalker/32/2599_2.png) [@iwalker](https://forums.rockylinux.org/u/iwalker)\
**Post date:** [November 12, 2021, 9:26pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/6 "2021-11-12T21:26:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SomervilleTom](https://avatars.discourse-cdn.com/v4/letter/s/8797f3/32.png) [@SomervilleTom](https://forums.rockylinux.org/u/SomervilleTom)\
**Post date:** [November 12, 2021, 9:40pm UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/7 "2021-11-12T21:40:21Z")

</div>

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

I think I’m all set then.

---

<div class="post-metadata">

**Author:** ![brian](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.rockylinux.org/brian/32/5777_2.png) [@brian](https://forums.rockylinux.org/u/brian)\
**Post date:** [August 25, 2023, 4:01am UTC](https://forums.rockylinux.org/t/unable-to-set-the-hostname-in-rockylinux/4541/8 "2023-08-25T04:01:45Z")

</div>


