Jenkins安全設置指南
Jenkins作為持續集成/交付(CI/CD)核心工具,其安全性直接關系到代碼、憑證及系統穩定。以下是分層、實戰化的安全設置方案,覆蓋基礎配置、權限管理、網絡安全等關鍵環節:
進入Jenkins管理界面→「Manage Jenkins」→「Configure Global Security」,勾選“Enable security”(核心開關)。此設置強制所有用戶通過身份驗證,禁止匿名訪問(除非特殊需求)。
選擇「Security Realm」(安全域),推薦以下方式:
權限是安全的核心,推薦基于角色的訪問控制(RBAC),避免權限過度分配:
限制Jenkins端口的訪問范圍,減少外部攻擊面:
sudo firewall-cmd --permanent --add-port=8080/tcp # 默認HTTP端口
sudo firewall-cmd --permanent --add-port=443/tcp # HTTPS端口(可選)
sudo firewall-cmd --reload
sudo ufw allow 8080/tcp
sudo ufw enable
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port port="8080" protocol="tcp" accept')。避免數據傳輸被竊聽,推薦使用Let’s Encrypt免費證書:
sudo apt install certbot python3-certbot-nginx(Debian/Ubuntu);sudo certbot --nginx -d jenkins.yourdomain.com;/etc/letsencrypt/live/jenkins.yourdomain.com/fullchain.pem和/etc/letsencrypt/live/jenkins.yourdomain.com/privkey.pem)。在「Configure Global Security」→「CSRF Protection」部分,勾選“Enable CSRF Protection”(默認開啟)。如需兼容代理服務器,勾選“Enable proxy compatibility”。
使用「Credentials Binding Plugin」或「Jenkins Credentials Plugin」加密存儲敏感信息(如Git密碼、API密鑰、SSH私鑰):
withCredentials塊引用憑據(如withCredentials([usernamePassword(credentialsId: 'git-creds', usernameVariable: 'GIT_USER', passwordVariable: 'GIT_PASS')]) { ... })。定期備份Jenkins主目錄(默認/var/lib/jenkins),可使用rsync或tar命令:
sudo tar -czvf jenkins_backup_$(date +%F).tar.gz /var/lib/jenkins
備份文件存儲在異地(如云存儲),確保災難發生時可快速恢復。
/etc/pam.d/common-password)設置密碼復雜度要求(如至少8位、包含大小寫字母、數字、特殊字符);/etc/pam.d/su,添加auth required pam_wheel.so use_uid,僅允許wheel組成員使用su切換到root。通過以上步驟,可構建多層次、全生命周期的Jenkins安全體系,有效防范未授權訪問、數據泄露及惡意攻擊。需根據團隊規模(如小型團隊可使用內置權限系統,大型企業推薦RBAC插件)和環境需求(如是否集成LDAP)調整配置。