我有一个Android设备,我想通过3g / 4g连接,我正在拒绝连接。
任何想法如何做到这一点?
我有一个Android设备,我想通过3g / 4g连接,我正在拒绝连接。
任何想法如何做到这一点?
I am having an Android device and I want to connect in via 3G/4G and I am getting connection refused.
Any ideas how to do that?
如果在Android设备上运行的工作SSH服务器,则可以在本地/专用网络上连接到它,而不存在任何问题(在适当的身份验证设置后显然)。如果您的手机具有真正的公共IP地址(我认为在地球上发生),则可以保持相同的公共网络(互联网)。但是,当您需要交叉网络时,即遍历网关和路由器,涉及网络地址转换。如果您的SSH服务器无法接触到公共IP地址,您将无法从Internet访问您的设备。
nat和pat?
简单的单词,NAT是从私人地址到公共路由的源IP的转换;当IP数据包离开路由器时,他们称之为 - 地址,以便可以使用可识别的IP地址发送数据包。在路由器处收回相同数据包的响应时发生反转。它意味着将本地网络上每个主机(电话,PC等)的私有IP映射到唯一的公共IP。但在通常的情况下,我们只有一个由ISP分配的公共IP。因此端口地址转换出现了行动。 Pat将每个本地IP地址转换为相同的公共IP地址,但使用唯一的端口。
什么是cgnat?
要解决 IP地址短缺,Internet服务提供商还执行NAT,称为运营商级NAT 。这意味着ISP向您分配的IP地址不是公共但私有IP地址。这是一个具有移动数据的明确情况,即3G / 4G,但与DSL连接不太常见。
如果ISP分配给手机的IP(您可以通过 ip address | grep inet
命令检查)与这里,你是一个cgnat。同样对于Wi-Fi路由器,PPP连接设置下出现的IP地址应与公共IP匹配。
动态IP地址:
为了最大限度地利用可用的IP地址池,即使没有CGNAT,ISP也会主要分配动态IP地址。因此,根据ISP的政策,公众IP每天,每周,每月或随机或随机等待。
ipv6 是不需要NAT的即将到来的协议地球上的网络主机将能够拥有唯一的IP地址。
解决方案:
现在来你的问题:
如果没有cgnat,我们可以通过设置端口转发。但是这个选项不起作用如果:
通过注册DDNS服务可以解决动态IP问题。它为您指定了始终解析为当前公共IP的域名。 dynudns 是一个适合我的免费服务。您必须在手机或PC上安装软件,以使其更新您的公共IP。
如果您使用最后一个选项(使用公共/静态/专用IP地址设置个人SSH或VPN服务器),请按照以下步骤将特定端口从SSH / VPN服务器转发到手机:
ssh:
使用ssh服务器设置,可以从手机创建 abcdefghijklmnReverse Port Forwarding
隧道。请注意,如果您在同一端口上连接到服务器,则无法将远程服务器(默认SSH)端口22转发到手机端口22。
确保<代码> GatewayPorts yes 和 AllowTcpForwarding yes
在服务器上设置为 sshd_config
,因此sshd允许端口转发并接受公共连接。现在在手机上创建一个反向隧道:
~$ ssh -NTR 2222:localhost:22 <server_user>@<server_ip>
您可以使用或一些应用程序如 ConnectBot
如果需要。
进一步选项,请参阅此答案
vpn:
使用VPN应用程序连接到VPN服务器。当在虚拟专用网络(VPN)上时,服务器和手机都成为本地网络的一部分,因此只需应用 iptables DNAT
将特定端口转发到手机的IP:
~# iptables -t nat -I PREROUTING -p tcp --dport 2222 -j DNAT --to <phone_ip>:22
服务器上也需要在VPN服务器配置期间已设置的IP转发:
Reverse Port Forwarding0
Reverse Port Forwarding2
身份验证后您将登录到您的手机。
我在这里解释了ssh的例子。以同样的方式,您可以在手机上运行任何其他服务器,并将其端口从SSH / VPN服务器(带有公共IP)转发到手机(没有公共IP),以便您的服务器可从Internet访问。
相关:
If you have a working SSH server running on Android device, you can connect to it on local/private network without any issues (after proper authentication setup obviously). Same may hold true for public network (internet) if your phone has a true public IP address (I don't think that happens on earth). However, when you need to cross networks i.e. traversing gateways and routers, there is Network Address Translation involved. You won't be able to access your device from internet if your SSH server can't be exposed to a public IP address.
WHAT IS NAT AND PAT?
In simple words, NAT is the translation of source IP from private address to public - routable; what they call it - address when an IP packet is leaving the router, so that packet could be sent back with identifiable IP address. Inverse happens when response for same packet is received back at router. It would mean mapping private IP of every host (phone, PC etc.) on local network to a unique public IP. But in usual cases, we just have one public IP assigned by ISP. So Port Address Translation comes in action. PAT translates every local IP address to same public IP address but with a unique port.
WHAT IS CGNAT?
To address the problem of IP Address Shortage, Internet Service Providers also perform NAT, called Carrier Grade NAT. It means that IP address assigned by ISP to you isn't either a public but private IP address. This is a definite situation with Mobile Data i.e. on 3G/4G but less common with DSL connections.
If the IP assigned to your phone by ISP (you can check by ip address | grep inet
command) is different from the one shown here, you are behind a CGNAT. Similarly for Wi-Fi router, IP address appearing under PPP Connection Settings should match with the public IP.
DYNAMIC IP ADDRESS:
In order to maximum utilize the available pool of IP addresses, ISP's mostly assign dynamic IP address even when there is no CGNAT. So the public IP keeps on changing on daily, weekly, monthly or randomly basis, depending on the ISP's policy.
IPv6 is the upcoming protocol which won't need NAT and every network host on earth would be able to have a unique IP address.
SOLUTION:
Now coming to your question:
If there is no CGNAT, we can tackle the problem of DSL/3G/4G router's NAT by setting up Port Forwarding. But this option doesn't work if:
Problem of Dynamic IP can be worked around by signing up a DDNS service. It assigns you a domain name that always resolves to your current public IP. DynuDNS is a free service that works perfect for me. You will have to install their software on your phone or PC to keep them updated of your public IP.
If you go with last option (setup a personal SSH or VPN server with public/static/dedicated IP address), follow the steps below to forward a specific port from SSH/VPN server to your phone:
SSH:
With an SSH server setup, you can create Reverse Port Forwarding
tunnel from your phone. Note that you can't forward remote server's (default SSH) port 22 to your phone's port 22 if you are connecting to the server on same port.
Make sure GatewayPorts yes
and AllowTcpForwarding yes
are set in sshd_config
on server, so that sshd allows port forwarding and accepts connections from public. Now on your phone create a reverse tunnel:
~$ ssh -NTR 2222:localhost:22 <server_user>@<server_ip>
You can make ssh
tunnel persistent using autossh or some app like ConnectBot
if you want.
For further options see this answer.
VPN:
From your phone connect to the VPN server using a VPN app. When on a Virtual Private Network (VPN), both server and phone become part of a local network, so just apply iptables DNAT
to forward the specific port to your phone's IP:
~# iptables -t nat -I PREROUTING -p tcp --dport 2222 -j DNAT --to <phone_ip>:22
IP Forwarding is also required on server, which has probably been already set up during VPN server configuration:
~# echo 1 >/proc/sys/net/ipv4/ip_forward ~# iptables -I FORWARD -d <phone_ip> -j ACCEPT ~# iptables -I FORWARD -s <phone_ip> -j ACCEPT
Now you can ssh
from any host on internet to your phone:
~$ ssh <phone_user>@<server_ip> -p 2222
You'll be logged in to your phone after authentication.
I explained here the SSH example. In the same way you can run any other server on your phone and forward its port from SSH/VPN server (with public IP) to your phone (with no public IP) so that your server becomes accessible from internet.
RELATED:
© 2022 it.wenda123.org All Rights Reserved. 问答之家 版权所有