Comprehensive Guide to Setting Up SSH Keys for Jenkins Remote Deployment

Nov 23, 2025 · Programming · 10 views · 7.8

Keywords: Jenkins | SSH Keys | Remote Deployment

Abstract: This article provides a detailed technical guide on configuring SSH keys in Jenkins for secure remote deployment. It covers the complete workflow from SSH key pair generation on the Jenkins server, public key deployment to target servers, Jenkins system configuration for SSH publishing, to permission management and security best practices. With clear step-by-step instructions and code examples, readers will gain practical knowledge for implementing robust SSH-based deployment pipelines.

SSH Key Generation Process

On the Jenkins server, generate SSH key pairs as the Jenkins user. First verify the current user identity:

build1:~ jenkins$ whoami
jenkins

Execute the ssh-keygen command to generate keys:

build1:~ jenkins$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/jenkins/.ssh/id_rsa): 
Created directory '/var/lib/jenkins/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa.
Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub.

Verify the generated key files:

build1:~ jenkins$ ls -l .ssh
total 2
-rw-------  1 jenkins  jenkins  1679 Feb 28 11:55 id_rsa
-rw-r--r--  1 jenkins  jenkins   411 Feb 28 11:55 id_rsa.pub

Public Key Deployment to Target Server

Copy the public key content to the authorization file on the target server:

target:~ bob$ cd .ssh
target:~ bob$ vi authorized_keys

Paste the content from id_rsa.pub into the authorized_keys file. Ensure proper directory and file permissions:

chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keys

Jenkins System Configuration

Complete SSH configuration in the Jenkins management interface:

  1. Navigate to "Manage Jenkins" -> "Configure System" -> "Publish over SSH"
  2. Provide the private key path or paste private key content directly
  3. Enter passphrase (if set)
  4. Configure server connection information and user credentials

Permissions and Security Considerations

Ensure the Jenkins user has exclusive access to private key files, and authorization file permissions on target servers are correctly set. Using strong passphrases is recommended for enhanced security, along with regular key pair rotation.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.