ssh tunnel 问题求助

2016-06-29 21:44:42 +08:00
 lilifenghao44
之前我将公司的内网机器,自己的 openwrt ,公司内网 web 这些需要的端口都用 autossh 映射到了 vps 上( debian ),我可以在任何地方通过 vps ip :端口号 进行访问( ssh , web , vnc 之类)

但是最近服务器折腾挂了,换了个 ubuntu ,再次映射后,只能在 vps 本地进行连接。在外网无法用 vps ip :端口进行访问了。

请问该如何设置? iptables 目前全部 accept
4346 次点击
所在节点    Linux
17 条回复
Siril
2016-06-29 22:44:26 +08:00
感觉你描述不清。。。

是不是这种场景:

例如 vps 的 ip 1.2.3.4 , 公司 出口路由器 公网 ip 2.3.4.5 , 公司出口路由器上做好了映射。

然而出于跨 ISP 速度蛋疼之类的原因,由 VPS 转发:
例如 vps 的 2222 转发到公司的 22:
iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 2222 -j DNAT --to-destination 2.3.4.5:22
iptables -t nat -A POSTROUTING -d 2.3.4.5 -p tcp --dport 22 -j SNAT --to-source 1.2.3.4

如此这般,就等价于微林了。
Siril
2016-06-29 22:47:22 +08:00
我弄错了, 貌似你是用的 ssh 反向连接。

先在 vps 上看下监听的地址。
lilifenghao44
2016-06-29 23:06:55 +08:00
@Siril vps 可以看到所有的 ssh 监听端口, curl 能拿到网页, ssh 能连。
就是无法在其它机器用 vps 的 IP 和端口 进行连接。用 Debian 时是可以的。
robin4700
2016-06-29 23:07:41 +08:00
/etc/ssh/sshd_config 里加一句 GatewayPorts yes
fatedier
2016-06-29 23:20:32 +08:00
我之前用的 ssh tunnel ,不过用着不是很稳定的样子,可以尝试下我的一个用于内网穿透的开源项目 https://github.com/fatedier/frp
lilifenghao44
2016-06-29 23:35:45 +08:00
@robin4700 添加这句后,重启客户机的 autossh 重启 vps 的 ssh ,还是不行。

root@lovit:~# cat /etc/ssh/ssh
ssh_config ssh_host_ecdsa_key ssh_host_rsa_key
sshd_config ssh_host_ecdsa_key.pub ssh_host_rsa_key.pub
ssh_host_dsa_key ssh_host_ed25519_key
ssh_host_dsa_key.pub ssh_host_ed25519_key.pub
root@lovit:~# cat /etc/ssh/sshd_config
# Package generated configuration file
# See the sshd_config(5) manpage for details

# What ports, IPs and protocols we listen for
Port 29688
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes

# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 1024

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

X11Forwarding yes
X11DisplayOffset 10
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.

GatewayPorts yes
UsePAM yes
root@lovit:~#
lilifenghao44
2016-06-29 23:42:48 +08:00
感谢,明天上班运行看看。
这个还是要用 vps 跳转所有流量啊。要是能用 Python 写个 socket ,借助 vps 通信,然后可以两个不同 nat 内的机子尝试穿透直接路由就好了。
lilifenghao44
2016-06-29 23:48:42 +08:00
@fatedier 回复忘记 @ 了
Siril
2016-06-30 08:25:48 +08:00
在内网机和 vps 上弄个 n2n / tinc / openvpn 呗。
lilifenghao44
2016-06-30 08:54:47 +08:00
@Siril 主要是之前可以用,现在不能用,想找到原因.
如果用 vpn 的话,其它设备要访问资源也要连进来.
Siril
2016-06-30 09:15:20 +08:00
@lilifenghao44

vps 上一个 vpn ,内网 linux 上一个 vpn , 其他设备就不需要了。
分别作 DNAT + SNAT , 可以转发任意内网 linux 可访问的 tcp/udp 端口 从 vps 访问, 很灵活。

只是。。。 你明显没有公司出口路由器(或防火墙)的控制权限, 如此这般在防火墙上开洞, 也不太合适吧。


给例子:
vps IP 1.2.3.4 , 隧道接口的 ip 192.168.1.1
内网 linux 的 隧道接口 192.168.1.2 , 内网接口 192.168.2.2.
目的为从外网 ssh 到 192.168.2.3:22 , 可以这样:
vps 上打开 ip forwarding ,然后
iptables -t nat -A PREROUTING -d 1.2.3.4 -p tcp --dport 2222 -j DNAT --to-destination 192.168.1.2:1001
iptables -t nat -A POSTROUTING -d 192.168.1.2 -p tcp --dport 1001 -j SNAT --to-source 192.168.1.1
内网 linux 上也打开 ip forwarding , 然后
iptables -t nat -A PREROUTING -d 192.168.1.2 -p tcp --dport 1001 -j DNAT --to-destination 192.168.2.3:22
iptables -t nat -A POSTROUTING -d 192.168.2.3 -p tcp --dport 22 -j SNAT --to-source 192.168.2.2

然后 ssh 到 1.2.3.4:2222 等同于 192.168.2.3:22

个人认为比较简单易配置的有 openvpn 、 n2n 、 tinc , 当然 pptp 、 ipsec 之类也可。
Siril
2016-06-30 09:19:38 +08:00
@lilifenghao44

提到 ip_forwarding , 你检查下这个,是不是相关。



另外, 标准的 vnc 是无加密的,不建议搞成公网可访问。
Siril
2016-06-30 10:22:51 +08:00
@lilifenghao44

避免 vps 中转所有流量, 还要穿透 nat , 则 vpn 貌似只能用 udp , tcp 的不行;
udp 在许多地方的 ISP 有明显的限速, 你试过就知道了;

可以试试 n2n 或 tinc 。
lilifenghao44
2016-06-30 11:15:42 +08:00
@Siril 上面用 shadowVPN 加 iptables 是可以的. 关于这个我有点疑惑需要请问,可以留个联系方式么.
现在边看看推荐的 n2n 用起来怎样.
Siril
2016-06-30 11:30:17 +08:00
@lilifenghao44

QQ 上搜 Siril ,(注意大写首字母), 应该只有 1 个结果,
头顶个面具的就是我。

本人现在是某小公司打杂的 网管 /技术支持, 常有空闲时间。
fatedier
2016-06-30 11:40:13 +08:00
@lilifenghao44 你说的那种打洞方式的话确实效率比较高,不过不一定能穿透所有的 NAT 设备吧, udp 感觉还好, tcp 的话感觉难度会比较大,而且用户也需要安装客户端支持。相对来说通过 VPS 中转流量的话更加稳定,对用户来说是透明的。

我对这方面也比较感兴趣,如果有什么比较好的方案的话希望能够学习借鉴。
gocoolshj4
2016-06-30 14:15:02 +08:00
自己编译个 ngrok,超简单

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/289209

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX