How to use the SSH Agent Plugin


I would like to use ssh based operations (for instance call an scp command) from within a build.



[The SSH Agent Plugin] can help you by using an ssh-agent to provide credentials to the build.

NOTE: On recent versions of the plugin (greater than 1.14), this should work out of the box on Linux.
It will require you to manually install ssh-agent on Windows.
This article does not cover the installation of ssh-agent, as the steps really depend on your setup and OS version.
Various resources are available on the Internet.

The following steps should get you going:

Setup the credentials

Create a new credential of type SSH Username with private key:

NOTE: You can select the method of your choice to enter the Private Key.
In case you have troubles creating credentials, please refer to Injecting Secrets into Jenkins Build Jobs.

Setup the Job (Pipeline)

You can simply use the sshagent step that will inject the credentials, as the following example:

sshagent(['my-ssh-key']) {
    sh 'scp /Users/exampleUser/home/aws/ ubuntu@ip-172-31-69-105.ec2.internal:/home/ubuntu'

Setup the Job (Non Pipeline Jobs)

  1. In your freestyle job, tick the SSH Agent in the Build Environment section and select the credential you just created.

  2. In your Build section, you can now use scp, the SSH Agent step will properly inject the credential and you can simply run the scp command.

Going further

You might want to review the SSH Credentials Management with Jenkins article for more details on the management of SSH credentials.


Have more questions?


Please sign in to leave a comment.