在Debian环境下设置GitLab的安全配置涉及多个方面,包括防火墙配置、SSH服务配置、更改默认端口、启用公钥认证等。以下是详细的步骤:
配置防火墙
ufw
(Uncomplicated Firewall)管理防火墙规则:sudo apt-get install ufw # 安装ufw
sudo ufw allow 80/tcp # 允许HTTP(80端口)入站连接
sudo ufw allow 443/tcp # 允许HTTPS(443端口)入站连接
sudo ufw enable # 启用ufw
iptables
:sudo apt-get install iptables # 安装iptables
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许HTTP(80端口)入站连接
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许HTTPS(443端口)入站连接
sudo iptables-save /etc/iptables/rules.v4 # 保存规则
sudo iptables-restore /etc/iptables/rules.v4 # 重启时加载规则
使用HTTPS
sudo apt-get install certbot python3-certbot-nginx # 安装Certbot和Nginx插件
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com # 获取并安装SSL证书
/etc/gitlab/gitlab.rb
,设置 external_url 'https://yourdomain.com'
,然后重新配置并重启GitLab:sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
设置访问控制
配置SSH认证
ssh-keygen -t rsa -C "your_email@example.com"
cat ~/.ssh/id_rsa.pub | ssh gitlab@your_server_ip "cat >> ~/.ssh/authorized_keys"
/etc/ssh/sshd_config
文件,设置 PubkeyAuthentication yes
和 PasswordAuthentication no
。sudo systemctl restart ssh
定期备份
更新GitLab
监控日志
Logwatch
或 Fail2ban
自动监控并报告系统活动。启用双因素认证
强化密码策略
限制文件上传
.gitignore
文件忽略敏感信息文件,检查提交内容,防止敏感信息上传。加密敏感文件
安全审查
实时监控和日志记录