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/hostnameThis 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.
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.
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.
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"
;;
*)
;;
esacIf 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.
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.
| Sequence | Description |
| \a | The ASCII bell character (you can also type \007) |
| \d | Date in "Wed Sep 06" format |
| \e | ASCII escape character (you can also type \033) |
| \h | First part of hostname (such as "mybox") |
| \H | Full hostname (such as "mybox.mydomain.com") |
| \j | The number of processes you've suspended in this shell by hitting ^Z |
| \l | The name of the shell's terminal device (such as "ttyp4") |
| \n | Newline |
| \r | Carriage return |
| \s | The name of the shell executable (such as "bash") |
| \t | Time in 24-hour format (such as "23:01:01") |
| \T | Time in 12-hour format (such as "11:01:01") |
| \@ | Time in 12-hour format with am/pm |
| \u | Your username |
| \v | Version of bash (such as 2.04) |
| \V | Bash version, including patchlevel |
| \w | Current working directory (such as "/home/drobbins") |
| \W | The "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 "#" |
| \xxx | Inserts 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. |
