如何在 Linux VPS 上自定义 Bash 提示符

介绍


当您管理 Linux 服务器时,您将花费相当长的时间使用命令行,对于大多数人来说,这意味着花费大量时间使用 Bash 壳。

虽然大多数发行版提供合理的默认设置,用于用户和 root 提示,但自定义您的提示可以帮助您添加自己的偏好,您可以包含大量有用的信息,可以帮助您保持定位并提醒您,当您使用高级特权时。

我们将使用Ubuntu 12.04 VPS进行实验,但几乎所有的现代Linux发行版都应该以类似的方式运作。

验证你的壳是Bash


在我们开始实际定制壳之前,您应该验证您的当前壳实际上是 Bash。

这应该适用于绝大多数系统,但有时分布维护者选择不同的壳,或者用户测试一个新的壳。

很容易通过检查 /etc/passwd 文件来验证。

1less /etc/passwd

此文件中的每个行都包含有关不同用户的信息. 在第一个列中找到您的用户和根用户,分隔一个(:)。

1root:x:0:0:root:/root:/bin/bash
2. . .
3demouser:x:1000:1000:,,,:/home/demouser/bin/bash

如果最后一个字段是/bin/bash,那么你已经设置了。

如果最后一个字段是 not /bin/bash,并且您希望将默认壳更改为Bash,您可以用根权限编辑此文件,并更改与您的用户相关的最后一个字段:

1sudo nano /etc/passwd

更改后,退出并重新登录以使用Bash壳。

观察当前价值观


要开始,让我们探索已经在我们的配置文件中是什么来定义Bash提示。

Bash 通过使用PS1PS2环境变量来配置其提示。

PS1 定义了你会看到的主要提示,你每次登录时都会看到这个提示,默认情况下,在 Ubuntu 系统中,这应该是:

username@hostname: current_directory$

这意味着壳是一个正常的用户壳,对于根用户来说,这被替换为一个# ,以区分并让您意识到您正在使用高级特权。

PS2 提示用于多行命令. 您可以通过在终端中键入以下方式查看当前 PS2 变量设置为何:

1echo \

点击后直接输入以查看提示. 默认情况下,在Ubuntu中,这是>

通常,我们在我们的 ~/.bashrc 文件中定义这些变量将包含什么,当我们的交互壳开始时读取它。

在 Ubuntu 12.04 上的这个文件中,我们可以找到一个类似于此的部分:

 1# uncomment for a colored prompt, if the terminal has the capability; turned
 2# off by default to not distract the user: the focus in a terminal window
 3# should be on the output of commands, not on the prompt
 4# force_color_prompt=yes
 5
 6if [ -n "$force_color_prompt" ]; then
 7    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
 8        # We have color support; assume it's compliant with Ecma-48
 9        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
10        # a case would tend to support setf rather than setaf.)
11        color_prompt=yes
12    else
13        color_prompt=
14    fi
15fi
16
17if [ "$color_prompt" = yes ]; then
18    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
19else
20    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
21fi
22unset color_prompt force_color_prompt

我们可以看到某些逻辑决定什么时候提示将被彩色。如果您想要彩色提示,您需要删除force_color_prompt=yes行。

1force_color_prompt=yes

我们关心的部分是设置提示符的部分,这是嵌入在一个 if-else 构造中,根据您是否使用颜色而使用不同的提示符:

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

上面的部分添加了色彩支持,让我们看看第二个部分,没有色彩,首先了解一些基本知识:

1PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '

这看起来相当复杂,有一些部件似乎不匹配我们在正常壳使用中看到的任何东西。

包含「debian_chroot」的部分表明,如果您在更改根环境中运行,则提示会被修改以提醒您。

其余的快速定义看起来是这样的:

1\u@\h:\w\$:

这描述了我们整个时间所看到的主要提示,使用了一些逃跑序列。

别名:Bash Escape Sequences


