Mac OS Hostname and the Bash prompt

When I am using my MacBook Pro on my offices LAN, I sometimes encounter an issue where my hostname displayed in the bash prompt is actually of a different machine. For example, instead of saying:

user@geekybrit$

It would say:

user@somewhereelse$

A tad annoying to say the least. It turns out that this is because the DHCP Client ID was empty, and something strange was happening on our DNS and DHCP servers. To fix this I manually set the DHCPClient ID on each interface. To do this go to System Preferences > Network, and then for each interface in the “Show:” Drop down, set a DHCP Client ID. It is best to enter a unique name per interface.

You may also want to set your hostname in the System Preferences > Sharing dialog to be something suitable, especially if this is a long string. I always try to keep my hostname shorter than 8 characters, more out of a habit than anything else (DOS machines could only see 8 character hostnames).

Last but not least, if your bash prompt gets too long whenever you are deep in a directory, you may want to change the prompt to only show the last directory you are in, rather than the full path. For example:

user@geekybrit /path/to/a/deep/directory/gets/really/annoying/$

It would say:

user@geekybrit annoying$

if you type “echo $PS1” into your bash prompt you will see the settings for the prompt in your current environment.

By default on Mac OSX Tiger it is: “\h:\w \u\$”
\h = hostname
\w = full directory path
\u = user.

If you want to change this you will have to edit this environment variable. You can try exporting some different options. Here is a good page which explains the various options the PS1 variable can take.

Try some options out by typing:
export PS1=”new options here”

Once you have chosen a suitable schema, you can edit your own bash profile (~/.bashrc) or the system bash profile for all users (/etc/bashrc).

Simply add or edit the following line:

export PS1="your schema"

Myself? I use:

export PS1="\u@\h \W"

The \W only prints the current directory and not the whole path.

Leave a Comment