bsky
bsky

Reputation: 20242

Connecting to Jenkins with ssh key

I want to use the Jenkins CLI, and for that I think that I need to generate an SSH key pair on my machine and put the public key of that pair in the Jenkins server configuration.

I generated an SSH key on my machine with ssh-keygen -t rsa -b 4096 -C "[email protected]" and I'm trying to give the public key to the Jenkins server.

However, if I go to Jenkins -> Credentials -> System -> Global Credentials, the only SSH-related option is SSH Username with private key.

Shouldn't it ask for an SSH public key instead of a private key?

Am I making any mistakes in my assumptions?

Upvotes: 2

Views: 14091

Answers (2)

George Smith
George Smith

Reputation: 602

Update I just found out why the SSH Public Keys section was missing - the SSH Server plugin did not come with this version of Jenkins. After installing it, you can find the section on the User's Credentials screen as described in my original answer below.

===

I figured this out. The section Username > Security > SSH Public Keys no longer exists.

Instead, navigating to Username > Credentials (in left sidebar after navigating to user settings or clicking on the username in the right-hand corner of the screen as show in the screenshot) > click the (global) link under Domain for the current user > Add Credential (blue button at top right), then configure:

  • ID: something meaningful, for example jenkinscli, if you are going to use it for CLI ops
  • Optional description
  • Kind: SSH Username with private key
  • Username: the username of your user (in my case, the username for which I also have a password to log into Jenkins)
  • Private Key: select the "Enter directly" radio button, and very, very important, provide the PUBLIC KEY(AND NOT THE PRIVATE ONE) of your SSH key pair. The one you usually reveal with "cat id_rsa.pub"

After that, just navigate to http://jenkinsurl/cli and download the CLI jar. You should be able to authenticate without a problem when you execute:

java -jar jenkins-cli.jar -s http://34.214.36.85:8080/ help

It should return a list of CLI commands you can use to control Jenkins via the CLI.

enter image description here

enter image description here

Upvotes: 0

Christopher Orr
Christopher Orr

Reputation: 111625

You need to add the public key to your user account in Jenkins.

Click your username in the top-right of any page in Jenkins, click Configure in the sidebar, and there you'll find a SSH Public Key textfield to paste into.

Upvotes: 7

Related Questions