Connect Jenkins for SSH

Please post a screenshot of your job config containing the part where the ssh-agent is configured

This?

The only explanation I have is that something is wrong with the credential stored in Jenkins. You should double check that you really added the content of the private key there (not the path to the key file)

Your shell script has a bug
#sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$USERNAME@$REMOTE_SERVER" << EOF

should be changed to
ssh -o StrictHostKeyChecking=no "$USERNAME@$REMOTE_SERVER" << EOF
Due to the <<EOF at the end the following commands need to be executed on the remote machine

1 Like

Markus,
Why do I need that part of the script now if the credentials are “taken” from SSH-Agent?
I have done it as you told me and I get the error…

How to create an agent on the remote machine?
This is the first news I have of this…

you still need to run the ssh command to just without the askpass in front.

Have you properly set the password for the keystore in the credential in Jenkins?

sh-keygen -t ecdsa -m PEM -f key_name"

I’m not sure if that private key type is the best choice. I would use an ed25519 type
sh-keygen -t ed25519 -m PEM -f key_name"

Can you try from command line the following steps assuming you in <key_file> your private key.

ssh-agent > ssh-agent-conf
source ssh-agent-conf
ssh-add <key-file>  # this should ask for the key password if the key has a password

ssh <user>@<remote> ls -la
1 Like

You create the agent in Jenkins by going to /computer
Here you create a new agent and configure it to connect via ssh (requires SSH Build Agents plugin).
Here you can just select the credential, configure the root and number of executors and then you can execute jobs on it.

Thank you.
We understand different things when we talk about “agent”…
For example, I have SIEM servers which installs an agent on the remote machines that I want to monitor… or Zabbix, which installs an agent on the remote machines that I want to monitor, that is what I understand by agent.
He thought Jenkins had the same option, but I understand he doesn’t.
When Jenkins talks about “agents” I mean agent machines that help the node, for example, with the workload of compiling code, that doesn’t interest me.

I haven’t added anything to the keystore… could the error be there? The only thing I have done is generate a key pair with:
ssh-keygen -t ecdsa -m PEM -f key_name
And copy the public key to the remote machine, and the private key to the SSH-Agent plugin, to my Jenkins server, along with the credentials.
Is not correct?

Thanks for everything Markus, I’ll owe you dinner after this xdddddd

A node can also be used to deploy things, not only for building.

1 Like

When you created the private key did you specify a password for it? And have you maintained the password in the credential in Jenkins?

1 Like

Ok ok, but for that I already do it from the Jenkins server itself, I don’t need nodes.

If right.
When I created the keys it asked me for a password… in the SSH-Agent plugin, what I added, as far as I understand, is the username and password of the remote machine, not the password of the key pair…

Works perfectly, connects perfectly

Markus, I think this is where the problem was…
I entered the password of the remote server user, and not the password of the key pair!!!
What a noob I am for God’s sake!!!
Now the error is different, but it seems that the authentication is correct!!!

Hi Mrkus.
I’ve been reading about it and I was wrong or I didn’t know. I had misunderstood the concept of “agent” in Jenkis, as you say, an agent can also be any machine in production on which I want to deploy my applications.
That would give me greater control I understand over the machines and the delivery of the code.

I have an added question, my node (Jenkins server) runs with Java 11, and the pages in production where I have to deploy the applications (future agents) with Java 1.8, due to the need for the application itself.
The connection throws an error, since the compilation of the remote.jar is made with a version higher than the agent machine… I have tried to solve this, “manually” installing the same version with which the node runs, in the agent, and deploying the remote.jar there and it doesn’t work either…

In the advanced section of the agent configuration you can specify the JavaPath. By default it is empty which means it will use the java from the path. Just set this to the full path to the java that should be used.
Note that on Linux the current directory is not in the path usually.
From the log it seems you installed java11 to /opt/jenkins-agent/jdk-11.0.0.1
You should not use this directory as the Remote root directory in Jenkins.
If 11.0.0.1 is really your java version that is very outdated.

1 Like

So I can tell Jenkis (node) with which version of Java to compile the remote.jar for the agent (remote machine in production)?

The scheme is the following:

Jenkins Server (Node) Java 11.0.22
I can’t run it with version 1.8 because it doesn’t start
Agents (servers in production where applications run) Java 1.8… cannot run with other versions of Java.

What is the solution?
-Is it possible to compile the remote.jar on the Jenkins server with version 1.8?
-Install version 11.0.22 in the agents and indicate the path of the remote Java installation in the agent?

Thank you.