在 centos 上设置 jenkins:ssh 密钥和 git 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12599077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:32:42  来源:igfitidea点击:

Setting up jenkins on centos: problems with ssh keys and git

gitsshjenkinscentosssh-keys

提问by Tadzys

Went through a lot of questions, but nothing seems to be solving my issue. Or to be more precise I am not sure if I am doing the whole thing correctly. So here it is:

经历了很多问题,但似乎没有解决我的问题。或者更准确地说,我不确定我是否正确地完成了整件事。所以这里是:

Have installed centos 6.3 OS. Then I followed the following guide to install jenkins:

已安装centos 6.3 OS。然后我按照以下指南安装 jenkins:

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+RedHat+distributions

https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins+on+RedHat+distributions

Jenkins works fine. Now I am trying to set up a simple build job, which requires to clone a git repository. (I've installed git plugin)

詹金斯工作正常。现在我正在尝试设置一个简单的构建作业,它需要克隆一个 git 存储库。(我已经安装了 git 插件)

In repository URL i type the following: git@gitserver:myrepo.git Of course I get an error: stderr: Host key verification failed.

在存储库 URL 中,我键入以下内容: git@gitserver:myrepo.git 当然我得到一个错误:stderr:主机密钥验证失败。

ok, I need to generate ssh keys and all will be good. So I do the following:

好的,我需要生成 ssh 密钥,一切都会好起来的。所以我执行以下操作:

su - jenkins

but unfortunately it doesn't switch to jenkins user.

但不幸的是它没有切换到 jenkins 用户。

cat /etc/passwd

猫 /etc/passwd

shows the following:

显示以下内容:

jenkins:x:496:492:Jenkins Continuous Build Server:/var/lib/jenkins:/bin/false

so seems that it doesn't have a usual home directory.

所以似乎它没有一个通常的主目录。

The question is how do I generate keys for jenkins or if the above steps where not the right way to do it, how do I fix it?

问题是我如何为 jenkins 生成密钥,或者如果上述步骤不是正确的方法,我该如何解决?

Thanks a lot!

非常感谢!

Update: I generated keys (as a root user) and placed them in jenkins home and did exactly and copied public key to git server. Still didn't help.

更新:我生成了密钥(作为 root 用户)并将它们放在 jenkins home 中,并且完全正确地将公钥复制到了 git 服务器。仍然没有帮助。

When I look at the log of the build it says:

当我查看构建日志时,它说:

Started by user anonymous
Building in workspace /var/lib/jenkins/jobs/myrepo/workspace

this user anonymous is this another user created by jenkins, or is it still jenkins that runs the commands?

这个匿名用户是 jenkins 创建的另一个用户,还是运行命令的 jenkins?

回答by gareth_bowles

I'm assuming that you have root access if you were able to install the Jenkins RPM. Were you su-ing to the jenkins user while logged in as root ? If not, you should do so or use

如果您能够安装 Jenkins RPM,我假设您具有 root 访问权限。您是否在以 root 身份登录时向 jenkins 用户发出警告?如果没有,你应该这样做或使用

sudo su - jenkins

if your logged in user has sudo access.

如果您的登录用户具有 sudo 访问权限。

Then run

然后运行

ssh-keygen -t rsa

to generate an RSA keypair for the jenkins user, and you can upload the public key to your git server. The key will be generated as /var/lib/jenkins/.ssh.id_ra.pub if you take the defaults.

为 jenkins 用户生成 RSA 密钥对,您可以将公钥上传到您的 git 服务器。如果采用默认值,密钥将生成为 /var/lib/jenkins/.ssh.id_ra.pub。

回答by jordan

1 change user jenkins login setting

1 更改用户 jenkins 登录设置

vi /etc/passwd

vi /etc/passwd

update /bin/false to /bin/bash

将 /bin/false 更新为 /bin/bash

2 su - jenkins

2 苏詹金斯

jekins user home would be /var/lib/jenkins

jekins 用户主页将是 /var/lib/jenkins

3 ssh-keygen

3 ssh-keygen

cat .ssh/id_rsa.pub

猫.ssh/id_rsa.pub

copy this key to bitbucket

将此密钥复制到 bitbucket

4 ssh [email protected]

4 SSH [email protected]

this is going to set the bitbucket ssh key in .ssh/known_hosts

这将在 .ssh/known_hosts 中设置 bitbucket ssh 密钥

Now you should be able to access

现在您应该可以访问

回答by Jonathan Airey

I was having problems changing to the jenkins user. I needed to actually create the user properly in the system so I could log in to it. I ran this command to help me create the jenkins user in the system:

我在更改为 jenkins 用户时遇到问题。我需要在系统中正确创建用户,以便我可以登录它。我运行这个命令来帮助我在系统中创建 jenkins 用户:

su -s /bin/bash jenkins

回答by Sridhar

1) First make jenkins as real user by editing /etc/passwd file Change

1) 首先通过编辑 /etc/passwd 文件更改 jenkins 为真实用户

/bin/false to /bin/bash

/bin/false 到 /bin/bash

2) Login to jenkins user, Now Jenkins home directory will be /var/lib/jenkins

2) 登录 jenkins 用户,现在 Jenkins 主目录为 /var/lib/jenkins

su - jenkins

3)Generate ssh keys again ( keys will be created in /var/lib/jenkins/.ssh)

3)再次生成ssh密钥(密钥将在/var/lib/jenkins/.ssh中创建)

ssh-keygen -t rsa

4) copy id_rsa.pub key and paste in your git's authorized_keys file

4) 复制 id_rsa.pub 密钥并粘贴到您的 git 的 authorized_keys 文件中

5) Still if it won't work, probably you have to check permissions of your .ssh directory and contents

5)如果它仍然不起作用,可能你必须检查你的 .ssh 目录和内容的权限

chmod 700 ~/.ssh && chmod 600 ~/.ssh/*

回答by Daniel S.

I think the .ssh directory should live in /var/lib/jenkins.

我认为 .ssh 目录应该位于 /var/lib/jenkins 中。

Works for me

对我有用

回答by Mohamed Dernoun

jenkins is a service account, it doesn't have a shell by design. It is generally accepted that service accounts shouldn't be able to log in interactively.

jenkins 是一个服务帐户,它的设计没有外壳。人们普遍认为,服务帐户不应该能够以交互方式登录。

I didn't answer this one initially as it's a duplicate of a question that has been moved to server fault. I should have answered rather than linked to the answer in a comment.

我最初没有回答这个问题,因为它是一个已移至服务器故障的问题的副本。我应该回答而不是链接到评论中的答案。

if for some reason you want to login as jenkins, you can do so with: sudo su -s /bin/bash jenkins

如果由于某种原因你想以 jenkins 身份登录,你可以这样做:sudo su -s /bin/bash jenkins

回答by firmanelhakim

In order to login as "jenkins" user, maybe you could try this:

为了以“jenkins”用户身份登录,也许您可​​以尝试以下操作:

sudo -s -H -u jenkins

And try to invoke:

并尝试调用:

bash-4.1$ whoami
jenkins

Hope it helps.

希望能帮助到你。

回答by Hieu Pham

What I did: sudo -u jenkins ssh-keygen. It works so far

我做了什么:sudo -u jenkins ssh-keygen。到目前为止它有效