Obviously you can do it manually, but there are a number of tools that can do it for you:

  1. hostnamectl command : Control the system hostname. This is recommended method.
  2. nmtui command : Control the system hostname using text user interface (TUI).
  3. nmcli command : Control the system hostname using CLI part of NetworkManager.
Tip stolen from here