Host key verification failed. Lost connection in Jenkins - jenkins

I have two servers (AWS-Ubuntu), one is for Devops in which I have installed Jenkins. And in another server I have a dev server. I have built code in the Jenkins server and need to transfer my built files to the dev server.
Added (jenkins)id_rsa.pub key to dev server, restarted SSH service:
cd ${WORKSPACE}/Angular
npm install
cd ${WORKSPACE}/node
npm install
cd ${WORKSPACE}/Angular
npm run build
scp -r /var/lib/jenkins/workspace/project123/Angular/dist/* ubuntu#172.31.14.41:/var/
I expect to transfer files from Jenkins instance to dev server but I am getting this error:
scp -r /var/lib/jenkins/workspace/project123/Angular/dist/openwifi ubuntu#172.31.45.66:/var/www/html/jenkinsdev/Angular/
Host key verification failed.
lost connection
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Host key verification failed error occurs when you try to ssh/scp using the wrong username.
username should be same which is used for establishing ssh.
If above does not help then check the ssh keys are not corrupted or changed for that user.

Related

Jenkins Host key verification failed to deploy

i have project jenkins to deploy to my remote server, my local machine and my remote server was connected with ssh but when i try to build job on jenkins, i am getting error like this
image
Login on your jenkins and then try to connect manually on the command line with ssh from jenkins to the remote server. Then approve the host key of the remote system. Run again your job. Then I think everything should be fine.

Why am I unable to configure ssh keys to read a Gerrit repo from Jenkins?

I am in the process of trying to migrate Gerrit and Jenkins to a new datacenter. I have successfully installed the applications on their new hardware, but am having difficulty getting Jenkins to read from a Gerrit repository on the same server.
As the Jenkins user already existed on the server, I updated its ssh keys using one I generated from my account on my laptop (pablo#mena-xps), using the following command:
cat ~/.ssh/id_rsa.pub | ssh -p 29418 pmena#gerrit_host gerrit set-account --add-ssh-key - jenkins
I confirmed that I am able to use this key to interrogate gerrit from my laptop:
pablo#mena-xps=> ssh -p 29418 pmena#gerrit_host gerrit version
gerrit version 2.10
On the Jenkins server, I added the Private key under "Credentials" and then Configured the specific job to use that key to access a specific gerrit repo. That returns the following error in the GUI:
Failed to connect to repository : Command "git -c core.askpass=true ls-remote -h ssh://gerrit_host:29418/my_repo HEAD" returned status code 128:
stdout:
stderr: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
How do I go about troubleshooting this issue? Thank you in advance.
To make a long story short, this issue is resolved. The Jenkins account on Gerrit was removed and recreated, after which I was able to successfully push the ssh key pair.

Jenkins add new node

I am trying to set up Jenkins to make a production deployment. The aim is to allow Jenkins execute git clone, build and deployment etc. on production server.
Jenkins is running on Configuration Management Server (CentOs 7) and Production Server is another Server (Centos 7).
I started by creating a new node in Jenkins with configuration below:
Then I added Production Server IP address to Jenkins known host like below
-bash-4.2$ cat /var/lib/jenkins/.ssh/known_hosts
192.168.1.xx ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYblabla=
And I confirmed that on Configuration Management server under Jenkins account I could ssh to Production Server.
But on Jenkins the node status is always like this:
This agent is offline because Jenkins failed to launch the agent process on it.
Node log was like this:
SSHLauncher{host='192.168.1.xx', port=22, credentialsId='service account at w', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=60, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.NonVerifyingKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true}
[02/12/20 20:57:51] [SSH] Opening SSH connection to 192.168.1.xx:22.
[02/12/20 20:57:51] [SSH] WARNING: SSH Host Keys are not being verified. Man-in-the-middle attacks may be possible against this connection.
ERROR: Server rejected the 1 private key(s) for service_account (credentialId:service_account at webi/method:publickey)
[02/12/20 20:57:51] [SSH] Authentication failed.
Authentication failed.
[02/12/20 20:57:51] Launch failed - cleaning up connection
[02/12/20 20:57:51] [SSH] Connection closed.
Could anybody help with following questions:
Do I need to install Jenkins on Production Server as well?
How to configure Jenkins to run pipeline on
new node?
Okay I finally figured out:
There is no need to install Jenkins on Production Server;
The reason for SSH connection failure was wrong credentials for Jenkins set up. Essentially Jenkins will execute an SSH connection thus username/password should be used for this. so what I did was to create a new Credential and Jenkins will copy over a JAR file to /var/jenkins directory.
You would also need to change ownership of /var/jenkins directory!

Can't access jenkins user on server

Can't access the jenkins user on the server. Server currently running ubuntu 14.04, the user is available for a short period after a restart but is not accessible again after a few minutes.
All builds fail with message Build step 'Execute shell' marked build as failure
Restarting the service also fails as it cannot use the jenkins user that is specified on the server.
The jenkins user is setup to use bash and is available in /etc/passwd

Jenkins won't deploy - SSH server 'Host key Verification failed'

I'm deploying an app from git to a target server with Jenkins. I've set in my project a new Jenkinsfile which its stage('deploy') is pointing at the right machine. The conection would happen through SSH.
I've been reading for the deployment to succesfully run, both machines (git & target server) must know each others Host keys, which are stored in known_hosts at ~/.ssh/ . Therefore I connected through ssh from the git machine to the server (prompted if I wanted to continue establishing the connection, 'yes'):
checked the known_hosts file in git server to find the target server entry.
checked the known_hosts file at target server to find an entry, which looks like |1|KCIHm6...lo= ecdsa-sha2-nistp256 AAAA...(bla bla).
I think it's all set for Jenkins to be able to deploy on server, but everytime I build the project from Jenkins, the moment the first ssh deploy command is ran, the deployment fails with a 'Host key Verification failed' message. I can navigate through the target server no matter if I connect through my local machine or the git server, I've tried redoing the process but I can't really replicate the first connection.
Any suggestion is appreciated. Thanks in advance.
Create the ssh keys with jenkins user and restart jenkins.
su jenkins
ssh-keygen
ssh-copy-id destUser#destServer #Type the destUSer password
More info here

Resources