In Ubuntu for changing hostname one needs to change /etc/hosts and /etc/hostname file. Instead of doing the changes manually, the bash script in this repository can be used to automate it.
After clone, change the file permissions to make it executable.
chmod +x change_hostname.sh
Execute the file as:
./change_hostname.sh
Reference: Change ubuntu hostname