我们可以在Bash男人页面中找到可能的逃跑序列的完整列表:

    \a an ASCII bell character (07)
    \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
    \D{format}
           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 required
    \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 variable)
    \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 prompt
    \]     end a sequence of non-printing characters

正如你所看到的,有一些基本信息,还有一些你可能不需要的信息(ASCII铃声字符、Bash版本等)。

我们的提示现在有用户名(\u),一个 @ 符号,主机名(\h)的第一部分,当前的工作目录(\w),最后,普通用户的$和根用户的#

让我们离开 ~/.bashrc 文件,以便我们可以测试其他一些选项。

测试新的Bash Prompts


虽然最终我们会想要编辑我们的 ~/.bashrc 文件以使我们的选择永久化,但更容易尝试从命令行本身更改提示。

在我们开始修改事物之前,让我们将我们当前的PS1值保存到一个新的变量中,这将使我们更容易返回原来的提示,而无需退出并再次登录,如果我们犯了一个错误。

1ORIG=$PS1

现在,我们有一个名为ORIG的环境变量,它将保存我们的默认提示的副本。

如果我们需要返回原始提示,我们可以键入:

1PS1=$ORIG

让我们从简单的开始,只需要我们的用户名和实际提示的$:

1PS1="\u$"

我们应该得到一个看起来像这样的东西:

1demouser$

让我们空间,我们的一点,使它看起来更漂亮:

1PS1="\u $: "

1demouser $:

我们可能不想使用$字面字符,但我们应该使用\$逃跑序列,这将根据我们是否是根动态地修改提示,允许我们的PS1作为根正确使用:

1PS1="\u \$: "

我们可以添加我们想要的任何字母字符串:

1PS1="Hello, my name is \u! \$: "

1Hello, my name is demouser! $:

我们还可以使用常规壳功能插入任意命令的结果。

在这里,我们可以用我们的提示给出我们服务器的当前负载,通过将在 `/proc/loadavg 中发现的负载指标的第一个列拉来插入命令的结果:

1PS1="\u, load: `cat /proc/loadavg | awk '{ print $1; }'` \$: "

1demouser, load: 0.01 $:

这是一个好方法来知道你是否正在征税你的系统。

如果日期或时间对你来说很重要,你可以尝试一些这样的东西。我们也将我们的数据分区一点,用一些支架和子来保持组织。

1PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t) \w \$ "

1[demouser@host, load: 0.01] (Thu Feb 20 - 13:15:20) ~ $

这开始变得有点尴尬,特别是如果我们将目录更改为有很长的路径的东西:

1cd /etc/systemd/system/multi-user.target.wants

1[demouser@host, load: 0.01] (Thu Feb 20 - 13:18:28) /etc/systemd/system/multi-user.target.wants $

如果我们仍然想要所有这些信息,但想要使其更短,一个策略是用\n新线字符将信息分为两行:

1PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t)\n\w \$ "

1[demouser@host, load: 0.00] (Thu Feb 20 - 13:20:00)
2/etc/systemd/system/multi-user.target.wants $

有些人不喜欢多行提示,但这是在提示中提供更多信息的一种方式。

改变快速颜色


现在,我们对我们可以影响我们的提示的不同方式有了很好的了解,让我们尝试一些颜色。

Bash 允许您使用特殊代码将颜色输入您的提示,这些代码往往是混淆的来源,因为所选择的代码不太描述性。

在我们走到实际的颜色代码之前,我们应该谈谈我们如何实际地实施它们.在Bash设置中定义颜色代码有正确和错误的方式。

首先,您必须将您的整个颜色代码描述包装到 [ 和 `] 标签中,这些标签表示,在第一个序列到最后一个序列之后存在的字符应该被视为非打印字符。

Bash需要知道这一点,以便它可以估计打印的字符数目,然后将其包裹到下一行。

其次,在不打印序列中,您需要通过键入e033来指定颜色提示的开始。

\]之前,您需要一个m来表示您正在给出一个颜色序列。

