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       localhost       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\]\$ '
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    # PS1='${debian_chroot:+($debian_chroot)}[\u]:\w\$ '
unset color_prompt force_color_prompt

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

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. This is from 'bash's manpage:

       When executing interactively, bash displays the primary prompt PS1 when
       it is ready to read a command, and the secondary  prompt  PS2  when  it
       needs  more  input  to  complete  a  command.  Bash allows these prompt
       strings to be customized by inserting  a  number  of  backslash-escaped
       special characters that are decoded as follows:
              \a     an ASCII bell character (07)
              \d     the  date  in "Weekday Month Date" format (e.g., "Tue May
                     the format is passed to strftime(3)  and  the  result  is
                     inserted  into the prompt string; an empty format results
                     in a locale-specific time representation.  The braces are
              \e     an ASCII escape character (033)
              \h     the hostname up to the first `.'
              \H     the hostname
              \j     the number of jobs currently managed by the shell
              \l     the basename of the shell's terminal device name
              \n     newline
              \r     carriage return
              \s     the  name  of  the shell, the basename of $0 (the portion
                     following the final slash)
              \t     the current time in 24-hour HH:MM:SS format
              \T     the current time in 12-hour HH:MM:SS format
              \@     the current time in 12-hour am/pm format
              \A     the current time in 24-hour HH:MM format
              \u     the username of the current user
              \v     the version of bash (e.g., 2.00)
              \V     the release of bash, version + patch level (e.g., 2.00.0)
              \w     the current working  directory,  with  $HOME  abbreviated
                     with  a tilde (uses the value of the PROMPT_DIRTRIM vari‐
              \W     the basename of the current working directory, with $HOME
                     abbreviated with a tilde
              \!     the history number of this command
              \#     the command number of this command
              \$     if the effective UID is 0, a #, otherwise a $
              \nnn   the character corresponding to the octal number nnn
              \\     a backslash
              \[     begin  a sequence of non-printing characters, which could
                     be used to embed a terminal  control  sequence  into  the
              \]     end a sequence of non-printing characters

       The  command  number  and the history number are usually different: the
       history number of a command is its position in the history list,  which
       may  include  commands  restored  from  the  history  file (see HISTORY
       below), while the command number is the position  in  the  sequence  of
       commands  executed  during the current shell session.  After the string
       is decoded, it is expanded via parameter expansion,  command  substitu‐
       tion,  arithmetic expansion, and quote removal, subject to the value of
       the promptvars shell option (see the description of the  shopt  command
       under SHELL BUILTIN COMMANDS below).

Source: http://manpages.ubuntu.com/manpages/oneiric/en/man1/bash.1.html

Related Posts: