温馨提示×

GitLab在Linux中的网络配置怎样设置

小樊
47
2025-10-12 04:45:20
栏目: 智能运维

GitLab在Linux中的网络配置步骤

1. 准备工作

  • 操作系统:推荐使用Ubuntu(如22.04 LTS)、CentOS(如7/8 Stream)等主流Linux发行版;
  • 资源要求:服务器需满足至少2GB RAM(推荐4GB+)、20GB可用存储空间;
  • 网络环境:确保服务器已接入网络,可通过ping命令测试外网连通性(如ping www.baidu.com)。

2. 配置静态IP地址(可选但推荐)

若服务器需固定IP(避免DHCP动态分配导致IP变动),需修改网络接口配置文件:

  • CentOS:编辑/etc/sysconfig/network-scripts/ifcfg-<网卡名>(如ifcfg-ens33),设置以下参数:
    BOOTPROTO=static       # 改为静态IP
    ONBOOT=yes             # 开机自启
    IPADDR=192.168.1.100   # 替换为目标IP
    NETMASK=255.255.255.0  # 子网掩码(根据网络环境调整)
    GATEWAY=192.168.1.1    # 网关地址
    DNS1=8.8.8.8           # 首选DNS服务器
    
  • Ubuntu:编辑/etc/netplan/01-netcfg.yaml(文件名可能不同),设置以下参数:
    network:
      version: 2
      ethernets:
        ens33:               # 替换为实际网卡名(通过`ip a`查看)
          dhcp4: no          # 关闭DHCP
          addresses: [192.168.1.100/24]  # IP地址及子网掩码
          gateway4: 192.168.1.1         # 网关
          nameservers:
            addresses: [8.8.8.8, 8.8.4.4] # DNS服务器
    

保存后重启网络服务:

# CentOS
sudo systemctl restart network
# Ubuntu
sudo netplan apply

通过ip a命令验证IP是否生效。

3. 安装并配置防火墙

GitLab需开放**HTTP(80)、HTTPS(443)、SSH(22)**等端口,以下以firewalld(CentOS默认)和ufw(Ubuntu默认)为例:

  • CentOS(firewalld)
    sudo firewall-cmd --permanent --add-service=http     # 开放HTTP
    sudo firewall-cmd --permanent --add-service=https    # 开放HTTPS
    sudo firewall-cmd --permanent --add-service=ssh      # 开放SSH
    sudo firewall-cmd --reload                           # 重新加载规则
    
  • Ubuntu(ufw)
    sudo ufw allow http                                  # 开放HTTP
    sudo ufw allow https                                 # 开放HTTPS
    sudo ufw allow ssh                                   # 开放SSH
    sudo ufw enable                                      # 启用ufw
    

通过sudo firewall-cmd --list-all(CentOS)或sudo ufw status(Ubuntu)验证端口是否开放。

4. 修改GitLab核心网络配置

GitLab的主网络配置文件为/etc/gitlab/gitlab.rb,需修改以下关键参数:

  • 设置外部访问URL:指定GitLab的访问地址(域名或IP),替换your_domain_or_IP为实际值:
    external_url 'http://your_domain_or_IP'  # 若用HTTPS,改为'https://your_domain_or_IP'
    
  • 配置SSH端口(可选):若需修改默认SSH端口(22),需同步修改以下两项:
    gitlab_rails['gitlab_ssh_host'] = 'your_domain_or_IP'  # SSH访问地址
    gitlab_rails['gitlab_shell_ssh_port'] = 2222           # 新SSH端口(如2222)
    

保存文件后,执行以下命令使配置生效:

sudo gitlab-ctl reconfigure  # 重新配置GitLab
sudo gitlab-ctl restart      # 重启GitLab服务

通过grep "external_url" /etc/gitlab/gitlab.rb验证配置是否正确。

5. 配置HTTPS(可选但推荐)

若需通过HTTPS加密访问,需获取SSL证书(如Let’s Encrypt免费证书),并修改/etc/gitlab/gitlab.rb

external_url 'https://your_domain_or_IP'  # 强制使用HTTPS
nginx['ssl_certificate'] = "/etc/letsencrypt/live/your_domain_or_IP/fullchain.pem"  # 证书路径
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/your_domain_or_IP/privkey.pem"  # 私钥路径

重新配置并重启GitLab:

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

通过浏览器访问https://your_domain_or_IP,验证HTTPS是否生效。

6. 验证网络配置

  • 步骤1:在浏览器中输入配置的external_url(如http://192.168.1.100),若出现GitLab登录页面,则说明网络可达;
  • 步骤2:通过SSH连接测试(默认端口22或自定义端口):
    ssh git@your_domain_or_IP  # 默认端口22
    # 若修改了SSH端口,需指定端口:ssh -p 2222 git@your_domain_or_IP
    

首次登录需设置管理员密码(默认用户名为root)。

注意事项

  • 若使用域名,需确保域名已解析到服务器IP(通过DNS配置);
  • 若80/443端口被占用,可修改external_url中的端口(如http://your_domain_or_IP:8080),但需同步修改防火墙规则;
  • 生产环境中建议启用HTTPS,并定期更新SSL证书。

0