如何为您的SSH客户端配置自定义连接选项

引言

SSH,又称为安全外壳协议,是远程管理Linux服务器最常用的方式。虽然通过命令行连接单台服务器相对简单,但是连接多个远程系统的工作流程优化有许多。

OpenSSH是大多数系统上最常用的命令行SSH客户端,允许您提供自定义连接选项。这些选项可以保存在一个配置文件中,每个服务器都包含不同的选项。这可以帮助您将用于每个主机的不同连接选项分开和组织,并避免在每次需要连接时在命令行中提供大量选项。

在这个指南中,我们将介绍SSH客户端配置文件的结构,并讨论一些常见选项。

先决条件

完成这个指南,你需要对SSH有一定的工作知识,并了解连接时可以提供的一些选项。你还应该为一些用户或服务器配置基于SSH密钥的身份验证,至少用于测试目的。

SSH配置文件的结构和解释算法

在您的系统上,每个用户都可以在其个人目录中维护自己的SSH配置文件。这些文件可以包含您在命令行上用于指定连接参数的任何选项。在连接时,可以通过在ssh命令中添加附加标志来覆盖配置文件中定义的值。

SSH客户端配置文件的位置

客户端的配置文件位于~/.ssh/config——~是指向您的家目录的通用快捷方式。通常情况下,这个文件不会默认创建,所以您可能需要自己创建它。如果文件不存在,则touch命令将创建它(如果文件已存在,则更新最后修改时间戳)。

  1. touch ~/.ssh/config

 

配置文件结构

配置文件是按主机(即远程服务器)组织的。每个主机定义可以为特定匹配的主机定义连接选项。通配符也支持应用于具有更广泛范围的选项。

每个部分都以一个标题开始,这个标题定义了应该与后续配置选项匹配的主机。然后在下面定义与该匹配主机相关的具体配置项。只需指定与默认值不同的项,因为每个条目将继承任何未定义项的默认值。每个部分跨越从一个主机标题到下一个主机标题。

通常情况下,为了组织目的和提高可读性,对每个主机设定的选项进行缩进处理。这并不是硬性要求,而是一种有用的约定,可以方便快速解读。

常见的格式大致如下:

以下是对~/.ssh/config的中文本地化表述:

用户家目录下的.ssh/config文件。

Host firsthost
    Hostname your-server.com
    User username-to-connect-as
    IdentityFile /path/to/non/default/keys.pem

Host secondhost
    ANOTHER_OPTION custom_value

Host *host
    ANOTHER_OPTION custom_value

Host *
    CHANGE_DEFAULT custom_value

在这里,我们有四个部分,每个连接尝试都将根据所讨论的主机是否匹配而应用。

解释算法

了解SSH如何解读文件并应用配置值是很重要的。在使用通配符和Host *通用主机定义时会产生一些问题。

SSH将会将在命令行中提供的主机名与定义配置部分的每个主机头进行匹配。

以此定义为例,请考虑以下情况:

请把以下内容用中文重新表达一遍,只需要一种版本:
~/.ssh/config将 “~/.ssh/config” 重定义为 “用户主目录/ssh/配置文件”。

Host devel
    HostName devel.example.com
    User tom

通过在命令行上输入以下内容,我们可以作为tom@devel.example.com连接到该主机。

  1. ssh devel

 

SSH从配置文件的顶部开始检查每个主机定义,以查看是否与命令行上给出的值匹配。当找到第一个匹配的主机定义时,将应用与即将建立的连接相关的每个SSH选项。

SSH然后向下移动文件,检查是否还有其他主机定义与之匹配。如果找到另一个与命令行上给出的当前主机名匹配的定义,它将考虑与新部分相关联的SSH选项。然后,它将应用尚未由前面的部分定义的新部分的任何SSH选项。

这是一个重要的要点。SSH会按照顺序解释与命令行中给定主机名相匹配的每个主机部分。在这个过程中,它总是使用每个选项的第一个给定值。没有办法覆盖先前匹配部分已经给定的值。

这意味着你的配置文件应该遵循在最顶端拥有最具体配置的规则。更一般的定义应该放在后面,以便应用前面匹配部分中未定义的选项。

让我们再来看一下前一节中的例子。 yī jié de .)

把以下配置作为中文的译文,只需要一种选择:
~/.ssh/configSSH配置文件的路径为~/.ssh/config。

