在Debian系统上设置GitLab的安全配置是一个涉及多个方面的过程,包括系统更新、安装必要的软件包、配置网络设置、设置访问控制等。以下是一些关键步骤:
确保系统和所有软件包都是最新版本,以修补已知的安全漏洞。
sudo apt update && sudo apt upgrade
安装GitLab所需的基本依赖软件包,如 curl
、openssh-server
、ca-certificates
和 postfix
(用于邮件服务)。
sudo apt install curl openssh-server ca-certificates tzdata perl
使用 iptables
或 ufw
(Uncomplicated Firewall)来限制对GitLab端口的访问。
使用iptables:
sudo apt-get install iptables
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许HTTP流量
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT # 允许HTTPS流量
sudo iptables -A INPUT -j DROP # 拒绝所有其他入站连接
sudo service iptables save
sudo service iptables start
使用ufw:
sudo apt-get install ufw
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
禁止root用户通过SSH登录,使用普通用户登录后再切换到root。启用公钥认证,禁用密码登录。
编辑 /etc/ssh/sshd_config
文件:
sudo nano /etc/ssh/sshd_config
将以下行修改为:
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
保存并退出编辑器,然后重启SSH服务:
sudo systemctl restart sshd
修改GitLab的外部URL以使用HTTPS:
编辑 /etc/gitlab/gitlab.rb
文件,找到并修改以下配置项:
external_url 'https://yourdomain.com'
保存并退出编辑器。
为GitLab设置SSL证书:
使用Let’s Encrypt免费证书。安装Certbot及其Nginx插件:
sudo apt update
sudo apt install certbot python3-certbot-nginx
获取并安装SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
这个命令会自动配置Nginx并安装SSL证书。你需要将 yourdomain.com
和 www.yourdomain.com
替换为你的实际域名。
重新配置并重启GitLab:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
创建普通用户并限制其对GitLab资源的访问权限。遵循最小权限原则,删除不必要的默认用户和用户组。
sudo useradd -m git
sudo usermod -aG sudo git yourusername
定期检查GitLab的日志文件,以便及时发现和响应任何异常活动。
sudo tail -f /var/log/gitlab/gitlab-rails/production.log