termux_ngrok_内网穿透

termux ngrok 内网穿透

前言

这篇教程以termux为例,当然其他系统也可以使用以下方法。

一、安装ngrok工具

安装ngrok工具建议去官网下载

ngrok官网链接

termux先在在命令行中输入uname -m确认内核

1
2
  ~ $ uname -m
  aarch64

arrch64也是ARM64,所以在官网中获取下载链接后输入到命令行中

1
  wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-arm64.tgz

解压,给予执行权限并移动到$PREFIX/bin文件夹下

1
2
3
  tar ngrok-stable-linux-arm64.tgz
  chomod +x ngrok
  mv ngrok $PREFIX/bin/

测试

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  .../usr/bin $ ngrok
  NAME:
  ngrok - tunnel local ports to public URLs and inspect traffic

  DESCRIPTION:
  ngrok exposes local networked services behinds NATs and firewalls to the
  public internet over a secure tunnel. Share local websites, build/test
  webhook consumers and self-host personal services.
  Detailed help for each command is available with 'ngrok help <command>'.
  Open http://localhost:4040 for ngrok's web interface to inspect traffic.

  EXAMPLES:
	  ngrok http 80                    # secure public URL for port 80 web server
	  ngrok http -subdomain=baz 8080   # port 8080 available at baz.ngrok.io
	  ngrok http foo.dev:80            # tunnel to host:port instead of localhost
	  ngrok http https://localhost     # expose a local https server
	  ngrok tcp 22                     # tunnel arbitrary TCP traffic to port 22
	  ngrok tls -hostname=foo.com 443  # TLS traffic for foo.com to port 443
	  ngrok start foo bar baz          # start tunnels from the configuration file

  VERSION:
	 2.3.35

  AUTHOR:
	inconshreveable - <alan@ngrok.com>

  COMMANDS:
	 authtoken    save authtoken to configuration file
	 credits      prints author and licensing information
	 http         start an HTTP tunnel
	 start        start tunnels by name from the configuration file
	 tcp          start a TCP tunnel
	 tls          start a TLS tunnel
	 update       update ngrok to the latest version
	 version      print the version string
	 help         Shows a list of commands or help for one command

安装完成


二、使用

安装成功后要到官网注册一个账号或直接用GitHub账号登录

然后在本地建立一个ssh密钥

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  .../usr/bin $ ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa
  Generating public/private rsa key pair.
  /data/data/com.termux/files/home/.ssh/id_rsa already exists.
  Overwrite (y/n)? y
  Enter passphrase (empty for no passphrase):
  Enter same passphrase again:
  Your identification has been saved in /data/data/com.termux/files/home/.ssh/id_rsa
  Your public key has been saved in /data/data/com.termux/files/home/.ssh/id_rsa.pub
  The key fingerprint is:
  SHA256:IVPTTiVL0fQKi/g+iCkMzZO++VBDQsHd6ZV4wQqEOTQ u0_a345@localhost
  The key's randomart image is:
  +---[RSA 2048]----+
  | oE*.. ++++=o    |
  | .=.o +.=ooo..   |
  |  ...oo+.oo   .  |
  |   o  o+ o.o .   |
  | o .o . S . .    |
  |. =. . .         |
  | +.. o ..        |
  |  +oo ...        |
  |  o+.   ..       |
  +----[SHA256]-----+
  .../usr/bin $

公钥路径

1
  ~ $ cat $HOME/.ssh/id_rsa.pub

将公钥粘贴到ngrok网站里
点击Add SSH Key
Public Key:填入公钥
到这里获取认证信息
然后回到termux输入认Authoken

1
  ~ $ ngrok authtoken [Your Authoken]

完成

实验

重点:在启动服务之前启动一下termux-chroot,不然可能连接不上

测试不在同一网段使用ssh链接shell:

在其他设备的命令行输入连接shell(以windows 10 为例)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  # 启动ngrok服务 ssh
  ~ $ ssh -R 0:localhost:8022 tunnel.us.ngrok.com tcp 8022
  Enter passphrase for key '/data/data/com.termux/files/home/.ssh/id_rsa':
  Allocated port 18594 for remote forward to localhost:8022

  ngrok (via SSH) by @inconshreveable (Ctrl+C to quit)

  Account     Alioth-yuheng (Plan: Free)
  Region      us
  Forwarding  tcp://0.tcp.ngrok.io:18594

  # windows 10 cmd
  C:\Users\Administrator>ssh -p 18594 0.tcp.ngrok.io
  The authenticity of host '[0.tcp.ngrok.io]:18594 ([3.13.191.225]:18594)' can't be established.
  ECDSA key fingerprint is SHA256:cmIJf1VfAU3B8o9bCq7czAC4rRDl7lRAy0A6ujAl/5g.
  Are you sure you want to continue connecting (yes/no)? yes
  Warning: Permanently added '[0.tcp.ngrok.io]:18594,[3.13.191.225]:18594' (ECDSA) to the list of known hosts.
  alioth@0.tcp.ngrok.io's password:

  Welcome to Termux!

  Community forum: https://termux.com/community
  Gitter chat:     https://gitter.im/termux/termux
  IRC channel:     #termux on freenode

  Working with packages:

  ,* Search packages:   pkg search <query>
  ,* Install a package: pkg install <package>
  ,* Upgrade packages:  pkg upgrade

  Subscribing to additional repositories:

  ,* Root:     pkg install root-repo
  ,* Unstable: pkg install unstable-repo
  ,* X11:      pkg install x11-repo

  Report issues at https://termux.com/issues

  The Google Play version of the Termux app no longer
  receives updates. For more information, visit:
  https://wiki.termux.com/wiki/Termux_Google_Play

  ~ $

解决遇到过的报错

  • 启动服务后到127.0.0.1:4040点击了网页链接却报错:
    解决:端口要有服务在运行,比如在80端口配置了apache服务,命令就应该是
1
2
3
  ~ $ ngrok http 80
  # 或
  ~ $ ngrok https 80
  • 启动ngrok失败的可能性1
    没有安装两个软件
1
  $ pkg install proot resolv-conf
  • 启动ngrok失败的可能性2
    没有进入termux-chroot
1
  $ termux-chroot

5 分钟,使用内网穿透快速实现远程手机桌面

Licensed under CC BY-NC-SA 4.0