Servers

Setting Ubuntu SSH/CLI Prompt to have a user friendly server name


This is something I change on all new servers I setup, by default a lot of servers use their hostname as its IP, and if you are on a local static network, that might be meaningful to you. But if you get random IPs from cloud hosting like AWS, it isn’t too helpful. When I have multiple SSH terminals open, I want to make it clear that this terminal is on production-01.company.com or dev-03.company.com.

After a fresh install of Ubuntu on AWS, your default prompt looks like:

ubuntu@ip-192-168-0-128:/var/log$

To Edit the Prompt

We need to edit the bash config but there isn’t just a single file, there is a system wide one and a per user one. If a user config exists, it will overwrite the system wide config.

To edit, use one of these (probably want to try the user one first):

sudo vi /etc/bash.bashrc #system-wide
vi ~/.bashrc #user-only but overrides system-wide

The configuration of the prompt is stored in the environment variable PS1, so look for a line that assigns the value:

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
# but only if not SUDOing and have SUDO_PS1 set; then assume smart user
 if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
   PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w</strong>\$ '
 fi
  • \u means show the current username – ubuntu
  • \h means show the hostname up to the first ‘.’ – production-01
  • \H means show the full hostname – production-01.zazeski.com
  • \w means working directory – /var/log
  • \W means basename of the working directory – log
  • \t means time in 24-hour HH:MM:SS format – 18:28:58
  • \T means time in 12-hour HH:MM:SS format
  • \@ means time in 12-hour am/pm format
  • \A means time in 24-hour HH:MM format

Now you have choice, you can change the \h to \H to get full hostname visible and then edit your machine’s hostname with this command:

sudo hostname {{new-name}}

Be warned, if you are on AWS, part of its startup process will reset the hostname back to a valid hostname, so you can’t just type in anything. Also your sudo command might have hostname bindings, so if you change it to a hostname that doesn’t map to 127.0.0.1 on the local machine, you might find yourself unable to sudo from that hostname. (Which is why AWS probably auto changes it on reboot, so if you have this issue on AWS, restart the instance from AWS console)

Or, you can hardcode a value in place of the \h in the PS1 command and set it to anything you want:

\t@PROD-01:\w\$

which gives you a prompt that shows the server’s system time (UTC) and a user friendly name “PROD-01” which might be better if your FQDN (fully qualified domain name like prod-01.cluster32.department.example.com) aren’t very helpful.

openanalytics 2097 views

I'm a 35 year old UIUC Computer Engineer building mobile apps, websites and hardware integrations with an interest in 3D printing, biotechnology and Arduinos.


View Comments
There are currently no comments.

This site uses Akismet to reduce spam. Learn how your comment data is processed.