..

Change the host and computer name on macOS

sudo scutil --set ComputerName "newname"
sudo scutil --set LocalHostName "newname"
sudo scutil --set HostName "newname"

Flush the DNS cache:

dscacheutil -flushcache

Restart your Mac.