How to Set or Change System Hostname?
Step 1 - Check your Current Hostname
Check the Hostname with hostname Command.
$ hostname
oxygen
Step 2 - Change Hostname with hostnamectl Command
Change the Hostname from oxygen to hydrogen.
$ sudo hostnamectl set-hostname hydrogen
Step 3 - Update /etc/hosts
Use your favorite text editor to change any instances of your old hostname to the new hostname in /etc/hosts. If you have a DNS name pointed to this instance, it's a best practice to also set that name here.
$ sudo nano /etc/hosts
From:
127.0.0.1 localhost
127.0.1.1 oxygen
To:
127.0.0.1 localhost
127.0.1.1 hydrogen.example.com hydrogen
Step 4 - Confirm the Change
Reboot the Server.
Test your change with hostname.
$ hostname
hydrogen