Auto‑Update GitHub Hosts on macOS with a Custom Bash Script
This guide shows how to fetch the latest GitHub IP addresses from https://github-hosts.tinsfox.com/hosts and automatically update the macOS /etc/hosts file using a Bash script generated with AI, including backup, execution, and shortcut setup for quick reuse.
Background
To avoid carrying a laptop between office and home, the author syncs a ThinkPad T460s and a MacBook Pro via GitHub. Frequent GitHub login failures led to discovering a site (https://github-hosts.tinsfox.com/) that provides the latest GitHub IP addresses.
Generating a Custom Script
Because the site’s built‑in tool didn’t meet personal needs, the author used the DeepSeek AI model with the prompt:
I want a macOS script that reads the content from "https://github-hosts.tinsfox.com/hosts" and updates the local /etc/hosts file.
DeepSeek returned a complete Bash script, which is reproduced below.
#!/bin/bash
# GitHub Hosts 更新脚本
# 下载地址: https://github-hosts.tinsfox.com/hosts
# 备份并更新 /etc/hosts 中的 GitHub 相关记录
# 配置参数
HOSTS_URL="https://github-hosts.tinsfox.com/hosts"
TEMP_FILE="/tmp/github_hosts_temp"
BACKUP_DIR="$HOME/hosts_backups"
# 确保备份目录存在
mkdir -p "$BACKUP_DIR"
# 创建时间戳备份
BACKUP_FILE="$BACKUP_DIR/hosts_$(date +%Y%m%d_%H%M%S).bak"
echo "备份当前 hosts 文件: $BACKUP_FILE"
sudo cp /etc/hosts "$BACKUP_FILE"
# 下载最新 hosts 内容
echo "下载 GitHub hosts 更新..."
curl -fsSL "$HOSTS_URL" -o "$TEMP_FILE"
if [ $? -ne 0 ]; then
echo "错误: 无法下载 hosts 文件"
exit 1
fi
# 处理 hosts 文件
echo "更新 hosts 文件..."
sudo bash -c "
# 删除旧的 GitHub hosts 区块
sed -i '' '/^# GitHub Hosts Start/,/^# GitHub Hosts End/d' /etc/hosts
echo '' >> /etc/hosts
echo '# GitHub Hosts Start - 更新于 $(date)' >> /etc/hosts
cat '$TEMP_FILE' >> /etc/hosts
echo '# GitHub Hosts End' >> /etc/hosts
"
# 清理临时文件
rm -f "$TEMP_FILE"
echo "更新完成! 已保留备份: $BACKUP_FILE"
echo "当前 /etc/hosts 最后几行:"
tail -n 8 /etc/hosts
# 刷新MacBook的DNS缓存
sudo killall -HUP mDNSResponderSaving and Preparing the Script
Save the script as update-github-hosts.sh. Grant execution permission: chmod +x update-github-hosts.sh Before running, ensure the /etc/hosts file is writable (use sudo as shown).
Running the Script
Execute with an absolute path to avoid "command not found" errors:
sudo /full/path/update-github-hosts.shMaking the Command Convenient
Copy the script to a directory in $PATH (e.g., /usr/local/bin) and rename it for short invocation:
sudo cp /full/path/update-github-hosts.sh /usr/local/bin/upd-github
sudo chmod +x /usr/local/bin/upd-githubNow the update can be run simply with:
sudo upd-githubConclusion
The entire process—from identifying the need for fresh GitHub IPs, prompting an AI model, obtaining a ready‑to‑run Bash script, to streamlining execution—took under ten minutes, demonstrating how AI can accelerate system‑administration tasks while still allowing manual verification and customization.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Breakthrough
Focused on fintech, sharing experiences in financial services, architecture technology, and R&D management.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
