目 录CONTENT

文章目录

【第十六期】-Ubuntu 命令行如何实现科学上网

项目背景

在平时部署应用过程中,经常要实现科学上网去拉取镜像或者一些模型,网上的代理地址又不稳定,那么如何在Ubuntu命令行模式下实现科学上网?依靠Mihomo就可以啦,前提是已经获取到小猫咪订阅地址

640-aGBP

正式部署

首先需要下载安装包,选择适合自己机器版本的安装包,因为我的NAS设备CPU比较老,这里用的是v1.18.5 版本

root@ubuntu00:~# cd /tmp
root@ubuntu00:/tmp# wget https://github.com/MetaCubeX/mihomo/releases/download/v1.18.5/mihomo-linux-amd64-compatible-v1.18.5.gz
--2026-07-24 15:20:06--  https://github.com/MetaCubeX/mihomo/releases/download/v1.18.5/mihomo-linux-amd64-compatible-v1.18.5.gz
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://release-assets.githubusercontent.com/github-production-release-asset/369178935/00c9e1be-9d3f-4d85-8e32-eefbcc19d247?sp=r&sv=2018-11-09&sr=b&spr=https&se=2026-07-24T08%3A19%3A01Z&rscd=attachment%3B+filename%3Dmihomo-linux-amd64-compatible-v1.18.5.gz&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2026-07-24T07%3A18%3A02Z&ske=2026-07-24T08%3A19%3A01Z&sks=b&skv=2018-11-09&sig=g4ZAMGrQDqD4oINLF0Sp9HYIylCvXeu5gWkaW%2FtneSU%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc4NDg3OTQwNiwibmJmIjoxNzg0ODc3NjA2LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.UXC_3osvztaHYGKLxjs3Xwdf5TboXw2uvbyOES_hkkM&response-content-disposition=attachment%3B%20filename%3Dmihomo-linux-amd64-compatible-v1.18.5.gz&response-content-type=application%2Foctet-stream [following]
--2026-07-24 15:20:07--  https://release-assets.githubusercontent.com/github-production-release-asset/369178935/00c9e1be-9d3f-4d85-8e32-eefbcc19d247?sp=r&sv=2018-11-09&sr=b&spr=https&se=2026-07-24T08%3A19%3A01Z&rscd=attachment%3B+filename%3Dmihomo-linux-amd64-compatible-v1.18.5.gz&rsct=application%2Foctet-stream&skoid=96c2d410-5711-43a1-aedd-ab1947aa7ab0&sktid=398a6654-997b-47e9-b12b-9515b896b4de&skt=2026-07-24T07%3A18%3A02Z&ske=2026-07-24T08%3A19%3A01Z&sks=b&skv=2018-11-09&sig=g4ZAMGrQDqD4oINLF0Sp9HYIylCvXeu5gWkaW%2FtneSU%3D&jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmVsZWFzZS1hc3NldHMuZ2l0aHVidXNlcmNvbnRlbnQuY29tIiwia2V5Ijoia2V5MSIsImV4cCI6MTc4NDg3OTQwNiwibmJmIjoxNzg0ODc3NjA2LCJwYXRoIjoicmVsZWFzZWFzc2V0cHJvZHVjdGlvbi5ibG9iLmNvcmUud2luZG93cy5uZXQifQ.UXC_3osvztaHYGKLxjs3Xwdf5TboXw2uvbyOES_hkkM&response-content-disposition=attachment%3B%20filename%3Dmihomo-linux-amd64-compatible-v1.18.5.gz&response-content-type=application%2Foctet-stream
Resolving release-assets.githubusercontent.com (release-assets.githubusercontent.com)... 185.199.110.133, 185.199.108.133, 185.199.109.133
Connecting to release-assets.githubusercontent.com (release-assets.githubusercontent.com)|185.199.110.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10711632 (10M) [application/octet-stream]
Saving to: ‘mihomo-linux-amd64-compatible-v1.18.5.gz.1’

