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
jenkinsExecute 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.pubPublic 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_keysPaste the content from id_rsa.pub into the authorized_keys file. Ensure proper directory and file permissions:
chmod 700 ~/.ssh
chmod 644 ~/.ssh/authorized_keysJenkins System Configuration
Complete SSH configuration in the Jenkins management interface:
- Navigate to "Manage Jenkins" -> "Configure System" -> "Publish over SSH"
- Provide the private key path or paste private key content directly
- Enter passphrase (if set)
- 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.