When creating a new EC2 instance, it auto assigns a hostname matching it local IP address, something like ip-172-1-1-111. The default Amazon AMI EC2 instance does not have hostnamectl command.
Code:
[root@ip-172-1-1-111 ~]# hostnamectl set hostname devopsrun
-bash: hostnamectl: command not found
There are ways to assign a static hostname to Amazon Linux AMI without hostnamectl command.

Using the hostname command


Sudo to root user and, follow these two steps,
  • Edit network config scripts
  • set hostname using the hostname command
  • Restart your instance

Step #1 Editing the configurations


Edit the HOSTNAME to your preferred hostname
Code:
vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=devopsrun
NOZEROCONF=yes

Step #2 Issue the hostname command


Set the preferred hostname
Code:
[root@ip-172-1-1-111 ~]# hostname devopsrun

Step #3 Restart the server instance


Code:
init 6

Verifying


After the server restarts, your server will come up with the new hostname!
Code:
admin@devopsrun-local~$ ssh -i mykey.pem ec2-user@xx.xx.xx.xx
X11 forwarding request failed on channel 0
Last login: Thu XXX  X 05:44:16 20XX from x.x.x.x

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2018.03-release-notes/
10 package(s) needed for security, out of 18 available
Run "sudo yum update" to apply all updates.
[ec2-user@devopsrun ~]$
Congratulations!