调用 ssh 时是否可以更改 $TERM 的值?

29 linux ssh terminal environment-variables

在我的本地终端上,我有 TERM=konsole-256color,但并非所有我连接的远程机器都有这个定义。

是否可以让 ssh 在远程机器上更改 TERM?无需更改远程计算机上的 .bash* 脚本,只需更改本地桌面上的配置?

Joh*_*han 16

我已将以下别名添加到我的 .bashrc 文件中。它使用 OG 的答案,但将其包装在别名中。奇迹般有效 ;)

# Force xterm-color on ssh sessions
alias ssh='TERM=xterm-color ssh'
Run Code Online (Sandbox Code Playgroud)

  • 在这种情况下,您可以使用以下命令在脚本中包含您的 bash 配置文件或单独的别名脚本: shopt -s expand_aliases; 源 ~/.bash_aliases (2认同)

dse*_*vec 16

2021 年 6 月 4 日,“允许 ssh_config SetEnv 覆盖 $TERM”已提交给 openssh-portable,这听起来像是可以让您在指令中进行SetEnv TERM设置。(或者大概是,一旦发布了此补丁,它就会让您这样做。)~/.ssh/configHost

  • 这是[类似问题的答案](https://serverfault.com/a/971097/202828),其中包含有关“~/.ssh/config”修改的更多详细信息。 (2认同)

Paw*_*cki 9

人 ssh:

     ssh reads ~/.ssh/environment, and adds lines of the format
     “VARNAME=value” to the environment if the file exists and users are
     allowed to change their environment.  For more information, see the
     PermitUserEnvironment option in sshd_config(5).
Run Code Online (Sandbox Code Playgroud)

编辑:

老鼠,我希望它可以在本地,但是,如果有意愿,就有办法。人 ssh_conf:

SendEnv
             Specifies what variables from the local environ(7) should be sent
             to the server.  Note that environment passing is only supported
             for protocol 2.  The server must also support it, and the server
             must be configured to accept these environment variables.  Refer
             to AcceptEnv in sshd_config(5) for how to configure the server.
             Variables are specified by name, which may contain wildcard char-
             acters.  Multiple environment variables may be separated by
             whitespace or spread across multiple SendEnv directives.  The
             default is not to send any environment variables.
Run Code Online (Sandbox Code Playgroud)

根据接收端 sshd 的配置,这可能满足也可能不满足“无远程文件修改”的要求。

  • SendEnv 不相关。它可以*可能*发送额外的环境变量,但是: 1. 它不能修改它们,并且 2. 无论如何都会发送 TERM,即使它没有在 SendEnv 中列出。 (4认同)
  • 是的,但这是远程端的配置,我需要/想要一些仅在本地端更改的内容。 (2认同)

O G*_*O G 8

TERM=vt100 ssh some.host.name
Run Code Online (Sandbox Code Playgroud)

在远程,执行echo $TERM

  • 我虽然考虑过它,但我希望它只为我通过 ssh 连接的主机的子集设置,因此始终发出 TERM=... ssh 将不起作用。并记住哪些主机具有旧的 termcap 信息,然后即时更改命令也不好。 (2认同)

doc*_*hat 6

这是我刚刚拼凑起来的快速而肮脏的解决方案。我更喜欢更好的东西。我想我可以改用 shell 脚本。调整TERM值留给读者作为练习。

# To move into a separate plugin...
function ssh {
   if [[ "${TERM}" = screen* || konsole* ]]; then
     env TERM=screen ssh "$@"
   else
     ssh "$@"
   fi
}
Run Code Online (Sandbox Code Playgroud)

理想情况下,它会做一些事情,比如检查另一端的 TERM,使用这些ControlPersist东西来防止多个连接的长时间延迟。