ping命令测试外网连通性(如ping www.baidu.com)。若服务器需固定IP(避免DHCP动态分配导致IP变动),需修改网络接口配置文件:
/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服务器
/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是否生效。
GitLab需开放**HTTP(80)、HTTPS(443)、SSH(22)**等端口,以下以firewalld(CentOS默认)和ufw(Ubuntu默认)为例:
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 # 重新加载规则
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)验证端口是否开放。
GitLab的主网络配置文件为/etc/gitlab/gitlab.rb,需修改以下关键参数:
your_domain_or_IP为实际值:external_url 'http://your_domain_or_IP' # 若用HTTPS,改为'https://your_domain_or_IP'
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验证配置是否正确。
若需通过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是否生效。
external_url(如http://192.168.1.100),若出现GitLab登录页面,则说明网络可达;ssh git@your_domain_or_IP # 默认端口22
# 若修改了SSH端口,需指定端口:ssh -p 2222 git@your_domain_or_IP
首次登录需设置管理员密码(默认用户名为root)。
external_url中的端口(如http://your_domain_or_IP:8080),但需同步修改防火墙规则;