语境: 2026年Linux服务器翻墙配置完整教程,涵盖Clash、V2Ray、sing-box等主流客户端安装与配置。
→
Linux服务器翻墙配置教程 | 2026年完整版
输出
前言
Linux服务器在开发、运维场景中广泛使用,但访问GitHub、Docker Hub、NPM等海外资源时常受网络限制。本文将详细介绍2026年Linux服务器翻墙的完整配置方案,帮助开发者在服务器上实现稳定的科学上网。
一、Linux翻墙方案对比
1.1 主流方案概览
| 方案 | 资源占用 | 配置难度 | 稳定性 | 推荐度 |
|---|---|---|---|---|
| Clash (Mihomo) | 中等 | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
| sing-box | 极低 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| V2Ray (Xray) | 中等 | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ |
| Shadowsocks-libev | 极低 | ⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ |
1.2 方案选择建议
新手推荐: Clash(Mihomo)- 配置灵活、社区活跃 极客推荐: sing-box - 性能优秀、新协议支持 传统方案: V2Ray/Xray - 成熟稳定 轻量需求: Shadowsocks - 资源占用最低
二、Clash(Mihomo)配置
2.1 安装Mihomo
方法一:官方二进制安装
# 下载最新版本
wget https://github.com/MetaCubeX/mihomo/releases/latest/download/mihomo-linux-amd64-v1.18.0.gz
# 解压
gzip -d mihomo-linux-amd64-v1.18.0.gz
# 重命名并移动
mv mihomo-linux-amd64-v1.18.0 mihomo
chmod +x mihomo
sudo mv mihomo /usr/local/bin/
# 验证安装
mihomo -v
方法二:包管理器安装
# Arch Linux
yay -S mihomo-bin
# macOS (Homebrew)
brew install mihomo
2.2 配置文件设置
创建配置目录:
sudo mkdir -p /etc/mihomo
sudo chown $USER:$USER /etc/mihomo
下载订阅配置:
# 替换为你的订阅链接
wget -O /etc/mihomo/config.yaml "https://your-airport.com/api/v1/client/subscribe?token=xxx&flag=clash"
手动创建基础配置:
# /etc/mihomo/config.yaml
mixed-port: 7890
allow-lan: false
mode: rule
log-level: info
external-controller: 127.0.0.1:9090
dns:
enable: true
enhanced-mode: fake-ip
fake-ip-range: 198.18.0.1/16
nameserver:
- https://1.1.1.1/dns-query
- https://8.8.8.8/dns-query
proxies:
- name: '节点1'
type: hysteria2
server: example.com
port: 443
password: your-password
sni: example.com
proxy-groups:
- name: '节点选择'
type: select
proxies:
- 节点1
- DIRECT
rules:
- GEOIP,CN,DIRECT
- MATCH,节点选择
2.3 Systemd服务配置
创建服务文件:
sudo tee /etc/systemd/system/mihomo.service > /dev/null <<EOF
[Unit]
Description=Mihomo Daemon, Another Clash Kernel
After=network-online.target nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
启用并启动服务:
# 重载systemd
sudo systemctl daemon-reload
# 设置开机自启
sudo systemctl enable mihomo
# 启动服务
sudo systemctl start mihomo
# 查看状态
sudo systemctl status mihomo
# 查看日志
sudo journalctl -u mihomo -f
2.4 环境变量配置
全局代理环境变量:
# 添加到 ~/.bashrc 或 ~/.zshrc
export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"
export all_proxy="socks5://127.0.0.1:7890"
export no_proxy="localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
生效配置:
source ~/.bashrc
临时取消代理:
unset http_proxy https_proxy all_proxy
2.5 验证代理
# 测试代理是否生效
curl -I https://www.google.com
# 测试IP地址
curl ip.sb
# 查看当前出口IP
curl https://api.ip.sb/ip
三、sing-box配置
3.1 安装sing-box
官方安装脚本:
# 下载并安装
bash <(curl -fsSL https://sing-box.app/install.sh)
# 或手动安装
wget https://github.com/SagerNet/sing-box/releases/latest/download/sing-box-1.8.0-linux-amd64.tar.gz
tar -xzf sing-box-1.8.0-linux-amd64.tar.gz
sudo mv sing-box-1.8.0-linux-amd64/sing-box /usr/local/bin/
sudo chmod +x /usr/local/bin/sing-box
3.2 配置文件
创建配置目录:
sudo mkdir -p /etc/sing-box
基础配置示例:
{
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "cloudflare",
"address": "https://1.1.1.1/dns-query",
"detour": "proxy"
},
{
"tag": "local",
"address": "223.5.5.5",
"detour": "direct"
}
],
"rules": [
{
"outbound": "any",
"server": "local"
}
],
"strategy": "prefer_ipv4"
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 2080
},
{
"type": "tun",
"tag": "tun-in",
"interface_name": "tun0",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"stack": "system"
}
],
"outbounds": [
{
"type": "hysteria2",
"tag": "proxy",
"server": "example.com",
"server_port": 443,
"password": "your-password",
"tls": {
"server_name": "example.com"
}
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
}
],
"route": {
"rules": [
{
"geoip": "cn",
"outbound": "direct"
},
{
"geosite": "cn",
"outbound": "direct"
}
],
"final": "proxy",
"auto_detect_interface": true
}
}
3.3 Systemd服务
sudo tee /etc/systemd/system/sing-box.service > /dev/null <<EOF
[Unit]
Description=sing-box service
After=network.target nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box/config.json
Restart=on-failure
RestartSec=10s
LimitNOFILE=infinity
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now sing-box
四、TUN模式配置
4.1 TUN模式优势
TUN模式特点:
- 全局代理所有流量
- 支持UDP转发
- 无需配置环境变量
- 适合服务器全局翻墙
4.2 权限配置
# 设置CAP_NET_ADMIN权限
sudo setcap cap_net_admin,cap_net_bind_service=+ep /usr/local/bin/mihomo
# 或设置user为root
# 在service文件中添加 User=root
4.3 TUN配置(Clash)
tun:
enable: true
stack: system
dns-hijack:
- any:53
auto-route: true
auto-detect-interface: true
device: Meta
mtu: 1500
strict-route: true
4.4 验证TUN
# 检查TUN网卡
ip addr show Meta
# 测试全局代理
curl -I https://www.google.com
# 检查路由表
ip route show
五、开发场景应用
5.1 Git代理配置
配置Git使用代理:
# HTTP/HTTPS代理
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
# SOCKS5代理
git config --global http.proxy socks5://127.0.0.1:7890
git config --global https.proxy socks5://127.0.0.1:7890
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
针对特定域名代理:
# 仅GitHub走代理
git config --global http.https://github.com.proxy http://127.0.0.1:7890
5.2 Docker代理配置
配置Docker守护进程代理:
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/proxy.conf > /dev/null <<EOF
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1,::1,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
Docker build代理:
# 构建时传入代理参数
docker build \
--build-arg HTTP_PROXY=http://host.docker.internal:7890 \
--build-arg HTTPS_PROXY=http://host.docker.internal:7890 \
-t myimage .
5.3 包管理器代理
apt (Debian/Ubuntu):
sudo tee /etc/apt/apt.conf.d/proxy.conf > /dev/null <<EOF
Acquire::http::Proxy "http://127.0.0.1:7890";
Acquire::https::Proxy "http://127.0.0.1:7890";
EOF
yum (CentOS/RHEL):
sudo tee /etc/yum.conf > /dev/null <<EOF
[main]
proxy=http://127.0.0.1:7890
EOF
npm:
npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890
# 或使用nvm时设置镜像
npm config set registry https://registry.npmmirror.com
pip:
pip config set global.proxy http://127.0.0.1:7890
# 或使用国内镜像
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
5.4 Go开发代理
# 配置Go代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
# 或使用代理访问
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
六、Web管理面板
6.1 安装Yacd面板
# 下载Yacd
wget https://github.com/haishanh/yacd/releases/latest/download/yacd.tar.xz
tar -xJf yacd.tar.xz -C /etc/mihomo/ui --strip-components=1
6.2 配置外部UI
# config.yaml
external-controller: 0.0.0.0:9090
secret: 'your-secret-password'
external-ui: /etc/mihomo/ui
6.3 访问面板
http://your-server-ip:9090/ui
安全建议:
- 修改默认secret
- 配置防火墙限制访问
- 或仅监听127.0.0.1配合SSH隧道
6.4 SSH隧道访问
# 本地执行,建立隧道
ssh -L 9090:127.0.0.1:9090 user@your-server
# 本地访问
http://127.0.0.1:9090/ui
七、防火墙配置
7.1 iptables配置
# 允许Clash端口
sudo iptables -A INPUT -p tcp --dport 7890 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 9090 -j ACCEPT
# 保存规则
sudo iptables-save | sudo tee /etc/iptables/rules.v4
7.2 ufw配置
# 允许端口
sudo ufw allow 7890/tcp
sudo ufw allow 9090/tcp
# 或仅允许内网访问
sudo ufw allow from 192.168.1.0/24 to any port 7890
sudo ufw allow from 192.168.1.0/24 to any port 9090
7.3 firewalld配置
sudo firewall-cmd --permanent --add-port=7890/tcp
sudo firewall-cmd --permanent --add-port=9090/tcp
sudo firewall-cmd --reload
八、监控与维护
8.1 日志查看
# 实时日志
sudo journalctl -u mihomo -f
# 查看最近100行
sudo journalctl -u mihomo -n 100
# 查看今天的日志
sudo journalctl -u mihomo --since today
8.2 自动更新订阅
创建更新脚本:
sudo tee /usr/local/bin/update-clash.sh > /dev/null <<'EOF'
#!/bin/bash
SUBSCRIBE_URL="https://your-airport.com/api/v1/client/subscribe?token=xxx"
CONFIG_PATH="/etc/mihomo/config.yaml"
wget -O "$CONFIG_PATH" "$SUBSCRIBE_URL"
systemctl restart mihomo
echo "[$(date)] Subscription updated" >> /var/log/clash-update.log
EOF
sudo chmod +x /usr/local/bin/update-clash.sh
设置定时任务:
# 每天凌晨3点更新
echo "0 3 * * * /usr/local/bin/update-clash.sh" | sudo tee -a /var/spool/cron/crontabs/root
8.3 健康检查脚本
sudo tee /usr/local/bin/check-clash.sh > /dev/null <<'EOF'
#!/bin/bash
if curl -s --connect-timeout 5 https://www.google.com > /dev/null; then
echo "OK: Proxy is working"
else
echo "ERROR: Proxy not working, restarting..."
systemctl restart mihomo
sleep 5
if curl -s --connect-timeout 5 https://www.google.com > /dev/null; then
echo "OK: Proxy recovered"
else
echo "CRITICAL: Proxy still not working"
fi
fi
EOF
sudo chmod +x /usr/local/bin/check-clash.sh
# 每小时检查一次
echo "0 * * * * /usr/local/bin/check-clash.sh" | sudo tee -a /var/spool/cron/crontabs/root
九、常见问题解决
9.1 服务无法启动
排查步骤:
# 1. 检查配置文件语法
mihomo -t -d /etc/mihomo
# 2. 查看详细错误
journalctl -u mihomo -n 50
# 3. 检查文件权限
ls -la /etc/mihomo/
# 4. 手动运行测试
mihomo -d /etc/mihomo
9.2 TUN模式不工作
解决方案:
# 1. 检查TUN权限
sudo setcap cap_net_admin=+ep /usr/local/bin/mihomo
# 2. 检查内核模块
sudo modprobe tun
# 3. 检查/dev/net/tun
ls -la /dev/net/tun
# 4. 检查路由表
ip route show
9.3 DNS解析失败
排查方法:
# 1. 测试DNS
nslookup google.com 127.0.0.1
# 2. 检查DNS配置
cat /etc/mihomo/config.yaml | grep -A 20 dns
# 3. 测试DoH
curl -s "https://1.1.1.1/dns-query?name=google.com&type=A" -H "Accept: application/dns-json"
9.4 速度很慢
优化建议:
- 选择距离近的节点(日本/香港)
- 使用Hysteria2/TUIC协议
- 开启TCP Fast Open
- 检查服务器带宽限制
- 避开晚高峰使用
十、总结
10.1 方案推荐
| 场景 | 推荐方案 | 理由 |
|---|---|---|
| 开发服务器 | Clash + 环境变量 | 灵活配置,支持分流 |
| 生产服务器 | sing-box + TUN | 性能优秀,全局代理 |
| 轻量VPS | Shadowsocks | 资源占用最低 |
| Docker主机 | Clash + Docker代理 | 支持容器代理 |
10.2 最佳实践
✅ 推荐做法:
- 使用Systemd管理服务
- 配置自动更新订阅
- 设置健康检查脚本
- 使用SSH隧道访问管理面板
- 配置防火墙保护端口
- 定期查看日志监控状态
❌ 避免事项:
- 不要在生产服务器使用TUN模式(可能影响业务)
- 不要暴露管理面板到公网
- 不要使用不稳定的免费节点
- 不要忘记配置开机自启
10.3 安全建议
- 修改默认密钥:设置复杂的secret
- 限制访问:管理面板仅本地访问
- 使用SSH隧道:远程管理通过隧道
- 定期更新:及时更新内核版本
- 监控日志:关注异常连接
关注 润土分享 获取更多科学上网技巧!
更多翻墙机场推荐,请查看 机场大全
1.1k 词 · 1.4k 令牌