What is SSH Credential in Jenkins: Explanation and Usage
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.
pipeline {
agent any
stages {
stage('Run Remote Command') {
steps {
sshagent(['my-ssh-credential-id']) {
sh 'ssh -o StrictHostKeyChecking=no user@remote-server ls -l'
}
}
}
}
}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
sshagentor other SSH plugins. - Keep credentials safe and never expose them in plain text scripts.