skip to content
Server connection to GitHub

Connecting a Tencent Cloud Server to GitHub

1 min read

This page was translated by AI

1. Why

After moving my blog to Next.js, I put the code on GitHub. Although I had written my own DevOps setup, the server sometimes could not access GitHub and therefore could not pull the repository. I wrote an automation script that periodically updates the hosts file so that my server can access GitHub normally.

2. The script

Create a script named update_github_hosts.sh:

#!/bin/bash
# GitHub访问优化脚本
# 作者: 系统管理员
# 用途: 定期更新GitHub相关域名的hosts记录
HOSTS_FILE="/etc/hosts"
BACKUP_DIR="/etc/hosts_backups"
BACKUP_FILE="$BACKUP_DIR/hosts.backup.$(date +%Y%m%d_%H%M%S)"
# 创建备份目录
create_backup_dir() {
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
fi
}
# 备份hosts文件并管理备份数量
manage_backups() {
create_backup_dir
# 创建新备份
cp "$HOSTS_FILE" "$BACKUP_FILE"
echo "已备份hosts文件到: $BACKUP_FILE"
# 只保留最新的3个备份文件
cd "$BACKUP_DIR"
ls -t hosts.backup.* 2>/dev/null | tail -n +4 | xargs rm -f 2>/dev/null
echo "当前保留的备份文件:"
ls -la "$BACKUP_DIR"/hosts.backup.* 2>/dev/null || echo "无备份文件"
}
# 获取IP地址函数
get_ip() {
local domain=$1
local ip=""
# 尝试多个DNS服务器
for dns in "8.8.8.8" "1.1.1.1" "114.114.114.114"; do
ip=$(nslookup "$domain" "$dns" 2>/dev/null | grep -A 1 "Name:" | grep "Address:" | awk '{print $2}' | head -1)
if [ -n "$ip" ] && [[ "$ip" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "$ip"
return 0
fi
done
# 如果nslookup失败,尝试dig
ip=$(dig +short "$domain" @8.8.8.8 2>/dev/null | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | head -1)
if [ -n "$ip" ]; then
echo "$ip"
return 0
fi
return 1
}
# GitHub相关域名列表
GITHUB_DOMAINS=(
"github.com"
"api.github.com"
"www.github.com"
"raw.githubusercontent.com"
"gist.githubusercontent.com"
"cloud.githubusercontent.com"
"camo.githubusercontent.com"
"avatars0.githubusercontent.com"
"avatars1.githubusercontent.com"
"avatars2.githubusercontent.com"
"avatars3.githubusercontent.com"
"avatars.githubusercontent.com"
"github.githubassets.com"
"assets-cdn.github.com"
)
main() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - 开始更新GitHub hosts记录"
# 备份hosts文件并管理备份数量
manage_backups
# 移除旧的GitHub记录
sed -i '/# GitHub Start/,/# GitHub End/d' "$HOSTS_FILE"
# 添加新的GitHub记录
echo "" >> "$HOSTS_FILE"
echo "# GitHub Start - $(date)" >> "$HOSTS_FILE"
local updated_count=0
local failed_domains=()
for domain in "${GITHUB_DOMAINS[@]}"; do
echo "正在解析域名: $domain"
ip=$(get_ip "$domain")
if [ $? -eq 0 ] && [ -n "$ip" ]; then
echo "$ip $domain" >> "$HOSTS_FILE"
echo "成功更新: $ip $domain"
((updated_count++))
else
echo "警告: 无法解析域名 $domain"
failed_domains+=("$domain")
fi
# 避免请求过于频繁
sleep 1
done
echo "# GitHub End - $(date)" >> "$HOSTS_FILE"
# 刷新DNS缓存
if command -v systemctl &> /dev/null; then
systemctl restart systemd-resolved 2>/dev/null || true
fi
# 输出统计信息
echo "$(date '+%Y-%m-%d %H:%M:%S') - 更新完成! 成功更新 $updated_count 个域名"
if [ ${#failed_domains[@]} -gt 0 ]; then
echo "失败的域名: ${failed_domains[*]}"
fi
echo "GitHub hosts更新任务完成"
echo "----------------------------------------"
}
# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then
echo "请使用root权限运行此脚本"
exit 1
fi
# 执行主函数
main

3. Installation and configuration

1. Create the script

Terminal window
sudo vim /usr/local/bin/update_github_hosts.sh

2. Make it executable

Terminal window
sudo chmod +x /usr/local/bin/update_github_hosts.sh

3. Set up a scheduled task

Edit crontab:

Terminal window
sudo crontab -e

Add the following line to run it daily at 2:00 AM:

Terminal window
0 2 * * * /usr/local/bin/update_github_hosts.sh

Or run it every 12 hours:

Terminal window
0 2,14 * * * /usr/local/bin/update_github_hosts.sh

5. Test the script manually

Terminal window
sudo /usr/local/bin/update_github_hosts.sh

4. An additional optimization script

If you want a simpler version, here is a lightweight script:

#!/bin/bash
# 简化版GitHub hosts更新脚本
HOSTS_FILE="/etc/hosts"
# 移除旧记录
sed -i '/# GitHub/d' "$HOSTS_FILE"
# 获取并添加新记录
{
echo "# GitHub - $(date)"
echo "$(dig +short github.com @8.8.8.8) github.com"
echo "$(dig +short api.github.com @8.8.8.8) api.github.com"
echo "$(dig +short raw.githubusercontent.com @8.8.8.8) raw.githubusercontent.com"
} >> "$HOSTS_FILE"
echo "GitHub hosts已更新 - $(date)"

5. Usage notes

  1. Viewing logs: The script logs are stored in /var/log/github_hosts_update.log.
  2. Restoring backups: If something goes wrong, restore from /etc/hosts.backup.date.
  3. Manual execution: You can run the script manually at any time to update the entries.
  4. Checking the result: Run ping github.com to test the connection.