Host firsthost
    Hostname your-server.com
    User username-to-connect-as
    IdentityFile /path/to/non/default/keys.pem

Host secondhost
    ANOTHER_OPTION custom_value

Host *host
    ANOTHER_OPTION custom_value

Host *
    CHANGE_DEFAULT custom_value

在这里,我们可以看到前两个部分是通过字面主机名(或别名)定义的,意味着它们不使用通配符。如果我们使用ssh连接到firsthost,那么第一个部分将首先应用。这将为该连接设置Hostname、User和IdentityFile。

它将检查第二部分,并发现它不匹配,然后继续向下检查。接着,它会找到第三部分并发现它匹配。它会检查另一个选项,看看它在之前的部分中是否已经有一个值。发现没有,它会应用这部分的值。然后,它会匹配最后一部分,因为Host *的定义匹配所有连接。既然在其他部分中没有关于模拟CHANGE_DEFAULT选项的值,它将采用这部分的值。最终,连接将会使用从此过程中收集到的选项建立。

我们再试一次,假装从命令行调用ssh连接到第二台主机。

再次开始时,它将从第一部分开始检查是否匹配。由于只与 firsthost 相匹配,它将跳过此部分。它将继续进入第二部分。在找到此部分与请求匹配时,它将获取此连接的 ANOTHER_OPTION 的值。

SSH然后查看第三个定义,发现通配符与当前连接匹配。然后它将检查是否已经有了ANOTHER_OPTION的值。由于这个选项在已经匹配的第二节中定义,因此第三节的值被丢弃且无效。

SSH接着检查第四个部分,并应用其中尚未被先前匹配到的部分定义的选项。然后,它尝试使用它收集到的值进行连接。

连接选项

现在你已经知道如何编写配置文件了,让我们讨论一些常见选项以及在命令行中指定它们的格式。

我们首先要介绍的是连接到远程主机所需的最低设置。即,SSH服务器运行的主机名、用户名和端口。

为了以名为apollo的用户连接到名为example.com的主机,该主机在4567端口上运行SSH守护程序,你可以在命令行中运行ssh命令,如下所示:

ssh -p 4567 apollo@example.com

然而,你也可以使用带有-o标志的完整选项名称,像这样使用。

ssh -o "User=apollo" -o "Port=4567" -o "HostName=example.com"

你可以在SSH手册页面中找到所有可用选项的完整列表。

为了在配置文件中设置这些,您必须选择一个主机头名称,例如:home。

Host home
    HostName example.com
    User apollo
    Port 4567

常见的SSH配置选项

到目前为止,我们已经讨论了建立连接所需的一些选项。我们已经涵盖了以下选项:

  • HostName: The actual hostname that should be used to establish the connection. This replaces any alias defined in the Host header. This option is not necessary if the Host definition specifies the actual valid address to connect to.
  • User: The username to be used for the connection.
  • Port: The port that the remote SSH daemon is running on. This option is only necessary if the remote SSH instance is not running on the default port 22.

还有许多其他值得探索的实用选项。根据功能的不同,我们将讨论一些较常见的选项。

一般调整和连接项目

  • ServerAliveInterval: This option can be configured to let SSH know when to send a packet to test for a response from the server. This can be useful if your connection is unreliable and you want to know if it is still available.
  • LogLevel: This configures the level of detail in which SSH will log on the client-side. This can be used for turning off logging in certain situations or increasing the verbosity when trying to debug. From least to most verbose, the levels are QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG1, DEBUG2, and DEBUG3.
  • StrictHostKeyChecking: This option configures whether ssh SSH will ever automatically add hosts to the ~/.ssh/known_hosts file. By default, this will be set to “ask” meaning that it will warn you if the Host Key received from the remote server does not match the one found in the known_hosts file. If you are constantly connecting to a large number of ephemeral hosts (such as testing servers), you may want to turn this to “no”. SSH will then automatically add any hosts to the file. This can have security implications if your known hosts ever do change addresses when they shouldn’t, so think carefully before enabling it.
  • UserKnownHostsFile: This option specifies the location where SSH will store the information about hosts it has connected to. Usually you do not have to worry about this setting, but you may wish to set this to /dev/null so they are discarded if you have turned off strict host checking above.
  • VisualHostKey: This option can tell SSH to display an ASCII representation of the remote host’s key upon connection. Turning this on can be a useful way to get familiar with your host’s key, allowing you to recognize it if you have to connect from a different computer sometime in the future.
  • Compression: Turning compression on can be helpful for very slow connections. Most users will not need this.

