如何不通过 ssh 连接命令传递语言环境

How not to pass the locale through an ssh connection command

我有一些 ssh 的别名,例如:

alias buildWork="ssh work '~/build_app'"

问题是 ssh 命令传递了一些变量,例如 $LC_CTYPE,这导致了一些错误。

如何防止这种情况并使用服务器配置?

听起来您的 SSH 客户端配置为转发区域设置。您可以通过更改配置来防止这种情况(全局文件通常是 /etc/ssh/ssh_config):

# comment out / remove the following line
SendEnv LANG LC_*

或者,您可以通过在远程计算机上编辑 /etc/ssh/sshd_config 来更改服务器的配置(注意 sshd_config 中的 d):

# comment out / remove the following line
AcceptEnv LANG LC_*

简而言之:

$ touch ~/.ssh/config
$ ssh -F ~/.ssh/config your_user@your_host

详情见this answer

   To stop sending Environment Variables via sftp 
   Tested on CENTOS 7
 - create file config in ~/xyzuser/.ssh/config
 - set permission to 600 ~/xyzuser/.ssh/config
 - Put the following content in the file

   comment the below lines commented to disable env variables#########

 - Send locale-related environment variables
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY 
       SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE
       SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
       SendEnv XMODIFIERS

  Running without the ~/xyzuser/.ssh/config
   sftp -v   xyzuser@destinationhost
     -------------------truncated output--------
     debug1: Requesting no-more-sessions@openssh.com
     debug1: Entering interactive session.
     debug1: Sending environment.
     debug1: Sending env LANG = en_US.UTF-8
     debug1: Sending subsystem: sftp

  Running with the ~/xyzuser/.ssh/config

    sftp -v -F /home/xyzuser/.ssh/config  xyzuser@destinationhost

     ----truncated----------
     debug1: channel 0: new [client-session]
     debug1: Requesting no-more-sessions@openssh.com
     debug1: Entering interactive session.
     debug1: Sending subsystem: sftp
     Connected to destinationhost

接受的答案是正确的,但是,如果您不想更改您的配置文件,您可以在命令行上覆盖特定的语言环境

LC_TIME="en_US.UTF-8" ssh user@example.com

我遇到了归因于 ssh 传递区域设置的问题,所以我尝试在客户端站点禁用它,然后在服务器站点上也禁用它(如上所述),但登录后区域设置仍然在抱怨.

结果是目标服务器本身的语言环境混乱,设置取自 /etc/default/locale

我必须彻底清理它,运行 # dpkg-reconfigure locales 解决了问题。

如其他答案中所述,客户端将发送通过 SendEnv/etc/ssh/ssh_config 中指定的所有环境变量。您还可以使用您的用户配置强制 ssh 不发送已定义的变量名称。

来自OpenSSH man page

It is possible to clear previously set SendEnv variable names by prefixing patterns with -. The default is not to send any environment variables.

因此,为了防止发送您的语言环境,您可以将以下内容放入 ~/.ssh/config

SendEnv -LC_* -LANG*

要不发送我们的本地环境 (SendEnv) 这是默认行为,因为它在 /etc/ssh/ssh_config 中指定,您必须:

  1. 为您的用户创建配置文件~/.ssh/config
  2. 添加这些行
Host *
   SendEnv !LANG !LC_*
  1. 重新加载您的 shell sudo su - $YourSelf (或 logout/login)

一点解释

  • ! ==表示不是
  • * ==表示全部

所以

  • !LANG == 不要发送变量 LANG
  • !LC_* == 不要发送所有以 LC_
  • 开头的变量

来源:https://bugzilla.mindrot.org/show_bug.cgi?id=1285#c8

Remember that not everyone with this issue will have the power of editing /etc/ssh/ssh_config