Setting up jenkins on centos: problems with ssh ke

2019-03-26 13:33发布

问题:

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:

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)

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

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

su - jenkins

but unfortunately it doesn't switch to jenkins user.

cat /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?

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.

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?

回答1:

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

sudo su - jenkins

if your logged in user has sudo access.

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.



回答2:

1 change user jenkins login setting

vi /etc/passwd

update /bin/false to /bin/bash

2 su - jenkins

jekins user home would be /var/lib/jenkins

3 ssh-keygen

cat .ssh/id_rsa.pub

copy this key to bitbucket

4 ssh git@bitbucket.org

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

Now you should be able to access



回答3:

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

Works for me



回答4:

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:

su -s /bin/bash jenkins


回答5:

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

/bin/false to /bin/bash

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

su - jenkins

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

ssh-keygen -t rsa

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

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

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


回答6:

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.

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



回答7:

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

sudo -s -H -u jenkins

And try to invoke:

bash-4.1$ whoami
jenkins

Hope it helps.



回答8:

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