Howto change linux hostname on mounted (copied) drive

I have copied my headless Ubuntu 22LTS server to a backup drive with dd, and I want to deploy it as a backup server with the same relevant settings.
What is the proper way to change the hostname on the copied drive before booting it from the backup hardware?
As the systems run headless, I cannot login to the console to set it interactively after booting it.
I need to bring it on the network and login via ssh, but of course I need to avoid collision with the running server.
In the past, I just modified /etc/hostname on the mounted drive, but with systemd things may work differently.

Asked By: user333869

||

@Tom Yan: Thanks for the link.
I have searched for my hostname in config files on my system and found some which are not covered by the ‘hostname’ command.

The main files are /etc/hostname and /etc/hosts, but there may also be /etc/postfix/main.cf and /etc/mailname which require adaptation if postfix is configured.
The hostname may also be set in the gcos field of /etc/passwd to make mails from root show as root@hostname.
Thus, I think it is best to search for files containing the old hostname and adapt them manually if necessary to cover specific settings of a host.

Answered By: user333869
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.