1. 系统基础安全加固
sudo apt update && sudo apt upgrade,确保Debian系统及所有软件包(包括GitLab)为最新版本,修补已知安全漏洞。systemctl disable <service_name>关闭未使用的服务(如FTP、Telnet),减少攻击面。2. 防火墙配置
ufw(Uncomplicated Firewall),运行sudo apt install ufw安装。sudo ufw allow 80/tcp、sudo ufw allow 443/tcp、sudo ufw allow 22/tcp。sudo ufw enable激活规则,并通过sudo ufw status验证配置。3. SSH服务安全优化
/etc/ssh/sshd_config,将Port 22修改为非标准端口(如Port 2222),降低端口扫描攻击风险。PermitRootLogin no,强制用户通过普通账户登录后再切换至root(su -)。PubkeyAuthentication yes并禁用密码登录(PasswordAuthentication no),使用SSH密钥对(ssh-keygen -t rsa -b 4096生成,ssh-copy-id user@gitlab-server添加公钥)提升身份认证安全性。sudo systemctl restart ssh使配置生效。4. GitLab特定安全设置
sudo apt install certbot python3-certbot-nginx),运行sudo certbot --nginx -d gitlab.yourdomain.com自动获取并配置证书。/etc/gitlab/gitlab.rb,设置external_url 'https://gitlab.yourdomain.com',并指定证书路径(nginx['ssl_certificate'] = "/etc/letsencrypt/live/gitlab.yourdomain.com/fullchain.pem"、nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/gitlab.yourdomain.com/privkey.pem")。sudo gitlab-ctl reconfigure重启GitLab使HTTPS生效。Developer而非Maintainer),避免过度授权;启用双因素认证(2FA)(用户Settings → Account → Two-factor authentication),提升账户安全性。sudo gitlab-rake gitlab:backup:create创建每日备份(默认存储在/var/opt/gitlab/backups),并设置cron任务自动备份(如0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create)。5. 监控与审计
/var/log/gitlab(如gitlab-rails/production.log记录应用层活动,gitlab-shell.log记录SSH操作),使用tail -f /var/log/gitlab/gitlab-rails/production.log实时查看日志。Fail2ban(sudo apt install fail2ban)自动封禁多次登录失败的IP地址(配置/etc/fail2ban/jail.local,添加[gitlab-ssh] section),防范暴力破解;或使用Logwatch生成每日安全报告(sudo apt install logwatch)。