Change computer name

Just after installing a new version of OS X you just realized you don't like the name of your computer?. No problem just go to Preferences > Sharing and change it:

But then you go to Terminal.app and realized that your prompt still has the old name :(. That is because the default terminal prompt uses the hostname. Doing above things does not change the hostname.

You could change the prompt by modifying $PS1 variable but that will not solve the problem as the kernel hostname would still have the old name.

Change the Hostname

To change it we use sysctl kern.hostname=NEW_NAME
$ hostname
Ignacio-no-MacBook-Pro.local
$ sudo sysctl kern.hostname=IgnacioMBP
kern.hostname: Ignacio-no-MacBook-Pro.local -> IgnacioMBP
$ hostname
Ignacio-MBP.local
Sometimes Computer name field is disabled (I think it is because of some policies in school or work computer. Maybe MDM?) With the following command the name can me changed.
$ sudo scutil --set ComputerName Ignacio-MBP
$ scutil --get ComputerName
Ignacio-MBP
To confirm current value we use hostname command. Now we have to restart the terminal so the new hostname is read again populating $PS1 as intended :)


See also:
MDLog:/sysadmin - How to change the hostname of a Linux system

This work is licensed under BSD Zero Clause License | nacho4d ®