0
0
JenkinsConceptBeginner · 3 min read

What is SSH Credential in Jenkins: Explanation and Usage

In Jenkins, an SSH credential is a secure way to store and use SSH keys or usernames with passwords to connect to remote servers. It allows Jenkins jobs to authenticate safely when accessing servers or repositories over SSH without exposing sensitive data.
⚙️

How It Works

Think of SSH credentials in Jenkins like a special keychain that holds your keys to different houses (servers). Instead of typing your password every time, Jenkins uses these stored keys to unlock doors automatically and securely.

When you create an SSH credential in Jenkins, you provide either a private SSH key or a username and password. Jenkins then uses this information behind the scenes to connect to remote machines or services via SSH. This way, your sensitive login details are kept safe and not exposed in your job scripts.

It’s like giving Jenkins a trusted key that it can use whenever it needs to enter a server, so your automation tasks can run smoothly without manual intervention.

💻

Example

This example shows how to use an SSH credential in a Jenkins Pipeline to connect to a remote server and run a command.

groovy
pipeline {
  agent any
  stages {
    stage('Run Remote Command') {
      steps {
        sshagent(['my-ssh-credential-id']) {
          sh 'ssh -o StrictHostKeyChecking=no user@remote-server ls -l'
        }
      }
    }
  }
}
Output
total 12 -rw-r--r-- 1 user user 123 Apr 27 10:00 file1.txt -rw-r--r-- 1 user user 456 Apr 27 10:00 file2.txt
🎯

When to Use

Use SSH credentials in Jenkins when your automation needs to securely connect to remote servers or Git repositories over SSH. This is common for tasks like deploying code, running scripts on servers, or pulling private repositories.

For example, if you want Jenkins to deploy your application to a Linux server, you store the server’s SSH key in Jenkins credentials. Then Jenkins can log in and run deployment commands without asking for a password each time.

This method improves security by avoiding hard-coded passwords and makes your automation more reliable and easier to manage.

Key Points

  • SSH credentials store private keys or username/password securely in Jenkins.
  • They enable password-less, secure connections to remote servers or Git repos.
  • Used in pipelines with sshagent or other SSH plugins.
  • Keep credentials safe and never expose them in plain text scripts.

Key Takeaways

SSH credentials in Jenkins securely store keys or passwords for remote server access.
They enable automated, password-less SSH connections in Jenkins jobs and pipelines.
Use SSH credentials to improve security and simplify deployment or remote commands.
Manage credentials carefully to avoid exposing sensitive information.
The sshagent step in pipelines helps use SSH credentials easily during job execution.