Jenkins Generate Ssh Key Pair

10.12.2020
Jenkins Generate Ssh Key Pair Average ratng: 3,7/5 2587 reviews

Which is right as I did not put the Master SSH pub key on the Slave in the /.ssh/authorizedkeys. It looks like the Centos Yum install does not create ssh keys for the user running Jenkins, I did my Google fu and from what I read I need this to do what I want to do.

  • Configuring SSH authentication between GitHub and Jenkins. We’ll generate ssh key pair and give the new location to save keys. 19 thoughts on.
  • When you set up SSH, you create a key pair that contains a private key (saved to your local computer) and a public key (uploaded to Bitbucket). Bitbucket uses the key pair to authenticate anything the associated account can access. This two-way mechanism prevents man-in-the-middle attacks. This first key pair is your default SSH identity.
  • Jul 27, 2016  Connecting Bitbucket and Jenkins (through SSH) On July 27, 2016 April 3, 2017 By Janitha Tennakoon In Continuous Integration, Technical In the previous post I mentioned about what is Continuous Integration and how to configure one such tool, Jenkins.
  • Dec 18, 2019 In this tutorial, we will walk through how to generate SSH keys on Ubuntu 18.04 machines. We will also show you how to set up an SSH key-based authentication and connect to your remote Linux servers without entering a password.
  • Creating a new key pair for authentication. To create a new key pair, select the type of key to generate from the bottom of the screen (using SSH-2 RSA with 2048 bit key size is good for most people; another good well-known alternative is ECDSA). Then click Generate, and start moving the mouse within the Window. Putty uses mouse movements to.
  • Jun 22, 2012  SSH keys provide a more secure way of logging into a virtual private server with SSH than using a password alone. With SSH keys, users can log into a server without a password. This tutorial explains how to generate, use, and upload an SSH Key Pair.

SSH keys are a necessity for Python development when you are working withGit, connecting to remote servers and automating yourdeployments. Let's walk through how to generate SSHkey pairs, which contain both a public and a private key within a singlepair, on Ubuntu Linux. Comanche 4 cd key generator.

Generating the Public and Private Keys

Open up a new terminal window in Ubuntu like we see in the followingscreenshot.

The ssh-keygen command provides an interactive command line interface forgenerating both the public and private keys. Invoke ssh-keygen with thefollowing -t and -b arguments to ensure we get a 4096 bit RSA key.Optionally, you can also specify your email address with -C (otherwiseone will be generated off your current Linux account):

(Note: the -o option was introduced in 2014; if this command fails for you, simply remove the -o option)

The first prompt you will see asks where to save the key. However, there areactually two files that will be generated: the public key and the privatekey.

This prompt refers to the private key and whatever you enter will alsogenerate a second file for the public key that has the same name and .pubappended.

If you already have a key, you should specify a new filename. I use manySSH keys so I typically name them 'test-deploy', 'prod-deploy', 'ci-server'along with a unique project name. Naming is one of those hard computerscience problems, so take some time to come up with a system that works foryou and the development team you work with!

Next you will see a prompt for an optional passphrase:

Whether or not you want a passphrase depends on how you will use the key.The system will ask you for the passphrase whenever you use the SSH keyso it is more secure.However, if you are automating deployments with acontinuous integration server likeJenkins then you will not want a passphrase.

Be aware that it is impossible to recover a passphrase if it is lost. Keepthat passphrase safe and secure because otherwise a completely new key wouldhave to be generated.

Enter the passphrase (or just press enter to not have a passphrase) twice.You'll see some output like the following:

Your SSH key is now generated and ready to use!

What now?

Now that you have your public and private keys, I recommend settingup a Python development environment withone of the following tutorials so you can start coding:

Additional ssh-keygen command resources:

Questions? Contact me via Twitter@fullstackpythonor @mattmakai. I'm also on GitHub withthe username mattmakai.

See something wrong in this post? Forkthis page's source on GitHuband submit a pull request.

With many of the tools commonly used in a Continuous Delivery pipeline, Windows is not the original OS the tool was developed for. Although support and adoption are growing all the time, there can still be some pain points and gotchas in configuring some of them to work as you would expect on a Windows OS.

In this post, we’re going to combine two of the big hitters in this space, Jenkins and Git. Jenkins is the most widely adopted solution for automating build and CI/CD pipelines and Git is the dominant force in source control management. We’re going to get Jenkins talking to a remote Git repository, specifically a private GitHub repository, using SSH authentication.

Git was developed specifically for managing the development of the Linux kernel (by Linus Torvolds himself no less) and was brought to Windows as an afterthought. Git comes bundled with OpenSSH, which does not yet have a production-ready implementation on Windows, although Microsoft is working on one that is currently pre-release [Win32-OpenSSH]. For this reason, Git for Windows is bundled with MINGW, a minimal GNU development environment that runs on Windows.

With that background out of the way, let’s get started.

Requirements

ToolVersion used in this postLinkNotes
Jenkins2.60.2https://jenkins.io/
Git Plugin/Git Client Plugin (Jenkins)3.5.1/2.5.0https://wiki.jenkins.io/display/JENKINS/Git+Plugin
Git for Windows2.14.1https://git-scm.com/downloadsIncludes MINGW and OpenSSH
PsExec2.2https://docs.microsoft.com/en-us/sysinternals/downloads/psexec