因此,基本上,每次我们想要修改颜色,我们必须在我们的提示中输入一些看起来像:

\[\e[color_informationm\]

正如你所看到的,这是使我们的提示特别混乱的部分。

至于颜色代码,基本代码来改变前台文本的颜色是以下:

黑色: 黑色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 红色: 蓝色: 蓝色: 蓝色: 蓝色: 蓝色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色: 白色:

您还可以通过设置一个属性来修改这些基数值,以半列(;)隔开的基数值。

取决于您使用的终端类型,这些终端可能会有所不同,其中一些更常见的属性是:

  • 0 :正常文本 * ** 1** :大胆或轻,取决于终端 * ** 4** :突出文本

所以,如果你想强调绿色文本,你会使用这样的序列:

1\[\e[4;32m\]

之后,我们可能希望将颜色重置回原始值,以便我们输入到终端的文本不会有奇怪的颜色。

我们可以通过使用另一个颜色代码来做到这一点,该代码表示Bash应该重置提示颜色。

1\[\e[0m\]

这样一来,一个简单的彩色提示,带有用户名和主机,可以看起来像这样:

1PS1="\[\e[4;32m\]\u@\h\[\e[0m\]$ "

我们还可以指定背景颜色,以便更复杂。背景颜色不能采用属性。

  • 40**: 黑色背景 * 41** : 红色背景 * 42** : 绿色背景 * 43** : 黄色背景 * 44** : 蓝色背景 * 45** : 紫色背景 * 46** : 白色背景 * 47** : 白色背景

虽然您可以指定背景颜色、属性和文本颜色,但所有内容都如下:

1\[\e[42;1;36m\]

通常,如果您将背景信息与其他信息分开,它会更好地工作:

1\[\e[42m\]\[\e[1;36m\]

有时,如果你只是使用正常文本属性(0),颜色在某些终端中会变色,你可以通过不指定正常值为0来避免这种情况,因为它是默认值。

使您的快速更改永久化


现在我们已经玩了我们的快递时间,我们应该有一个很好的想法,我们希望我们的快递看起来如何. 你应该能够创建一个有色和无色快递。

对于我们的例子,我们将使用以下彩色提示:

1PS1="[\[\e[0;32m\]\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`\[\e[00m\]] (\[\e[00;35m\]\d - \t\[\e[00m\])\n\w \$ "

我们也将使用非彩色等级,因为当我们不希望有彩色 bash 提示时:

1PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t)\n\w \$ "

现在我们有我们想要的提示的两种版本,我们可以编辑我们的~/.bashrc文件中的PS1

1nano ~/.bashrc

正如我们在开始时所讨论的那样,我们文件中的提示现在包含了功能,使我们在chroot环境中时显而易见。

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

评论当前的PS1任务,并复制下面的debian_chroot逻辑,使其看起来像这样:

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

在提示的末尾,就在最后一个报价关闭之前,我们可以添加我们想要实施的报价,实际上,由于我们的报价使用单个报价,我们希望在当前报价中使用双报价的报价类型。

在第一个PS1任务中,使用您的提示的彩色版本;在第二个任务中,使用非彩色版本。

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)}[\[\e[0;32m\]\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`\[\e[00m\]] (\[\e[00;35m\]\d - \t\[\e[00m\])\n\w \$ "
else
    # PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS1="${debian_chroot:+($debian_chroot)}[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t)\n\w \$ "
fi
unset color_prompt force_color_prompt

当我们完成时,我们可以关闭并保存文件。

现在,当您退出并重新登录时,您的提示将更改为您设置的值。

结论


有许多不同的方式,您可以个性化您的配置. 颜色某些项目可以使它们脱颖而出,并可以帮助您在滚动终端历史时找到最后一个提示。

另一个流行的想法是为根用户提供一个特殊的提示,这样对比会提醒你你的特权。

By Justin Ellingwood
Published At
Categories with 技术
Tagged with
comments powered by Disqus