2026年3月8日

内网穿透方案大全 - 远程访问内网服务

内网穿透可以让你在外网访问家里的 NAS、服务器等设备。本文介绍几种常用的内网穿透方案。

什么是内网穿透

内网穿透(NAT 穿透)是一种技术,允许外部网络访问位于 NAT/防火墙后面的内部网络服务。

应用场景

  • 远程访问家庭 NAS
  • 外网访问内网网站
  • 远程桌面连接
  • 开发调试 Webhook
  • IoT 设备远程管理

方案对比

方案 优点 缺点 适用场景
frp 开源免费、功能强大 需要公网服务器 长期稳定使用
ngrok 简单易用 免费版限制多 临时测试
Cloudflare Tunnel 免费、无需服务器 需要域名 网站服务
Tailscale P2P 直连、速度快 需要客户端 设备互联
ZeroTier 功能丰富 配置复杂 企业组网

frp 内网穿透

简介

frp 是一个高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等协议。

安装 frp

下载地址

https://github.com/fatedier/frp/releases

服务端安装

# 下载
wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz

# 解压
tar -xzf frp_0.52.3_linux_amd64.tar.gz
cd frp_0.52.3_linux_amd64

服务端配置

# frps.toml
bindPort = 7000

# Dashboard 配置
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "admin123"

# 认证
auth.method = "token"
auth.token = "your_token_here"

启动服务端

# 直接启动
./frps -c frps.toml

# 后台运行
nohup ./frps -c frps.toml &

# 使用 systemd 管理
sudo vim /etc/systemd/system/frps.service
[Unit]
Description=frp server
After=network.target

[Service]
Type=simple
ExecStart=/path/to/frps -c /path/to/frps.toml
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
sudo systemctl enable frps
sudo systemctl start frps

客户端配置

# frpc.toml
serverAddr = "your_server_ip"
serverPort = 7000

auth.method = "token"
auth.token = "your_token_here"

# SSH 穿透
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000

# HTTP 穿透
[[proxies]]
name = "web"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["web.yourdomain.com"]

# HTTPS 穿透
[[proxies]]
name = "https"
type = "https"
localIP = "127.0.0.1"
localPort = 443
customDomains = ["secure.yourdomain.com"]

启动客户端

# 直接启动
./frpc -c frpc.toml

# 后台运行
nohup ./frpc -c frpc.toml &

# Windows 服务
# 使用 winsw 或 nssm 注册为服务

常用配置示例

穿透 Web 服务

[[proxies]]
name = "nas-web"
type = "http"
localIP = "192.168.1.100"
localPort = 5000
customDomains = ["nas.example.com"]

穿透远程桌面

[[proxies]]
name = "rdp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3389
remotePort = 3389

穿透多个端口

[[proxies]]
name = "range-ports"
type = "tcp"
localIP = "127.0.0.1"
localPort = "8000-8010"
remotePort = "8000-8010"

ngrok

简介

ngrok 是一个流行的内网穿透工具,使用简单,适合临时测试。

安装使用

# 下载
# https://ngrok.com/download

# 注册账号获取 token
ngrok config add-authtoken your_token

# 启动穿透
ngrok http 80

# 指定端口
ngrok http 8080

# TCP 穿透
ngrok tcp 22

免费版限制

  • 随机域名
  • 1 个隧道
  • 40 连接/分钟

付费版功能

  • 自定义域名
  • 多隧道
  • 更高带宽
  • IP 白名单

Cloudflare Tunnel

简介

Cloudflare Tunnel(原 Argo Tunnel)是 Cloudflare 提供的免费隧道服务,无需公网服务器。

安装 cloudflared

# Linux
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64
chmod +x cloudflared-linux-amd64
sudo mv cloudflared-linux-amd64 /usr/local/bin/cloudflared

# macOS
brew install cloudflare/cloudflare/cloudflared

# Windows
# 下载 exe 文件

快速开始

# 登录 Cloudflare
cloudflared tunnel login

# 创建隧道
cloudflared tunnel create my-tunnel

# 配置隧道
cloudflared tunnel route dns my-tunnel nas.example.com

配置文件

# ~/.cloudflared/config.yml
tunnel: your-tunnel-id
credentials-file: /root/.cloudflared/your-tunnel-id.json

ingress:
  - hostname: nas.example.com
    service: http://192.168.1.100:5000
  - hostname: web.example.com
    service: http://localhost:80
  - service: http_status:404

启动隧道

# 运行隧道
cloudflared tunnel run my-tunnel

# 后台运行
cloudflared service install
systemctl start cloudflared

优点

  • 完全免费
  • 无需公网服务器
  • 自动 HTTPS
  • DDoS 防护
  • 隐藏真实 IP

Tailscale

简介

Tailscale 是基于 WireGuard 的 VPN 组网工具,实现设备间 P2P 直连。

安装

# Linux
curl -fsSL https://tailscale.com/install.sh | sh

# macOS
brew install tailscale

# Windows/macOS/iOS/Android
# 官网下载客户端

使用方法

# 登录
tailscale up

# 查看状态
tailscale status

# 查看 IP
tailscale ip

特点

  • P2P 直连,速度快
  • 无需配置
  • 跨平台支持
  • 免费版支持 100 设备

ZeroTier

简介

ZeroTier 是一个开源的虚拟局域网工具,可以创建虚拟网络。

安装

# Linux
curl -s https://install.zerotier.com | sudo bash

# 加入网络
sudo zerotier-cli join your_network_id

# 授权
# 在 ZeroTier 官网管理页面授权设备

创建网络

  1. 注册 ZeroTier 账号
  2. 创建网络
  3. 获取 Network ID
  4. 设备加入网络
  5. 在管理页面授权

特点

  • 支持路由模式
  • 可配置路由规则
  • 免费版支持 25 设备

其他方案

花生壳

  • 国内老牌方案
  • 有免费版
  • 带宽限制

NATAPP

  • 国内服务
  • 速度快
  • 免费版有限制

Sunny-Ngrok

  • 国内 ngrok 镜像
  • 速度较快
  • 免费版可用

安全建议

基础安全

  1. 使用强密码/Token
  2. 启用 HTTPS
  3. 限制访问 IP
  4. 定期更换密钥

frp 安全配置

# 服务端
auth.method = "token"
auth.token = "strong_random_token"

# 限制用户绑定端口
allowPorts = [
  { start = 6000, end = 7000 }
]

Cloudflare 安全

  1. 启用 Cloudflare Access
  2. 配置访问策略
  3. 开启 WAF

性能优化

frp 优化

# 服务端
transport.maxPoolCount = 5
transport.tcpMux = true
transport.tcpMuxKeepaliveInterval = 60

# 客户端
transport.tcpMux = true

带宽优化

  • 选择就近服务器
  • 使用压缩
  • 合理配置缓存

常见问题

Q: 连接不稳定?

检查:

  1. 服务器网络质量
  2. 防火墙配置
  3. 心跳超时设置

Q: 速度慢?

尝试:

  1. 更换服务器位置
  2. 使用 P2P 方案(Tailscale)
  3. 优化传输配置

Q: 如何选择方案?

  • 长期使用:frp + 公网服务器
  • 网站服务:Cloudflare Tunnel
  • 设备互联:Tailscale
  • 临时测试:ngrok

总结

内网穿透方案各有特点:

需求 推荐方案
稳定长期使用 frp
无服务器需求 Cloudflare Tunnel
设备互联 Tailscale
临时测试 ngrok

根据实际需求选择合适的方案,注意安全配置。