简体   繁体   English

一个SSH密钥,用于多个Ubuntu服务器+ Windows + Teamcity

[英]One SSH Key for multiple Ubuntu servers + Windows + Teamcity

I would like to get some help from you all regarding an issue I'm having at the moment. 对于当前遇到的问题,我想从大家中获得一些帮助。 This is my issue. 这是我的问题。

I want to use the same SSH key to access multiple Ubuntu servers. 我想使用相同的SSH密钥来访问多个Ubuntu服务器。 The reason for this is that I want to deploy our application in DEV, STAGING, PROD, LOCAL environments through a CI server. 原因是我想通过CI服务器在DEV,STAGING,PROD,LOCAL环境中部署我们的应用程序。 We use Ubuntu servers in these environments. 我们在这些环境中使用Ubuntu服务器。 The tech lead asked me to generate one key and use it for all the servers to make the continuous integration easy. 技术负责人要求我生成一个密钥,并将其用于所有服务器,以使持续集成变得容易。 We useTeamcity for CI and the Teamcity server and its agents run in the same Windows server. 我们使用Teamcity for CI,Teamcity服务器及其代理在同一Windows服务器上运行。

I have generated id_rsa and id_rsa.pub key pairs in one server (LOCAL server). 我在一台服务器(本地服务器)中生成了id_rsa和id_rsa.pub密钥对。

The next step is to copy id_rsa.pub to all the other servers and upload the id_rsa file to the Teamcity server. 下一步是将id_rsa.pub复制到所有其他服务器,并将id_rsa文件上传到Teamcity服务器。

I assume that since now the Teamcity server has the id_rsa key it should be able to ssh to Ubuntu servers and issue commands through SSH Exec (Deployer plugin). 我假设从现在开始,Teamcity服务器具有id_rsa密钥,它应该能够SSH到Ubuntu服务器并通过SSH Exec(Deployer插件)发出命令。

Could someone tell me from the SSH key point of view what I have done and assumed so far is correct? 有人可以从SSH密钥的角度告诉我到目前为止我已经做过并假设是正确的吗? The remote servers are Ubuntu and the client (Teamcity server) is a Windows server. 远程服务器是Ubuntu,客户端(Teamcity服务器)是Windows服务器。

As I have described above I tried to connect to the server where I generated the keys from the city server. 如上所述,我尝试连接到服务器,在该服务器上我从城市服务器生成了密钥。 I this case, I uploaded the id_rsa to the teamcity server and tried to connect. 在这种情况下,我将id_rsa上传到teamcity服务器并尝试连接。 I have given the following as the path to the id_rsa file. 我给出以下内容作为id_rsa文件的路径。 I verified that the file actually resided in the following location. 我已验证该文件实际位于以下位置。

E:\\BuildServerConfig\\config\\projects\\MyProject\\pluginData\\ssh_keys where E:\\BuildServerConfig is the Data directory. E:\\ BuildServerConfig \\ config \\ projects \\ MyProject \\ pluginData \\ ssh_keys其中E:\\ BuildServerConfig是数据目录。

But I got the following error 但我收到以下错误

com.jcraft.jsch.JSchException: java.io.FileNotFoundException: E:\\BuildServerConfig\\config\\projects\\MyProject\\pluginData\\ssh_keys (The system cannot find the path specified) com.jcraft.jsch.JSchException:java.io.FileNotFoundException:E:\\ BuildServerConfig \\ config \\ projects \\ MyProject \\ pluginData \\ ssh_keys(系统找不到指定的路径)

I know the path is optional, when I removed the path it gave the following error 我知道路径是可选的,当我删除路径时,出现以下错误

Unexpected error: java.lang.IllegalArgumentException: Argument for @NotNull parameter 'path' of jetbrains/buildServer/util/FileUtil.resolvePath must not be null 意外错误:java.lang.IllegalArgumentException:jetbrains / buildServer / util / FileUtil.resolvePath的@NotNull参数'path'的参数不能为null

