ssh tunnel 问题求助 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
lilifenghao44
V2EX    Linux

ssh tunnel 问题求助

  •  
  •   lilifenghao44 2016-06-29 21:44:42 +08:00 via Android 4923 次点击
    这是一个创建于 3416 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前我将公司的内网机器,自己的 openwrt ,公司内网 web 这些需要的端口都用 autossh 映射到了 vps 上( debian ),我可以在任何地方通过 vps ip :端口号 进行访问( ssh , web , vnc 之类)

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

    请问该如何设置? iptables 目前全部 accept
    17 条回复    2016-06-30 14:15:02 +08:00
    Siril
        1
    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
        2
    Siril  
       2016-06-29 22:47:22 +08:00
    我弄错了, 貌似你是用的 ssh 反向连接。

    先在 vps 上看下监听的地址。
    lilifenghao44
        3
    lilifenghao44  
    OP
       2016-06-29 23:06:55 +08:00 via Android
    @Siril vps 可以看到所有的 ssh 监听端口, curl 能拿到网页, ssh 能连。
    就是无法在其它机器用 vps 的 IP 和端口 进行连接。用 Debian 时是可以的。
    robin4700
        4
    robin4700  
       2016-06-29 23:07:41 +08:00
    /etc/ssh/sshd_config 里加一句 GatewayPorts yes
    fatedier
        5
    fatedier  
       2016-06-29 23:20:32 +08:00   1
    我之前用的 ssh tunnel ,不过用着不是很稳定的样子,可以尝试下我的一个用于内网穿透的开源项目 https://github.com/fatedier/frp
    lilifenghao44
        6
    lilifenghao44  
    OP
       2016-06-29 23:35:4 +08:00 via Android
    @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
        7
    lilifenghao44  
    OP
       2016-06-29 23:42:48 +08:00 via Android
    感谢,明天上班运行看看。
    这个还是要用 vps 跳转所有流量啊。要是能用 Python 写个 socket ,借助 vps 通信,然后可以两个不同 nat 内的机子尝试穿透直接路由就好了。
    lilifenghao44
        8
    lilifenghao44  
    OP
       2016-06-29 23:48:42 +08:00 via Android
    @fatedier 回复忘记 @ 了
    Siril
        9
    Siril  
       2016-06-30 08:25:48 +08:00
    在内网机和 vps 上弄个 n2n / tinc / openvpn 呗。
    lilifenghao44
        10
    lilifenghao44  
    OP
       2016-06-30 08:54:47 +08:00
    @Siril 主要是之前可以用,现在不能用,想找到原因.
    如果用 vpn 的话,其它设备要访问资源也要连进来.
    Siril
        11
    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
        12
    Siril  
       2016-06-30 09:19:38 +08:00
    @lilifenghao44

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



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

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

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

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

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

    我对这方面也比较感兴趣,如果有什么比较好的方案的话希望能够学习借鉴。
    gocoolshj4
        17
    gocoolshj4  
       2016-06-30 14:15:02 +08:00 via Android
    自己编译个 ngrok,超简单
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3428 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 00:41 PVG 08:41 LAX 16:41 JFK 19:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86