如何自定义在Linux VPS bash提示符

介绍

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

虽然大多数发行版为用户和根提示的样式提供了明智的默认值,但自定义您的提示以添加自己的首选项会很有帮助。 您可以包括大量有用的信息,可以帮助您保持定位,并在您使用提升的权限操作时提醒您。

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

验证您的Shell是否为Bash

我们开始真正定制外壳之前,您应该验证您当前shell实际上Bash。

这对于绝大多数系统应该是正确的,但有时分发维护者选择不同的shell或用户测试一个新的shell。

这是很容易通过检查验证/etc/passwd文件。 在如下所示的打印机中打开文件:

less /etc/passwd

此文件中的每一行包含有关不同用户的信息。 在第一列中找到您的用户和root用户,以冒号(:)分隔。 在最后一个字段中,将列出该用户的默认登录Shell:

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

如果最后一个字段是/bin/bash ,那么你所有的设置。

如果最后一个字段是不是 /bin/bash ,你想改变你的默认的shell来砸,您可以编辑以root特权此文件并更改与用户相关联的最后一个字段:

sudo nano /etc/passwd

在进行更改后,注销并重新使用Bash shell。

查看当前值

为了开始,让我们探索一下我们的配置文件中已经存在的内容来定义Bash提示。

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

PS1定义了您将看到的主要提示。 你每次登录时都会看到。默认情况下,在Ubuntu系统中,这应该采取以下形式:

username@hostname: current_directory$

注意$末。 这表示shell是正常的用户shell。 对于root用户,这被替换为#区分,让你知道你正在使用提升的权限运行。

PS2提示用于多行命令。 您可以通过在终端中键入以下内容来查看当前PS2变量设置:

echo \

直接在反斜杠后面按Enter键即可查看提示。 默认情况下,在Ubuntu,这是>

通常情况下,我们定义什么这些变量将在我们举办~/.bashrc文件,该文件阅读我们的交互shell启动时。

在Ubuntu 12.04这个文件内,我们可以找到一个这样的部分:

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
# force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

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\$ '
fi
unset color_prompt force_color_prompt

我们可以看到一些逻辑指示当提示将被着色。 您需要取消对force_color_prompt=yes ,如果你想有一个彩色的提示行。 现在就这样做,以利用我们以后将要做的自定义。

force_color_prompt=yes

我们关心的部分是设置提示的部分。 这根据是否使用颜色,嵌套在具有不同提示的if-else结构中:

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\$ '
fi
unset color_prompt force_color_prompt

顶部添加了颜色支持。 让我们来看看第二部分,没有颜色,先掌握一些基础知识:

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

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

包括零件debian_chroot表明,如果你是在一个根的变化环境中运行,该提示将被修改来提醒你。 您可能希望保持这部分提示完整,因为这是一个有用的功能。

其余的提示定义如下所示:

\u@\h:\w\$: 

这描述了我们一直看到的主要提示,使用一些转义序列。

Bash逃生序列

我们可以在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),最后是普通用户的“$” “为root用户。

让我们将退出的~/.bashrc文件,以便我们可以测试一些其他选项。

测试新的Bash提示符

虽然最终我们将要编辑我们~/.bashrc文件,以使我们的选择永久性的,它是非常容易使用的命令行本身的变化及时进行实验。

在我们开始修改的东西,让我们拯救我们的电流值PS1到一个新的变量。 这将允许我们切换回到我们原来的提示更容易,而不必退出和重新进入,以防我们犯错误。

ORIG=$PS1

现在,我们有一个环境变量称为ORIG ,将节省我们的默认提示是一个副本。

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

PS1=$ORIG

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

PS1="\u$"

我们应该得到这样的东西:

demouser$

让我们的空间,我们有点,使它看起来更好:

PS1="\u $: "
demouser $:

我们可能不想使用“$”文字字符。 我们应该使用\$转义序列来代替。 这将根据我们是否为root动态修改提示,允许我们正确使用我们的PS1:

PS1="\u \$: "

我们可以在我们的提示中添加任何我们想要的文字字符串:

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

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