Could some please enlighten me on this? 可以请我对此有所启发吗?

You are putting the keys in: E:\\BuildServerConfig\\config\\projects\\MyProject\\pluginData\\ssh_keys The error message says it cannot find the key in: E:\\BuildServerConfig\\config\\projects\\MyProject\\pluginData\\ssh_keys. 您将密钥放入:E:\\ BuildServerConfig \\ config \\ projects \\ MyProject \\ pluginData \\ ssh_keys该错误消息表明无法在以下位置找到密钥:E:\\ BuildServerConfig \\ config \\ projects \\ MyProject \\ pluginData \\ ssh_keys。 That is probably why you get the message that it can't find the path specified. 这可能是为什么您收到一条消息,提示它找不到指定的路径。

The private key (id_rsa) normally stays on the CLIENT. 私钥(id_rsa)通常保留在CLIENT上。 If you put the private key (id_rsa) on the teamcity server, apparently you want to use the teamcity server as the client? 如果将私钥(id_rsa)放在teamcity服务器上,那么显然要使用teamcity服务器作为客户端吗?

The question you ask in the title is how to use multiple UBUNTU servers with a single key 您在标题中提出的问题是如何通过单个密钥使用多个UBUNTU服务器

  1. On the CLIENT machine, create a ssh key-pair: 在CLIENT机器上,创建ssh密钥对:

     ssh-keygen -t rsa 

    This creates a key-pair in hidden directory .ssh in the home directory. 这将在主目录的隐藏目录.ssh中创建密钥对。 The key-pair consists of a secret private key file (usually named 'id_rsa') and a public key file (usually named 'id_rsa.pub') 密钥对包括一个秘密私钥文件(通常称为“ id_rsa”)和一个公钥文件(通常称为“ id_rsa.pub”)

  2. From the CLIENT machine, copy the public key to the SERVER machines 从CLIENT机器,将公钥复制到SERVER机器

      ssh-copy-id username@servermachine 

    The server machine will likely ask you for a password for 'username' to login. 服务器计算机可能会询问您输入“用户名”的密码。 If login is successful, the public key will be copied to the server. 如果登录成功,公钥将被复制到服务器。 On the server, the client public key is added to the file .ssh/authorized_keys in the home directory of username on the server. 在服务器上,客户端公共密钥被添加到服务器上用户名主目录中的文件.ss​​h / authorized_keys中。 Repeat this step for all servers. 对所有服务器重复此步骤。

  3. You can now login from the CLIENT to all SERVERS using a single key. 现在,您可以使用一个键从CLIENT登录所有服务器。 To test this, try on the CLIENT machine: ssh username@servermachine1 Now you should be logged in on servermachine1 without the need for a password, the ssh connection uses the combination of private and public keys. 要对此进行测试,请在CLIENT计算机上尝试:ssh username @ servermachine1现在,您无需密码即可登录servermachine1,ssh连接使用私钥和公钥的组合。 You should repeat this test for servermachine2 to servermachineN 您应该对servermachine2到servermachineN重复此测试

For other operating systems, such as Windows, ssh is not a standard feature. 对于Windows等其他操作系统,ssh不是标准功能。 SSH commands, the names and locations of key files and the key storage format, depend on the ssh software you are using. SSH命令,密钥文件的名称和位置以及密钥存储格式取决于所使用的ssh软件。 The general idea is: 总体思路是:

  • create a key pair (private + public) on the client. 在客户端上创建一个密钥对(私有+公共)。 The key pair belongs to the user creating the pair 密钥对属于创建密钥对的用户
  • copy the public key to the server to a designated key location for the user on the server 将公钥复制到服务器上服务器上用户的指定密钥位置
  • from the client login to the server using the client key-pair and the designated username on the server. 使用客户端密钥对和服务器上的指定用户名从客户端登录名到服务器。 Instead of logging in, you can also copy files or send commands between client and server using the ssh software stack and the keys. 除了登录,您还可以使用ssh软件堆栈和密钥在客户端和服务器之间复制文件或发送命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM