VM name doesnt match Linux hostname

Problem:
I create a new Linux VM with Foreman in my vCenter environment. In Foreman i set the name testvm01, wich mean in foreman and in the vCenter the VM will have the hostname testvm01. My Problem is on the VM itself the hostname doenst change from the Ubuntu name like Ubuntu_18.04 to the name i sett (testvm01)

What would be the best way to automaticly change the hostname in the VM itself?

You need to customize your instance. There are two options. You can use ssh finish script if Foreman manages your DHCP and the IP address matches your VM. In your image in Foreman, you need to provide username and password and uncheck cloud init. Then associate finish template with your OS and during provisining Foreman will SSH to your host and can perform post configuration.

The other option is cloud init.

https://docs.theforeman.org/guides/build/doc-Provisioning_Guide/index-foreman.html#Provisioning_Virtual_Machines_in_VMware_vSphere