区分本地终端和ssh远程终端

 如果本地和远端服务器用户名和主机名,终端颜色配置都一致,通过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`}'

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Mac终端上配置私钥进行SSH远程连接服务器,你需要按照以下步骤进行操作: 1. 在终端中执行以下命令来创建一个.ssh目录: ``` mkdir ~/.ssh ``` 2. 将你的密钥对文件移动到.ssh目录下。假设你的密钥对文件名为MyKeyPair.pem,你可以使用以下命令将其移动到.ssh目录下: ``` mv ~/Downloads/MyKeyPair.pem ~/.ssh/MyKeyPair.pem ``` 3. 使用chmod命令来限制私有SSH密钥的权限,确保它不对公开可见: ``` chmod 400 ~/.ssh/MyKeyPair.pem ``` 4. 现在你可以使用密钥连接到远程服务器了。具体的连接命令可以根据你要连接的服务器和使用的密钥而有所不同。 以上是在Mac终端上配置私钥进行SSH远程连接服务器的步骤。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [mac ssh 密钥登陆远程服务器](https://blog.csdn.net/Guzarish/article/details/119163774)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [mac使用ssh密钥连接云服务器](https://blog.csdn.net/m0_46391989/article/details/127460244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值