如果本地和远端服务器用户名和主机名,终端颜色配置都一致,通过ssh登陆服务器的时候,在本地终端和ssh终端来回切换时,傻傻分不清当前终端是本地终端还是ssh远程服务器。
下面这个连接介绍了一种方法,是在客户端执行ssh的时候特别标记一下是ssh,这个方法稍微有点复杂,主要还是要一个脚本和别名:
区分本地终端和远程终端_xunknown的博客-CSDN博客
这个连接介绍了一种在.bashrc根据SSH相关环境变量区分是否SSH连接的方法,很有参考意义。
Detect ssh login from ~/.bashrc
下面按照第二种方法,配置.bashrc,用于区分本地登陆还是ssh远程登陆。
这里基于ssh登陆时,bash会自动定义3个SSH相关的环境变量,例如:
SSH_CONNECTION=127.0.0.1 55670 127.0.0.1 22
SSH_CLIENT=127.0.0.1 55670 22
SSH_TTY=/dev/pts/1
下面就是利用SSH_CLIENT变量是否定义,修改~/.bashrc文件,在PS1增加一个SSH客户端IP作为前缀,用于区分本地终端和SSH远程登陆。
下面是相关的修改片段,主要是定义两个变量ssh_prompt和color_ssh_prompt,
ssh_prompt根据SSH_CLIENT环境变量是否定义确定,如果定义了,则取其第一个空格前的一部分(默认是SSH客户端IP地址),否则定义为空。
color_ssh_prompt则加上bash的字体颜色定义,这里配置为黄色。
然后把这两个变量放在PS1定义的合适位置(需要注意PS1定义的字符串要改成双引号定义)。
# alpha@2023.03.26: PS1 adds SSH_CLIENT IP as a prefix
ssh_prompt="${SSH_CLIENT:+[${SSH_CLIENT%% *}] }"
color_ssh_prompt="\[\033[1;33m\]${ssh_prompt}\[\033[0;33m\]"
if [ "$color_prompt" = yes ]; then
if [[ ${EUID} == 0 ]] ; then
PS1="${debian_chroot:+($debian_chroot)}${color_ssh_prompt}\[\033[01;31m\]\H\[\033[01;34m\] \W \$\[\033[00m\] "
else
PS1="${debian_chroot:+($debian_chroot)}${color_ssh_prompt}\[\033[01;32m\]\u@\H\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] "
fi
else
PS1="${debian_chroot:+($debian_chroot)}${ssh_prompt}\u@\H \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)}${ssh_prompt}\u@\H \w\a\]$PS1"
;;
*)
;;
esac
unset ssh_prompt color_ssh_prompt
修改后,ssh登陆效果如下,终端提示符前多一个IP前缀:
alpha@mascot ~ $ ssh 127.0.0.1
alpha@127.0.0.1's password:
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.19.0-35-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
* Introducing Expanded Security Maintenance for Applications.
Receive updates to over 25,000 software packages with your
Ubuntu Pro subscription. Free for personal use.
https://ubuntu.com/pro
Expanded Security Maintenance for Applications is not enabled.
7 updates can be applied immediately.
1 of these updates is a standard security update.
To see these additional updates run: apt list --upgradable
16 additional security updates can be applied with ESM Apps.
Learn more about enabling ESM Apps service at https://ubuntu.com/esm
Last login: Sun Mar 26 21:42:07 2023 from 127.0.0.1
[127.0.0.1] alpha@mascot ~ $
[127.0.0.1] alpha@mascot ~ $
[127.0.0.1] alpha@mascot ~ $ exit
logout
Connection to 127.0.0.1 closed.
alpha@mascot ~ $
alpha@mascot ~ $
有时候我们更关心的是远程服务器是哪个,由于主机名称相同区分不出来,那么就把服务器的IP放到PS1提示符位置上,因此可以修改ssh_prompt定义为SSH服务器IP。
下面从SSH_CONNECTION环境变量取出SSH服务器的IP地址,放到PS1上(在这里,把数组下标2改成0,就是SSH客户端的IP地址):
# alpha@2023.03.26: PS1 adds SSH Server IP as a prefix
# SSH_CONNECTION=<client ip> <client port> <server ip> <server port>
if read -a ssh_conn <<< ${SSH_CONNECTION} && [ -n "${ssh_conn[2]}" ]; then
ssh_prompt="${ssh_conn[2]:+[${ssh_conn[2]}]} "
color_ssh_prompt="\[\033[1;33m\]${ssh_prompt}\[\033[0;33m\]"
fi
if [ "$color_prompt" = yes ]; then
if [[ ${EUID} == 0 ]] ; then
PS1="${debian_chroot:+($debian_chroot)}${color_ssh_prompt}\[\033[01;31m\]\H\[\033[01;34m\] \W \$\[\033[00m\] "
else
PS1="${debian_chroot:+($debian_chroot)}${color_ssh_prompt}\[\033[01;32m\]\u@\H\[\033[00m\] \[\033[01;34m\]\w \$\[\033[00m\] "
fi
else
PS1="${debian_chroot:+($debian_chroot)}${ssh_prompt}\u@\H \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)}${ssh_prompt}\u@\H \w\a\]$PS1"
;;
*)
;;
esac
unset ssh_prompt color_ssh_prompt
效果如下:
alpha@mascot ~ $ ssh 192.168.2.102
alpha@192.168.2.102's password:
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-60-generic x86_64)
...
Last login: Sun Mar 26 15:36:20 2023 from 192.168.2.103
[192.168.2.102] alpha@mascot ~ $
[192.168.2.102] alpha@mascot ~ $
[192.168.2.102] alpha@mascot ~ $ exit
logout
Connection to 192.168.2.102 closed.
alpha@mascot ~ $
alpha@mascot ~ $
alpha@mascot ~ $
也可以使用一个命令直接查询,为这个命令设置别名:
alias lh='read -a ssh_conn <<< ${SSH_CONNECTION} && echo ${ssh_conn[2]:-"localhost"}'
或者
alias lh='read -a ssh_conn <<< ${SSH_CONNECTION} && echo ${ssh_conn[2]:-`hostname`}'