云服务器搭建内网穿透,实现远程访问

云服务器搭建内网穿透,实现远程访问

FRP 自建内网穿透(云服务器中转)

原理:云服务器跑 frps(服务端),家里/内网设备跑 frpc(客户端),内网机器主动向外建立长连接;外网访问云服务器指定端口,流量转发回内网设备。

✅ 优点:完全开源、TCP/UDP/HTTP/HTTPS 都支持、端口不限、可控;国内云机注意合规,不要用来跑违规服务。
⚠️ 前置:一台带公网 IP 的云服务器(阿里云/腾讯云/雨云等);安全组必须放行端口。

QQ20261107-082745

一、准备

  1. 云服务器购买https://act.tag.org.cn:记录公网 IP,系统推荐 Ubuntu/Debian
  2. 下载 frp:https://github.com/fatedier/frp/releases
    • 云服务器:下载对应架构包,只用 frps
    • 内网机器(Windows / Linux / NAS):只用 frpc
  3. 端口规划示例:
    • bindPort = 7000:frpc 和 frps 通信主端口(最重要)
    • vhostHTTPPort = 7080:http 域名穿透
    • vhostHTTPSPort = 7443:https
    • 自定义远程端口:如 6000(ssh)、3390(远程桌面)
  4. 云服务器安全组放行:7000、7080、7443,以及你要用到的 remotePort

二、云服务器:frps(服务端)配置

新建 frps.toml

bindPort = 7000
auth.method = "token"
auth.token = "这里改成你自己的强密码"

# http/https 域名穿透端口
vhostHTTPPort = 7080
vhostHTTPSPort = 7443

# web管理面板(可选)
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "面板密码"

log.to = "/var/log/frps.log"
log.level = "info"
log.maxDays = 7

启动 + 设置开机自启(systemd)

# 放到固定目录
mkdir -p /usr/local/frp
cp frps frps.toml /usr/local/frp/

# 创建服务文件
cat > /etc/systemd/system/frps.service <<EOF
[Unit]
Description=frp server
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=/usr/local/frp
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.toml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable frps
systemctl start frps
systemctl status frps

QQ20261104-070958

三、内网设备:frpc(客户端)配置

新建 frpc.toml,放在内网机器

serverAddr = "你的云服务器公网IP"
serverPort = 7000
auth.token = "和服务端完全一样"

# 示例1:TCP 映射,远程 SSH
[[proxies]]
name = "ssh-local"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000

# 示例2:Windows远程桌面
[[proxies]]
name = "rdp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3389
remotePort = 3390

# 示例3:HTTP网站,自定义域名
[[proxies]]
name = "web"
type = "http"
localPort = 8080
customDomains = ["shturl.cc/1"]

Linux 内网机:systemd 后台常驻

mkdir -p /usr/local/frp
cp frpc frpc.toml /usr/local/frp/

cat > /etc/systemd/system/frpc.service <<EOF
[Unit]
Description=frp client
After=network.target

[Service]
Type=simple
WorkingDirectory=/usr/local/frp
ExecStart=/usr/local/frp/frpc -c /usr/local/frp/frpc.toml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable frpc
systemctl start frpc

Windows 内网机

直接测试:

frpc.exe -c frpc.toml

后台常驻推荐:用 nssm 将 frpc 注册为 Windows 服务。

四、访问方式

  • SSH:ssh root@云服务器IP -p 6000
  • 远程桌面:云IP:3390
  • HTTP 域名:域名解析 A 记录指向云服务器 IP,访问 [http://shturl.cc/1:7080](http://shturl.cc/1:7080)

五、安全优化(必看)

  1. 不要直接暴露 22、3389 默认端口到公网,remotePort 使用高位随机端口
  2. token 设置复杂,不要网上公开配置
  3. 优先使用 stcp(私密 TCP):需要访问方也运行 frpc,不用直接开放公网端口,适合敏感设备
  4. 开启 TLS:frp 支持 transport.tls.enable = true,加密隧道
  5. 国内云服务商:遵守《网络安全法》,不能穿透对外提供公网服务、建站等不合规业务,仅自用运维
  6. 关闭不需要的代理;面板密码不要弱口令

六、其他方案对比

  • frp:自建中转、自由度最高,适合长期自用运维
  • Cloudflare Tunnel:不需要公网端口,走 CF,适合 web,TCP 不方便
  • ngrok:托管,开箱即用,免费版域名随机、限速
  • ZeroTier:异地组网,不是端口转发,所有设备在一个虚拟局域网

七、常见排错

  1. 连不上:优先检查云服务器安全组、防火墙 iptables/firewalld
  2. token 不一致:frps/frpc token 必须完全相同
  3. http 域名打不开:确认域名解析正确、vhostHTTPPort 放行
  4. 断连:内网机器网络不稳,systemd 的 Restart=always 自动重连
posted @ 2026-11-07 08:31  BLOGAME  阅读(9)  评论(0)    收藏  举报