Tab Bar

Tuesday, February 14, 2012

Change the Command Prompt / Computer Name in Ubuntu / Linux

Presumably, the most users of Linux on the desktop have either a quite weird command prompt, derived from the machine their system is running on, or one that's stating their username twice, and really don't need the computer name part of it. You can easily change the command prompt, and/or shorten it so that you both have more space for the actual commands or can include other stuff that's more relevant for you. Or you may want to change the computer name for some reason. This is how to do both of that!


Computer Name

The first thing you got to ask yourself, of course, is whether you need the computer name, also called "hostname", in your command prompt or not. That's important if you are logging in to other systems remotely, rarely done by the average desktop user. If the answer to that question is 'yes' but you want to change the hostname to something more nice or appropriate, or you want to change it for some other reason, you just need to change two entries in two different configuration files, as shown below.

Notice the allowed characters for setting a hostname, otherwise you can easily end up with a broken system variable, with all sorts of possible issues!:

Host names may contain only alphanumeric characters, minus signs ("-"), and periods ("."). They must begin with an alphabetic character and end with an alphanumeric character.

Source: http://manpages.ubuntu.com/manpages/oneiric/en/man5/hosts.5.html

Open the concerning files with either of these commands. You will easily spot your currently set hostname; then just change it to your liking.

gksudo gedit /etc/hostname

gksudo gedit /etc/hosts

The default file contents look like these:

krytarik-desktop

127.0.0.1       localhost
127.0.1.1       krytarik-desktop

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

Depending on the Ubuntu version, there are different behaviors that can occur when editing those files. Particularly, when the changes are being applied, possibly messing up the "/etc/hosts" during the process; and the implications on the running desktop environment. But we are surely on the safe side when I advise to:
  • Load both the concerning files in a text editor at the same time (whether it's on the CLI or GUI), and save them after another, twice, alternately - ignoring any possible upcoming warnings about intermittent changes, as that's exactly what we want to override.
  • Re-check your "/etc/hosts" after editing both files, and after either running the below command or a reboot.
  • Either run this command to reload the hostname from the just edited file and apply it to your system immediately (thanks to Yetiman64 for this tip in the comments!), and if you notice any issues, relogin:
    sudo hostname -F /etc/hostname
    This has the bonus that it would output an error message if the newly chosen hostname is invalid, so I in fact recommend that.
  • Or, alternatively, just reboot.
Command Prompt

Regardless of what Linux distribution you are running, to change the command prompt, you always need to change the settings for the "PS1" variable in the file ".bashrc" located in your home directory, as long as you are using the Bash, of course, not the Shell.

Ubuntu / Debian

This is how to change the command prompt in Ubuntu, and presumably any other Debian-based Linux distro, judging from it being literally included in the code.

1. Open the file ".bashrc" located in the top-level of your home directory, you may need to press Ctrl+H to see the hidden files/directories.

2. Find the lines bold-marked below, and change them to your liking, with the help of the list of keys below. The lines below the default ones are how I've set up my own command prompt.

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    # PS1='${debian_chroot:+($debian_chroot)}[\u]:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    # PS1="\[\e]0;${debian_chroot:+($debian_chroot)}[\u]: \w\a\]$PS1"
    ;;
*)
    ;;
esac

3. Re-open the Terminal for the changes to take effect, or if at the console/tty, relogin there.

Other Linux

If you are running a non-Debian-based Linux distro, you'll obviously find other settings for "PS1" in the ".bashrc", but they should be similarly set up and easy to change. But if you don't find any settings for it in there, you can simply set them up yourself, derived from the code above.

Available Keys

To build up the command prompt, you have quite a range of keys available, each of which either pulls in some kind of data or just formats the command prompt. The most comprehensive list of available keys I've found so far is on this site (it also includes some instructions on how to color the prompt, if you really want to do that - I won't cover it here, as I find it really overly hanky-panky, and distractive from the more important things going on at the CLI). I can't guarantee, though, that every single one of the listed keys works, as I've tested only a couple of them.

SequenceDescription
\aThe ASCII bell character (you can also type \007)
\dDate in "Wed Sep 06" format
\eASCII escape character (you can also type \033)
\hFirst part of hostname (such as "mybox")
\HFull hostname (such as "mybox.mydomain.com")
\jThe number of processes you've suspended in this shell by hitting ^Z
\lThe name of the shell's terminal device (such as "ttyp4")
\nNewline
\rCarriage return
\sThe name of the shell executable (such as "bash")
\tTime in 24-hour format (such as "23:01:01")
\TTime in 12-hour format (such as "11:01:01")
\@Time in 12-hour format with am/pm
\uYour username
\vVersion of bash (such as 2.04)
\VBash version, including patchlevel
\wCurrent working directory (such as "/home/drobbins")
\WThe "basename" of the current working directory (such as "drobbins")
\!Current command's position in the history buffer
\#Command number (this will count up at each prompt, as long as you type something)
\$If you are not root, inserts a "$"; if you are root, you get a "#"
\xxxInserts an ASCII character based on three-digit number xxx (replace unused digits with zeros, such as "\007")
\\A backslash
\[This sequence should appear before a sequence of characters that don't move the cursor (like color escape sequences). This allows bash to calculate word wrapping correctly.
\]This sequence should appear after a sequence of non-printing characters.

Related Posts:

System