A note on installing Git for Windows

The first “gotcha” is how you install Git on the Jenkins worker in the first place. The Git plugin for Jenkins is not yet compatible with the Git Credential Manager for Windows. Ensure that you uncheck the box for this option at installation time.

If you see Jenkins hanging and timing out after ten minutes when executing a git fetch, this is a sign that you have the Git Credential Manager installed. The job is hanging because the Git Credential Manager has intercepted the command and is ignoring any other preconfigured authentication mechanism.

The only way around this at the moment is to choose not to install it.

Telling Jenkins where to find Git

After installation, our first job is to configure the Tool Location for Git. First, let’s figure out what the location is. Open PowerShell and input:

This will output something similar to:

Copy the path to git.exe.

In Jenkins, click Manage Jenkins then Configure System. In Global properties check Tool Locations, select (Git) Default and enter the path to git.exe in the Home textbox.

Save and exit the page.

Generating SSH keys for the Jenkins service

On Windows, the Jenkins services run as the Local System user by default, not your own user identity. It’s important to understand that Jenkins will be executing the Git commands and authenticating in the context of that user identity. This is important because OpenSSH will look for the SSH keys in the home directory of the user and the Local System account does not use the same home directory that your own personal account has.

Finding the Local System home directory

You probably don’t know what your Local System account considers its home directory. To find out, you need to start a command prompt as the Local System user and resolve %UserProfile%, but that’s easier said than done.

One way to run a process as the Local System account is to use PsExec, a Sysinternals tool that is normally used to execute programs on remote hosts. Using the -s parameter starts the process as Local System, so execute the following from PowerShell:

This will open a command prompt as Local System. Then resolve %UserProfile%:

This will output a path similar to:

Jenkins generate ssh key pair windows 10

So for me, C:WINDOWSsystem32configsystemprofile is the home directory of my Local Service account.

Generating keys with OpenSSH

One approach to enabling your Local Service account to work over SSH with a remote repo is to copy the .ssh directory from your personal home directory (containing previously generated and configured keys) over to the Local Service home directory, so that it effectively uses the same identity as your personal account over SSH. But I prefer to keep Local Service as a separate identity with its own keys, so generating new SSH keys is what we will be doing next.

From the Local Service command prompt you started with PsExec earlier, execute:

Ssh

This will start Bash in a MINGW window, the minimal GNU environment for Windows that I mentioned earlier, as the Local System user.

Execute the following to generate a new public and private RSA key with OpenSSH:

The proposed location of the keys will default to the home directory of the Local Service account that you discovered earlier, keep this default. When OpenSSH asks for a passphrase, just press Enter (and again on confirmation) to opt for no passphrase.

It is, of course, better to use SSH keys that are encrypted with a passphrase, but I have found that the Git plugin for Jenkins does not work very well with SSH keys that require a passphrase, even though its Credentials UI does allow you to enter one.

That whole workflow should look similar to the below:

You should see that there is now a .ssh directory in the Local Service home directory containing a public and private key pair. id_rsa.pub is the public key and is_rsa is the private key. The local Jenkins server will use the private key and the remote Git server will use the public key.

Adding the public key in GitHub

This next part is quite simple, we’re going to register the public key with our remote Git server. I’m going to use my personal GitHub, but it should be simple to replace these steps with the equivalents for the likes of BitBucket, etc.

  1. In your web browser, log in to GitHub.
  2. From your GitHub profile, click Settings, then SSH and GPG keys, then New SSH key.
  3. Give the key a name. I like SYSTEM@COMPUTER (substitute the name of your machine) because the name will remind me that it’s associated with the Local System account of my machine.
  4. On your local machine, open id_rsa.pub in a text editor, e.g. Notepad++. The contents will begin with: sshrsa.
  5. Press CTRL+A to select the entire file contents, then CTRL+C to copy it to your clipboard.
  6. Paste (CTRL+V) into the Key area in the web UI.
  7. Click Add SSH key.

And we’re done. Let’s test that we can now authenticate successfully with the corresponding private key.

Testing the SSH keys

Back in the MINGW shell (still running as our Local Service account) enter the following to connect to GitHub with SSH:

You will probably find that github.com is not yet a known host and you will get a prompt similar to below.

Just enter yes to accept the connection. On successfully connecting you will see:

We can see that the keys are correctly associated with our GitHub account.

Testing with a Jenkins project

Now all that remains is to test everything with a Jenkins project. For this I have a private GitHub repository containing the following Jenkinsfile:

This pipeline script just outputs “Hello, World!” to the console output, so nothing too interesting. Now we just need a Jenkins job to execute it:

The “Pipeline script from SCM” option means that the job definition lives in the SCM system itself and must be fetched to run. Our job won’t work at all if Jenkins isn’t successful in authenticating with GitHub using our SSH keys.

Make sure that the Repository URL is in a form that will use SSH to authenticate. A HTTPS URI, e.g. https://github.com/user/repo.git, will be expecting a username and password. See Git – The Protocols for more information.

Because OpenSSH will default to looking in %UserProfile%.ssh for keys, we don’t actually need to tell Jenkins what the private key is. So I’ve associated no credentials with this test job. Of course, you could choose to if you had a need.

If we run this, we should see:

Jenkins Git Ssh Key

We can see “Hello, World!” so we successfully fetched the job definition from the private Git repository.