amd64-compatible-v1.18.5.gz.1        5%[==> 

下载完成后进行解压安装

root@ubuntu00:/tmp# gzip -d mihomo-linux-amd64-compatible-v1.18.5.gz
root@ubuntu00:/tmp# chmod +x mihomo-linux-amd64-compatible-v1.18.5
root@ubuntu00:/tmp# mv -f mihomo-linux-amd64-compatible-v1.18.5 /usr/local/bin/mihomo
root@ubuntu00:/tmp# mihomo -v
Mihomo Meta v1.18.5 linux amd64 with go1.22.3 Sun May 19 08:47:37 UTC 2024
Use tags: with_gvisor
root@ubuntu00:/tmp#

创建系统目录

root@ubuntu00:~# mkdir /etc/mihomo
root@ubuntu00:~# cd /etc/mihomo/
root@ubuntu00:/etc/mihomo#

配置订阅(注意,这里的订阅链接一定要写对!通常以clash结尾)

curl -L "你的Clash订阅链接" -o /etc/mihomo/config.yaml

修改配置允许局域网及docker桥接

root@ubuntu00:/etc/mihomo# sed -i 's/allow-lan: false/allow-lan: true/g' /etc/mihomo/config.yaml
root@ubuntu00:/etc/mihomo# sed -i 's/bind-address: 127.0.0.1/bind-address: 0.0.0.0/g' /etc/mihomo/config.yaml
root@ubuntu00:/etc/mihomo# grep -E "allow-lan|bind-address|mixed-port" /etc/mihomo/config.yaml
mixed-port: 7890
allow-lan: true
bind-address: '*'
root@ubuntu00:/etc/mihomo#

创建服务文件

root@ubuntu00:/etc/mihomo# cat > /etc/systemd/system/mihomo.service << EOF
[Unit]
Description=Mihomo Proxy Service
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/mihomo -d /etc/mihomo
Restart=always
RestartSec=3
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF
root@ubuntu00:/etc/mihomo# systemctl daemon-reload
root@ubuntu00:/etc/mihomo#

测试运行(如果卡住了可以手动下载MMDB上传到/etc/mihomo下)

https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb

root@ubuntu00:/etc/mihomo# /usr/local/bin/mihomo -d /etc/mihomo
INFO[2026-07-24T15:34:27.931908607+08:00] Start initial configuration in progress
INFO[2026-07-24T15:34:27.934941816+08:00] Geodata Loader mode: memconservative
INFO[2026-07-24T15:34:27.93497611+08:00] Geosite Matcher implementation: succinct
INFO[2026-07-24T15:34:27.935134629+08:00] Can't find MMDB, start download

我们这里直接手动上传了

root@ubuntu00:/etc/mihomo# cp /tmp/Country.mmdb .
root@ubuntu00:/etc/mihomo# ll
total 9104
drwxr-xr-x   2 root root    4096 Jul 24 15:37 ./
drwxr-xr-x 113 root root    4096 Jul 24 15:28 ../
-rw-r--r--   1 root root 8844154 Jul 24 15:37 Country.mmdb
-rw-r--r--   1 root root  217394 Jul 24 15:32 config.yaml
-rw-r--r--   1 root root  245760 Jul 24 15:35 geoip.metadb
root@ubuntu00:/etc/mihomo#

再来测试一次

root@ubuntu00:/etc/mihomo# /usr/local/bin/mihomo -d /etc/mihomo
INFO[2026-07-24T15:38:06.418852816+08:00] Start initial configuration in progress
INFO[2026-07-24T15:38:06.421828819+08:00] Geodata Loader mode: memconservative
INFO[2026-07-24T15:38:06.422107362+08:00] Geosite Matcher implementation: succinct
INFO[2026-07-24T15:38:06.43060891+08:00] Initial configuration complete, total time: 11ms
INFO[2026-07-24T15:38:06.443052859+08:00] Sniffer is closed
INFO[2026-07-24T15:38:06.443534755+08:00] DNS server listening at: 127.0.0.1:1053
INFO[2026-07-24T15:38:06.444407796+08:00] Mixed(http+socks) proxy listening at: [::]:7890
INFO[2026-07-24T15:38:06.449683027+08:00] Start initial Compatible provider 故障转移
INFO[2026-07-24T15:38:06.449839812+08:00] Start initial Compatible provider ChatGPT
INFO[2026-07-24T15:38:06.450065897+08:00] Start initial Compatible provider Telegram
INFO[2026-07-24T15:38:06.450425743+08:00] Start initial Compatible provider 哔哩哔哩
INFO[2026-07-24T15:38:06.450699237+08:00] Start initial Compatible provider 海外使用
INFO[2026-07-24T15:38:06.451643552+08:00] Start initial Compatible provider 自动选择
INFO[2026-07-24T15:38:06.45279729+08:00] Start initial Compatible provider 磁力下载
INFO[2026-07-24T15:38:06.453695348+08:00] Start initial Compatible provider WhatsApp
INFO[2026-07-24T15:38:06.454175991+08:00] Start initial Compatible provider Copilot
INFO[2026-07-24T15:38:06.454729542+08:00] Start initial Compatible provider default
INFO[2026-07-24T15:38:06.454870116+08:00] Start initial Compatible provider 微软服务
INFO[2026-07-24T15:38:06.455083286+08:00] Start initial Compatible provider 境内使用
INFO[2026-07-24T15:38:06.455200487+08:00] Start initial Compatible provider 节点选择
INFO[2026-07-24T15:38:06.455300986+08:00] Start initial Compatible provider TikTok
INFO[2026-07-24T15:38:06.45544681+08:00] Start initial Compatible provider Twitter
INFO[2026-07-24T15:38:06.455615196+08:00] Start initial Compatible provider 抖音
INFO[2026-07-24T15:38:06.455891245+08:00] Start initial Compatible provider Netflix
INFO[2026-07-24T15:38:06.455921502+08:00] Start initial Compatible provider 苹果服务
INFO[2026-07-24T15:38:06.455939305+08:00] Start initial Compatible provider Steam
INFO[2026-07-24T15:38:06.455957239+08:00] Start initial Compatible provider 谷歌服务

到这里就没问题了,Ctrl+C结束掉,使用systemctl启动

^CWARN[2026-07-24T15:38:49.240151155+08:00] Mihomo shutting down
root@ubuntu00:/etc/mihomo#
root@ubuntu00:/etc/mihomo# systemctl start mihomo.service
root@ubuntu00:/etc/mihomo# systemctl status mihomo.service
● mihomo.service - Mihomo Proxy Service
     Loaded: loaded (/etc/systemd/system/mihomo.service; disabled; preset: enabled)
     Active: active (running) since Fri 2026-07-24 15:39:11 CST; 5s ago
 Invocation: fe0e8f0e579a4224990d39d893130630
   Main PID: 3629 (mihomo)
      Tasks: 8 (limit: 1752)
     Memory: 10.1M (peak: 10.4M)
        CPU: 164ms
     CGroup: /system.slice/mihomo.service
             └─3629 /usr/local/bin/mihomo -d /etc/mihomo

Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420119799+08:00" level=info msg="Start initial Compatible provider 谷歌>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420136631+08:00" level=info msg="Start initial Compatible provider Tele>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420525892+08:00" level=info msg="Start initial Compatible provider Copi>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420542253+08:00" level=info msg="Start initial Compatible provider 海外>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420562531+08:00" level=info msg="Start initial Compatible provider 境内>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420571829+08:00" level=info msg="Start initial Compatible provider 节点>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420582559+08:00" level=info msg="Start initial Compatible provider 自动>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420591385+08:00" level=info msg="Start initial Compatible provider Twit>
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.42067926+08:00" level=info msg="Start initial Compatible provider 抖音"
Jul 24 15:39:11 ubuntu00 mihomo[3629]: time="2026-07-24T15:39:11.420776483+08:00" level=info msg="Start initial Compatible provider defa>
lines 1-21/21 (END)

测试是否可以科学上网,显示返回 HTTP/2 200 代表代理链路完全正常

root@ubuntu00:/etc/mihomo# curl -x http://127.0.0.1:7890 -I https://storage.googleapis.com
HTTP/1.1 200 Connection established

HTTP/2 400
content-type: application/xml; charset=UTF-8
x-guploader-uploadid: AKhXX7INxrWUTTGyJoW8s6t35_l0yoW39bBl-QgW_Iz7ogZceGr0uJA3IZIpSI-tn86eX6vw
content-length: 181
date: Fri, 24 Jul 2026 07:40:48 GMT
expires: Fri, 24 Jul 2026 07:40:48 GMT
cache-control: private, max-age=0
server: UploadServer
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

root@ubuntu00:/etc/mihomo#

注意使用完成服务记得关闭,且不要设定开机自启动

另外,科学上网只用于个人学习切勿用作他用!!!!!!

640-iSmG

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区