Skip to main content

Command Palette

Search for a command to run...

远程访问私有 ip 地址的服务

Updated
•2 min read•View as Markdown

什么是公网 IP 和私有 IP 地址

在互联网中公网 IP 和私有 IP 地址用于不同的场景。

公网 IP 地址

一个公网 IP 地址是网络服务商(ISP)分配给每个设备独特的地址,如 Google 服务器,家庭路由器。每个公网 IP 地址在整个互联网中是独一无二的,Google 服务器的 IP 和 微软服务器的 IP 是不同的,他们都能从世界的任何角落访问。

ssh username@server_ip_address

私有 IP 地址

与此对比,私有 IP 地址是在一个私有网络中使用,比如一个家庭内或办公网络内的设备可以互相连接。这些地址对互联网的其他地方不可见也不能访问。私有地址一般由相同网络内的路由器分配给每个设备。不同私有网络中的设备可以拥有相同的私有地址。

私有 IP 地址的区间由 Internet Assigned Numbers Authority (IANA)指定,这些区间是:

  • Class A: 10.0.0.0 — 10.255.255.255

  • Class B: 172.16.0.0 — 172.31.255.255

  • Class C: 192.168.0.0 — 192.168.255.255

公网 IP 可以是这些区间和一些保留地址外的任何 IP 地址。

简而言之,公网 IP 地址和私有 IP 地址的主要区别在于它们的范围、通信和唯一性。 公网 IP 地址 可被全球访问,用于在互联网上通信,而私有 IP 地址仅可被本地设备访问,用于在私有网络上通讯。

如何 SSH 访问私有 IP 服务器

1.使用 sshx

github.com/ekzhang/sshx

sshx 是一个基于 web 的协作终端。你仅需运行一条命令即可将你的终端分享给任何人并可以实时看到其他人的光标移动。

# 运行下面命令获取 sshx 。它很小,数秒内即可下载好(3 MB).
curl -sSf https://sshx.io/get | sh
# 安装之后运行 sshx
sshx
# 你将看到类似这样的输出:https://sshx.io/aaaa#bbbb

现在从浏览器中打开 sshx.io/aaaa#bbbb 即可远程访问你的 ssh 服务器。

2.使用 bore

https://github.com/ekzhang/bore

bore 是一个现代、简洁 Rust 编写的 TCP 通道,可将本地端口转发至远端服务器,绕过常见的 NAT 连接限制。

# 首先在你的 ssh server 上运行下面命令
bore local 22 --to bore.pub
# 上述命令将会输出类似下面的内容
2024-01-12T08:21:47.663201Z  INFO : connected to server remote_port=19215
2024-01-12T08:21:47.663201Z  INFO : listening at bore.pub:19215

现在从任何远程位置运行下面命令即可访问你的 ssh 服务器。

ssh username@bore.pub -p 19215

如何访问私有 IP 的 HTTP 服务器

3.使用 Cloudflare Tunnel

按照链接中的说明下载和安装 cloudflaredhttps://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/

运行下面命令开启一条 HTTP 隧道。

cloudflared tunnel --url http://localhost:8080
# 你会看到类似下面输出
+----------------------------------------------------------+
|  Your quick Tunnel has been created!                     |
|  Visit it at (it may take some time to be reachable):    |
|  https://kuwait-played-bt-frequencies.trycloudflare.com  |
+----------------------------------------------------------+

现在从远程任何位置在你的浏览器打开 https://kuwait-played-bt-frequencies.trycloudflare.com 即可访问。

本地 HTTP 服务器 将收到类似下面请求

GET / HTTP/1.1

{
  "host": "kuwait-played-bt-frequencies.trycloudflare.com",
  "user-agent": "Mozilla/5.0 (...) Gecko/20100101 Firefox/115.0",
  "accept": "text/html,...,*/*;q=0.8",
  "accept-encoding": "gzip",
  "accept-language": "en-US,en;q=0.5",
  "cdn-loop": "cloudflare; subreqs=1",
  "cf-connecting-ip": "*.*.93.87",
  "cf-ew-via": "15",
  "cf-ipcountry": "JP",
  "cf-ray": "87bc2ed4765280f3-NRT",
  "cf-visitor": "{\"scheme\":\"https\"}",
  "cf-warp-tag-id": "5dcce6a4-c3eb-4a29-8b92-ab00c7981824",
  "cf-worker": "trycloudflare.com",
  "connection": "keep-alive",
  "dnt": "1",
  "sec-fetch-dest": "document",
  "sec-fetch-mode": "navigate",
  "sec-fetch-site": "none",
  "sec-fetch-user": "?1",
  "upgrade-insecure-requests": "1",
  "x-forwarded-for": "*.*.93.87",
  "x-forwarded-proto": "https"
}

如果你想改变接收到的 Host 头, 可使用 --http-host-header 选项:

cloudflared tunnel --url http://localhost:8080 --http-host-header yourhost

More from this blog

IRead Fun

30 posts