X

Prevent the Terminal host name from changing

The computer's hostname as shown in the terminal may change. Here's how to change it back and prevent that from happening.

Topher Kessler MacFixIt Editor
Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.
Topher Kessler
2 min read

When you launch the terminal, the default shell "Bash" will present the last login time, followed by the command prompt. This is formatted to show the computer's name, followed by the current directory and then a dollar sign. Depending on the system and network configuration, the computer's name may change.

My hostname is "Tophers-Desktop"

The computer's host name is the one you type in the "Sharing" system preferences, and while a change in the hostname should not affect the performance of the machine, it is nice to have it be consistent. The first thing to try is to use the "hostname" command to change the hostname:

I've just changed the hostname to "TEST", but the current login session does not yet reflect the change

After running this command, the terminal name will not change until you quit the current shell and log in again:

Starting a new terminal session shows the change.

The hostname can be changed dynamically by routers and other network devices through DHCP requests, which can have the hostname keep changing every time your IP address and other DHCP information is renewed. If this is the case, you may be able to set the router to never provide hostname information, or you can set the computer to never accept a hostname change. To do this, you will need to add a line to the computer's hostconfig file using the following procedure:

  1. Open the Terminal

  2. Enter the following command:

    sudo pico /etc/hostconfig

  3. Authenticate, scroll to the bottom of the file, and add the following line:

    HOSTNAME=Tophers-Desktop

    (Change "Tophers-Desktop" to your desired hostname--no spaces)

  4. Save the file and quit by pressing control-X, confirming the save with the "Y" key followed by "enter".

Editing the /etc/hostconfig file in "pico" so the hostname never changes

After this has been added to the file, the computer's hostname should not change even when you renew your DHCP lease.



Questions? Comments? Post them below!!!
Be sure to check us out on Twitter and the CNET Mac forums.