SSH 配置中的当前目录

KNe*_*jad 7 ssh ssh-keys

我有一个项目,id_rsa其中包含所有文件和 SSH 配置,其中的条目如下所示:

Host projectname-server-1
  User root
  Hostname 26.139.18.47
  IdentityFile ./certificates/server-1/id_rsa
Run Code Online (Sandbox Code Playgroud)

这个想法是用户可以从他们的主 SSH 配置文件中包含此配置,如下所示:

Include /home/myname/path-to-project/ssh-config
Run Code Online (Sandbox Code Playgroud)

然后只需运行即可ssh projectname-server-1连接到服务器。

问题是这仅当我在目录中时才有效/home/myname/path-to-project

有什么方法可以指定相对于配置文件位置的路径吗?

har*_*ymc 5

我不相信这样的“当前目录”变量存在。

ssh_config (5) - Linux 手册页 列出了可能有帮助的语法:

文件名可以使用波形符语法来引用用户的主目录或以下转义字符之一:(%d本地用户的主目录)、%u(本地用户名)、%l(本地主机名)、%h(远程主机名)或%r(远程用户名)。


Mat*_*tto 5

听起来您想要的是一种将ssh项目的所有配置和变量存储在项目目录中的方法,并且相对于包含的ssh 配置文件而不是当前路径来处理路径。

\n

简短的答案是否定的- AFAIK,ssh_config包含的行为类似于C #include- 您可以将它们视为将引用的文件扩展到当前文件。

\n

话虽这么说,您有一些选择可以以可扩展的方式完成此任务,并具有一定程度的约定 - 我个人对我的很多ssh配置进行版本控制,我最好的建议是:

\n

在项目根目录下,将ssh配置存储在一个目录下

\n
my_project/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _config.yml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _ssh_config\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 id_rsa\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1.config\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\n3 directories, 3 files\n
Run Code Online (Sandbox Code Playgroud)\n

server-1.config像这样写:

\n
Host projectname-server-1\n  User root\n  Hostname server-1.example.com\n  # Our identity file is found in folder under ~/.ssh\n  IdentityFile ~/.ssh/config.d/server-1/id_rsa\n
Run Code Online (Sandbox Code Playgroud)\n

~/.ssh/在新建一个文件夹下,config.d

\n
my_project/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _config.yml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 _ssh_config\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 id_rsa\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1.config\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 src\n\n3 directories, 3 files\n
Run Code Online (Sandbox Code Playgroud)\n

要引用您的项目特定的 ssh 文件,您可以将您的符号链接my_project/_ssh_config/server-1config.d

\n
Host projectname-server-1\n  User root\n  Hostname server-1.example.com\n  # Our identity file is found in folder under ~/.ssh\n  IdentityFile ~/.ssh/config.d/server-1/id_rsa\n
Run Code Online (Sandbox Code Playgroud)\n

更新您的Include指令~/.ssh/config

\n
# Relative "Include" paths are assumed to be relative to ~/.ssh/\nInclude config.d/server-1/server-1.config\n
Run Code Online (Sandbox Code Playgroud)\n

现在,你的~/.ssh样子是这样的:

\n
.ssh/\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 config.d\n    \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1 -> /path/to/my_project/_ssh_config/server-1/\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 id_rsa\n        \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 server-1.config\n\n2 directories, 3 files\n
Run Code Online (Sandbox Code Playgroud)\n

这种方法的结果是您可以轻松地将其扩展到server-2、3、4 等或其他项目。只需符号链接path/to/server-xconfig.d,您就可以在用户级别中使用 globInclude来处理它:

\n
Include config.d/**/*.config\n
Run Code Online (Sandbox Code Playgroud)\n

我建议ssh在项目中存储密钥听起来非常规,有潜在危险,而且不是特别有用。如果您的目标是配置的可移植性,我建议您对私钥的路径进行例外处理。

\n