Keep your command prompt host name static in OS X and other Unix systems

Keep your command prompt host name static in OS X and other Unix systems

 

The default terminal shell for users in OS X is the popular Bash shell, and when in use will provide you with a command prompt that gives your current host name, followed by the current folder, and the current user name for the Terminal session. This gives you some quick information about the system; however, there may be times when this will change.

When you connect to some corporate or even public networks, be they Wi-Fi or hard-wired, the router's configuration may instruct your system to adopt a new host name for proper identification on the network. This new host name may be a sequential numbering system, the computer's MAC address, or the issued IP address for the current session.
Though such renaming is common and beneficial for network admins, this will be reflected in the Terminal's command prompt, and may be a bit distracting whenever you open it to run commands and configure your system. For instance, my personal laptop gives me this familiar command line prompt whenever I use the Terminal:
Tophers-Laptop:~ tkessler$
This prompt indicates the computer (Tophers-Laptop:), the current folder ("~", or the home directory), and then the username (tkessler); however, if I connect to some networks I will see something like the following instead:
MobileWiFi-192-168-1-4:~ tkessler$
While such changes don't affect a thing, and can be overcome by running the "hostname" command to reset the current host name, it may be frustrating to deal with if you regularly use networks that invoke this change. Fortunately, you can overcome it by issuing a quick custom prompt for the Terminal shell.
To do this, you will need to edit your shell's profile, so run the following command to edit this hidden profile for your account:
pico ~/.bash_profile
In the editor that opens, enter the following line at the end of the document, making sure to change COMPUTERNAME to the desired name of your system:
PS1="COMPUTERNAME:\W \u$ "
With this line entered, press Control-O to save, followed by Control-X to exit. Now when you create a new Terminal session the system will use the static "COMPUTERNAME" text that you entered in place of the host name, but will mirror all other aspects of the bash prompt. This will keep host name changes from being reflected in the Terminal when you change networks.


Leave a Reply

Send us a Message

Get all the information