In order to change or set the desired hostname on CentOS 7, use the hostnamectl command as shown in the below command.
hostnamectl set-hostname your-new-hostname
You need to follow some rules while setting your hostname.
- hostnames can contain letters (from a to z).
- hostnames can contain digits (from 0 to 9).
- hostnames can contain only the hyphen character ( – ) as special character.
- hostnames can contains the dot special character ( . ).
- hostnames can contain a combination of all three rules but must start and end with a letter or a number.
- hostnames letters are case-insensitive.
- hostnames must contains between 2 and 63 characters long.
- hostnames should be descriptive (to ease identifying the computer purpose, location, geographical area, etc on the network).
Once you've set your hostname you can check using one of the following commands.
hostname
hostname -s
hostname -f