Linux環境下GitLab安全策略配置指南
通過防火墻限制外部對GitLab服務器的訪問,僅開放必要端口(HTTP 80、HTTPS 443),阻止非法IP或端口的探測。
sudo apt install ufw -y
sudo ufw allow 80/tcp # 允許HTTP
sudo ufw allow 443/tcp # 允許HTTPS
sudo ufw enable # 啟用防火墻
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
使用SSL證書加密GitLab與客戶端之間的數據傳輸,防止中間人攻擊。推薦使用Let’s Encrypt免費證書(自動續期):
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
/etc/gitlab/gitlab.rb,指定證書路徑并強制HTTPS:external_url 'https://yourdomain.com'
nginx['ssl_certificate'] = "/etc/letsencrypt/live/yourdomain.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/yourdomain.com/privkey.pem"
nginx['redirect_http_to_https'] = true # 自動跳轉HTTPS
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
替代密碼認證,提升用戶登錄安全性:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
~/.ssh/id_rsa.pub內容復制到GitLab用戶設置的「SSH Keys」中。ssh -T git@yourdomain.com
通過自動化備份防止數據丟失,建議每日備份并存儲到異地:
sudo gitlab-rake gitlab:backup:create
sudo crontab -e
添加以下內容(每日凌晨2點備份):0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
sudo gitlab-ctl cleanup-backup
定期升級GitLab到最新穩定版,修復已知安全漏洞:
sudo apt update
sudo apt upgrade gitlab-ce -y # Debian/Ubuntu
sudo yum update gitlab-ce -y # CentOS/RHEL
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
通過GitLab配置強制用戶使用復雜密碼,降低賬戶被破解風險:
編輯/etc/gitlab/gitlab.rb,添加以下規則:
gitlab_rails['password_authentication'] = true # 啟用密碼認證
gitlab_rails['password_complexity'] = {
'min_length' => 12, # 最小長度12位
'require_lowercase' => true, # 必須包含小寫字母
'require_uppercase' => true, # 必須包含大寫字母
'require_numbers' => true, # 必須包含數字
'require_special_characters' => true # 必須包含特殊字符
}
重載配置:
sudo gitlab-ctl reconfigure
為賬戶添加第二層驗證,即使密碼泄露也能阻止非法登錄:
防止用戶上傳敏感文件(如.env、config.json),避免敏感信息泄露:
編輯/etc/gitlab/gitlab.rb,添加以下規則:
gitlab_rails['gitlab_shell_upload_pack'] = true
gitlab_rails['gitlab_shell_receive_pack'] = true
gitlab_rails['gitlab_shell_ssh_port'] = 22
gitlab_rails['gitlab_shell_authorized_keys_file'] = '/var/opt/gitlab/.ssh/authorized_keys'
# 禁止上傳危險文件類型
gitlab_rails['upload_size_limit'] = 100.megabytes # 限制上傳大小
gitlab_rails['block_upload_extensions'] = ['exe', 'bat', 'sh', 'env', 'json', 'yml']
重載配置:
sudo gitlab-ctl reconfigure
與企業現有身份管理系統集成,統一用戶身份認證,減少密碼管理成本:
編輯/etc/gitlab/gitlab.rb,添加LDAP配置:
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = {
'main' => {
'label' => 'LDAP',
'host' => 'ldap.example.com', # LDAP服務器地址
'port' => 389, # LDAP端口(默認389)
'uid' => 'uid', # 用戶標識字段(如uid、sAMAccountName)
'encryption' => 'plain', # 加密方式(none/plain/ssl/tls)
'bind_dn' => 'cn=admin,dc=example,dc=com', # 綁定DN(管理員賬號)
'password' => 'your_ldap_password', # 綁定密碼
'base' => 'ou=users,dc=example,dc=com', # 用戶搜索基礎DN
'user_filter' => '(objectClass=person)' # 用戶過濾條件
}
}
重載配置:
sudo gitlab-ctl reconfigure
記錄用戶活動(如登錄、代碼提交、權限變更),便于追蹤安全事件:
/var/log/gitlab/gitlab-rails/audit.log。curl --header "PRIVATE-TOKEN: your_access_token" "https://yourdomain.com/api/v4/audit_events?created_after=2025-01-01"
通過角色權限和分支保護限制用戶對項目的訪問,遵循“最小權限原則”:
Private(僅成員可訪問),避免公開泄露代碼。Guest僅查看、Developer可推送代碼、Maintainer可管理項目)。main分支),限制只有Maintainer及以上角色可推送代碼,防止誤操作或惡意修改。