在这里,我们可以在提示符下在拉发现的负荷指标的第一列给我们的服务器的当前负载/proc/loadavg使用反引号插入命令的结果:

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

这是一个很好的方式来知道,如果你是税系统。

如果日期或时间对于您在提示中很重要,您可以尝试这样的。 让我们也分隔我们的数据有些括号和括号保持有组织。 让我们也添加\w早在跟踪我们的工作目录:

PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t) \w \$ "
[demouser@host, load: 0.01] (Thu Feb 20 - 13:15:20) ~ $

这开始有点尴尬,特别是如果我们改变目录到一个长路径的东西:

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

如果我们仍然希望所有这些信息,但要使它更短,一种策略是分两条线带之间的信息\n换行符:

PS1="[\u@\h, load: `cat /proc/loadavg | awk '{ print $1; }'`] (\d - \t)\n\w \$ "
[demouser@host, load: 0.00] (Thu Feb 20 - 13:20:00)
/etc/systemd/system/multi-user.target.wants $

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

更改提示颜色

现在我们已经掌握了不同的方式,我们可以影响我们的提示,让我们尝试一些着色。

Bash允许你通过使用特殊的代码在你的提示中引入颜色。 这些常常是一个混乱的根源,因为选择的代码不是很描述。

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

首先,你必须在包装您的整个颜色代码描述\[\]标签。 这些括号表示将第一个序列之前存在的字符直到最后一个序列视为非打印字符。

Bash需要知道这一点,以便它可以估计在打包到下一行之前允许打印多少字符。 如果你不括在你的颜色代码\[\]标签,Bash将计算所有的字符作为文字字符,并会太快换行。

其次,支架非打印序列中,您需要通过键入要么指定颜色提示的开始\e[\033[ 这两者做同样的事情,并指示转义序列的开始。 我将使用\e[本指南中,因为我认为这是一个有点更加明确。

在之前\]你需要一个“M”,表明你是给一个色序。

所以基本上,每次我们要修改颜色,我们必须在我们的提示符中输入以下内容:

\[\e[color_informationm\]

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

对于颜色代码,改变前景文本颜色的基本代码如下:

  • 30:黑色
  • 31:
  • 32:绿色
  • 33:黄色
  • 34:
  • 35:
  • 36:青色
  • 37:白色

您还可以通过在基本值之前设置“属性”(以分号(;)分隔)来修改这些基本值。

根据您使用的终端类型,这些终端的行为可能会有所不同。 一些更常见的属性是:

  • 0:正常文本
  • 1:粗体或浅,这取决于终端
  • 4:下划线的文本

所以如果你想下划线绿色文本,你会使用一个这样的序列:

\[\e[4;32m\]

然后我们输入我们想要的提示。 之后,我们可能想将颜色重置为其原始值,以便我们输入到终端的文本不会奇怪。

我们可以使用另一个颜色代码来表示Bash应该重置提示颜色。 这段代码是:

\[\e[0m\]

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

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

我们还可以指定背景颜色以获得更多的复杂性。 背景颜色不能带属性。 他们是:

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

虽然你可以一次指定背景颜色,属性和文本颜色,如下所示:

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

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

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

有时,如果你只是使用“正常”文本属性(0),在一些终端的着色会被乱码。 你可以通过不指定正常值为0来避免这种情况,因为它是默认值。

使您的提示更改永久

现在我们已经玩了一会儿,我们应该有一个好主意,我们想要我们的提示。 您应该能够创建彩色提示和无彩色提示。

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

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

我们将使用它的非彩色等效,因为当我们不希望有一个彩色bash提示:

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

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

nano ~/.bashrc

正如我们在开始讨论的,我们文件中的提示现在已经包括了功能,使其在我们在chroot环境中显而易见。 让我们离开这部分。它现在看起来像这样:

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\$ '
fi
unset 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

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

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

结论

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

另一个流行的想法是为root用户提供特殊提示,以便对比将提醒您的权限。 获得创意,并尝试找到适用于您的有用信息和混乱之间的平衡。

作者:Justin Ellingwood
赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