正如如何检测外壳是否受 SSH 控制?,如果设置了变量$SSH_CLIENT
或$SSH_TTY
,则表示您正在通过 SSH 连接。
如果您在基于 Debian 的系统(例如 Ubuntu)上,您可以将您.bashrc
的内容编辑为类似的内容以达到所需的效果(请注意,PS1
设置为的字符串必须用双引号定义,而不是单引号为默认情况下):
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then
if [ "$color_prompt" = yes ]; then
host="@\[\033[1;34m\]\h\[\033[00m\]"
else
host="@\h"
fi
fi
if [ "$color_prompt" = yes ]; then
PS1="${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]${host}:\[\033[01;34m\]\w\[\033[00m\]\$ "
else
PS1="${debian_chroot:+($debian_chroot)}\u${host}:\w\$ "
fi
unset host
unset color_prompt force_color_prompt
结果如下:

旁注:这些更改应在您通过 SSH连接的.bashrc
服务器上(或.profile
,取决于发行版)进行。在您自己的本地 Bash 配置文件中设置它们对连接到其他远程服务器时显示的内容没有影响。