Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Automate your Git host key verification setup in Jenkins

September 28, 2023
Mikel Sanchez
Related topics:
Automation and managementCI/CDJavaKubernetesMicroservices
Related products:
Developer Toolset

    As you might have noticed, some time ago, Jenkins was updated to version 2.361. This new release includes new updates to the Git Client Plug-in. SSH host key verification is now enabled by default (see JENKINS-69149).

    Configure SSH host key verification

    Following a security fix from Jenkins Security Advisory 2022-07-27, the Git Client Plug-in now has SSH host key verification enabled by default using the Known Hosts file strategy. Any Git / checkout step that uses the SSH protocol now performs strict host key verification based on the ~/ssh/known_hosts file in its environment. Impacted builds fail to check out the code and show the following error:

    stderr: No host key is known for, and you have requested strict checking.

    The Git Client Plug-in README describes the kinds of configurations allowed:

    • Accept first connection: Remembers the first host key encountered for each Git server and requires the same host key to be used for later access. This is usually the most convenient setting for administrators while still providing ssh host key verification.
    • Known hosts file: Uses the existing known_hosts file on the controller and the agent. This assumes the administrator has already configured this file on the controller and all agents.
    • Manually provided keys: Provides a form field where the administrator inserts the host keys for the Git repository servers. This works well when a small set of repository servers meet the needs of most users.
    • No verification: Disables all verification of SSH host keys. Not recommended because it provides no protection from "man-in-the-middle" attacks.

    Configure the host key verification strategy by navigating to Manage Jenkins → Configure Global Security → Git Host Key Verification Configuration (Figure 1).

    Host Key Configuration
    Figure 1: Host Key Configuration

    But how can we automate this in our Jenkins Source-to-Image (S2I) strategy? The next section will describe how you can achieve it.

    Jenkins Source-to-Image

    Red Hat introduced Jenkins Source-to-Image (S2I) feature, which simplifies customization of the official Jenkins image through the S2I build process. You can use Jenkins S2I to copy your custom Jenkins job definitions and additional plug-ins or replace the provided config.xml file with your own custom configuration. The resulting image is stored in the Red Hat OpenShift registry and can be used to deploy pre-configured Jenkins instances.

    Groovy script

    We can create a new Groovy script that will be executed each time the image runs. The location of the file needs to be created under the path /configuration/init.groovy.d/.groovy

    #!/usr/bin/env groovy
    
    import jenkins.model.*
    
    import org.jenkinsci.plugins.gitclient.verifier.*
    
    import jenkins.*
    
    import hudson.model.*
    
    import hudson.security.*
    
    import java.util.logging.Level
    
    import java.util.logging.Logger
    
    final def LOG = Logger.getLogger("APP")
    
    def hostKey = System.getenv('HOST_KEY')
    
    if (!hostKey) {
    
        hostKey = "YOUR_DEFAULT_HOST_KEY"
    
    }
    
    LOG.log(Level.INFO,  'running host-key.groovy' )
    
    def instance = Jenkins.getInstance().getDescriptor("org.jenkinsci.plugins.gitclient.GitHostKeyVerificationConfiguration")
    
    //strategy = new NoHostKeyVerificationStrategy()
    
    strategy = new ManuallyProvidedKeyVerificationStrategy(hostKey)
    
    instance.setSshHostKeyVerificationStrategy(strategy)
    
    instance.save()

    Reviewing the above code, we can see how the script tries to get a value from an environment variable called HOST_KEY. If the variable doesn't exist, we use a default one. The rest of the code retrieves the GitHostKeyVerificationConfiguration object, sets a new manual provided strategy, and adds the key that we retrieved previously. The last sentence saves the configuration.

    Configuration as Code plug-in

    If you use the Configuration as Code plug-in to set up your Jenkins configuration, there is also a way to set up your host key by updating the above example as follows:

    security:
      gitHostKeyVerificationConfiguration:
        sshHostKeyVerificationStrategy:
          manuallyProvidedKeyVerificationStrategy:
            approvedHostKeys: {HOST_KEY}

    I hope that this helps you to automate your Jenkins installation.

    Last updated: February 5, 2024

    Related Posts

    • A developer's guide to CI/CD and GitOps with Jenkins Pipelines

    • Get started with Jenkins CI/CD in Red Hat OpenShift 4

    • How to use continuous integration with Jenkins on OpenShift

    • Generate and save an HTML report in Jenkins on OpenShift 4

    • Deploy Helm charts with Jenkins CI/CD in Red Hat OpenShift 4

    Recent Posts

    • Red Hat Enterprise Linux 10.2 and 9.8: Top features for developers

    • What GPU kernels mean for your distributed inference

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    What’s up next?

    GitOps has become a standard in deploying applications to Kubernetes, and many companies are adopting the methodology for their DevOps and cloud-native strategy. Download the GitOps Cookbook for useful recipes and examples for successful hands-on applications development and deployment with GitOps.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.