考虑到以上配置项,我们可以进行许多有用的配置调整。

例如,如果我们在云服务提供商那里快速地创建和销毁主机,像这样的情况下可能会有用:

Host home
    VisualHostKey yes

Host cloud*
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    LogLevel QUIET

Host *
    StrictHostKeyChecking ask
    UserKnownHostsFile ~/.ssh/known_hosts
    LogLevel INFO
    ServerAliveInterval 120

这将为您的家庭连接打开可视化主机密钥,使您可以熟悉它,以便能够识别是否更改或从不同机器连接时。我们还设置了以cloud*开头的任何主机不检查主机和不记录失败。对于其他主机,我们设置了合理的备用值。

连接转发

SSH的一个常见用途是连接转发,可以让本地连接通过远程主机,也可以让远程机器通过本地机器进行连接。当你需要通过一个单独指定的“网关”服务器连接到一个防火墙后面的远程机器时,这种情况有时是必需的。SSH也可以使用像SOCKS5这样的协议来进行动态转发,其中包含了远程主机的转发信息。

控制这种行为的选项是:

  • LocalForward: This option is used to specify a connection that will forward a local port’s traffic to the remote machine, tunneling it out into the remote network. The first argument should be the local port you wish to direct traffic to and the second argument should be the address and port that you wish to direct that traffic to on the remote end.
  • RemoteForward: This option is used to define a remote port where traffic can be directed to in order to tunnel out of the local machine. The first argument should be the remote port where traffic will be directed on the remote system. The second argument should be the address and port to point the traffic to when it arrives on the local system.
  • DynamicForward: This is used to configure a local port that can be used with a dynamic forwarding protocol like SOCKS5. Traffic using the dynamic forwarding protocol can then be directed at this port on the local machine and on the remote end, it will be routed according to the included values.

这些选项可以用于双向转发端口,如你在这里所看到的。

# This will allow us to use port 8080 on the local machine
# in order to access example.com at port 80 from the remote machine
Host local_to_remote
    LocalForward 8080 example.com:80

# This will allow us to offer access to internal.com at port 443
# to the remote machine through port 7777 on the other side
Host remote_to_local
    RemoteForward 7777 internal.com:443

当您需要打开一个私密仪表板或另一个运行在无法直接访问的服务器上的网络应用程序时,这将特别有用,而唯一可通过SSH进行访问。

其他转发

随着连接转发功能,SSH还允许其他类型的转发。

您可以将存储在您本地计算机代理中的任何SSH密钥转发,从而允许我们使用存储在您本地系统上的凭据连接远程系统。您还可以在远程系统上启动应用程序,并通过X11转发将图形显示转发到我们的本地系统。X11是一个Linux显示服务器,在没有Linux桌面系统的情况下使用起来不太直观,但如果您同时使用远程和本地Linux环境,它可以非常有用。

这些是与这些能力相关的指示。

  • ForwardAgent: This option allows authentication keys stored on our local machine to be forwarded onto the system you are connecting to. This can allow you to hop from host-to-host using your home keys.
  • ForwardX11: If you want to be able to forward a graphical screen of an application running on the remote system, you can turn this option on.

指定键

如果你已经为你的主机配置了SSH密钥,这些选项可以帮助你管理每个主机使用哪些密钥。

  • IdentityFile: This option can be used to specify the location of the key to use for each host. SSH will use keys located in ~/.ssh by default, but if you have keys assigned per-server, this can be used to specify the exact path where they can be found.
  • IdentitiesOnly: This option can be used to force SSH to only rely on the identities provided in the config file. This may be necessary if an SSH agent has alternative keys in memory that are not valid for the host in question.

如果你需要为不同的主机跟踪多个密钥,并使用一个或多个SSH代理进行辅助,那么这些选项尤为有用。

结论

只要你记住SSH解释这些值的方式,你就可以建立丰富的具体值集,并合理设置回退选项。

如果您需要在非常糟糕或间歇性的连接情况下使用SSH,例如飞机上的Wi-Fi网络,您还可以尝试使用mosh,它专为在逆境下使SSH正常工作而设计。

广告
将在 10 秒后关闭
bannerAds