Keywords: Jenkins | Git | SSH Keys | Continuous Integration | Permission Configuration
Abstract: This paper provides an in-depth analysis of common SSH key verification failures when Jenkins connects to Git repositories, focusing on connection failures caused by improper SSH key file ownership configurations. Through detailed technical explanations and code examples, it systematically elaborates on how to correctly configure SSH key permissions for Jenkins users and offers solutions for various environments. The article also supplements related technical points including Git installation verification, special configurations for Windows systems, and HTTPS connection certificate handling, providing comprehensive technical guidance for Jenkins-Git integration.
Problem Background and Error Analysis
In continuous integration environments, integrating Jenkins with Git repositories is a common configuration requirement. However, during actual deployment, developers frequently encounter connection failures, particularly the "Host key verification failed" error when using SSH protocol. This error typically indicates that the Jenkins process cannot properly access SSH key files, leading to authentication failures.
Core Issue: SSH Key Ownership Configuration
The root cause of the problem lies in improper configuration of SSH key file ownership. When SSH key files (such as id_rsa and id_rsa.pub) are created by the root user, the Jenkins user may be unable to read these critical files. In Linux systems, file permissions and ownership are essential components of the security mechanism, and improper configurations can directly lead to access denial.
The following code example demonstrates the correct ownership change operation:
sudo chown jenkins:jenkins ~/.ssh/id_rsa
sudo chown jenkins:jenkins ~/.ssh/id_rsa.pub
sudo chmod 600 ~/.ssh/id_rsa
sudo chmod 644 ~/.ssh/id_rsa.pub
This code not only changes file ownership but also sets appropriate file permissions. chmod 600 ensures that the private key file is readable and writable only by the owner, while chmod 644 allows the public key file to be read by other users.
SSH Key Generation and Host Verification
In some cases, it may be necessary to regenerate SSH key pairs for the Jenkins user. The following demonstrates the key generation process on different operating systems:
On Linux systems:
sudo -iu jenkins
ssh-keygen -t rsa -b 4096 -C "jenkins@example.com"
# Press Enter continuously to accept default settings
ssh -T git@github.com # Trigger host key verification
On Windows systems, where the Jenkins service typically runs under the system account, special attention must be paid to the SSH key storage location:
# Execute in Git Bash
ssh-keygen -t rsa -C "jenkins-windows"
# Copy the generated .ssh folder to the system directory
copy .ssh C:\Windows\System32\config\systemprofile\
Git Environment Verification and Dependency Checking
Before addressing SSH issues, it is essential to ensure that Git is correctly installed on the Jenkins server. The installation status can be verified using the following commands:
git --version
which git # Linux/Mac
where git # Windows
If Git is not installed, it should be installed according to the operating system. For example, on Ubuntu:
sudo apt-get update
sudo apt-get install git
Special Handling for HTTPS Connections
For HTTPS connection methods, in addition to basic network proxy configurations, SSL certificate verification issues must be considered. When encountering certificate verification failures, they can be resolved by configuring the Java trust store:
# Add to Jenkins startup parameters
-Djavax.net.ssl.trustStore=/path/to/truststore.jks
-Djavax.net.ssl.trustStorePassword=password
Simultaneously, a .netrc file needs to be created in the Jenkins user directory to store authentication information for Git repositories:
machine github.com
login username
password token_or_password
Comprehensive Troubleshooting Process
It is recommended to follow this systematic process for problem investigation:
- Verify Git basic environment: Confirm that Git is correctly installed and version compatible
- Check SSH key permissions: Ensure the Jenkins user has appropriate read/write permissions for SSH key files
- Verify network connectivity: Test network connectivity to the Git repository server
- Configure host verification: Accept remote host keys during initial connection
- Test manual connection: Manually execute Git commands using Jenkins user identity to verify configuration
Through the systematic analysis and solutions provided above, various authentication and connection issues when Jenkins connects to Git repositories can be effectively resolved, ensuring the stable operation of continuous integration workflows.