DDNS内网穿透不稳定的替代方案——SSH Tunnel

  1. SSH container on host
    Create a container on HOST Server which will receive the SSH connection from the remote computer
version: '3'
services:
  relay_sshforward:
    container_name: eec_relay_sshforward
    image: docker.io/panubo/sshd
    restart: 'always'
    expose:
      # SSH Port of container: This port needs to be exposed on host = this is the port used by the remote computer to create ssh connection to the container 10001 (host) = 22 (container)
      - 10001
      # SSH port forwarded to container from remote computer = this is the port of the container where the remote computer will forward its port, for example the SSH port. For example 10002 (host) = 10002 (container) = 22 (remote computer)
      - 10002
      # NGINX port forwarded to container from remote computer = this is the port of the container where the remote computer will forward its port, for example the NGINX port. For example: 10003 (container) = 80 (remote computer). No need to expose 10003 publicly as the host nginx will forward traffic to this port using vhost. => This is only useful if you want to access the remote computer odoo instance from the internet but using the host as a relay.
      - 10003
    ports:
      - '10001:22'
      - '10002:10002'
    environment:
      # You must provide the a username and ID to allow connection
      - SSH_USERS=username:1000:1000
      - MOTD="Now logged inside container on host"
    volumes:
      # You must provide the SSH key used by username to connect to the container without password
      - ./volumes/username.pub:/etc/authorized_keys/username:ro
      - ./volumes/ssh:/etc/ssh/
    networks:
      - webproxy
      
networks:
  webproxy:
    external:
      name: webproxy  
  1. Open ports/firewall on host
    Make sure you open the host port (10001, 10002) so you can connect to it

  2. Remote computer autossh
    On the remote computer, set up a service at boot which will connect to the SSH container on the server (on port 10001), then forward the port 22 of the remote computer to the SSH container on port 10002. This way, you can connect to the remote computer through the HOST using its port 10002.

    Install autossh on remote computer

    sudo apt-get install autossh
    

    Create a file called fwd22.service on the remote computer at ~/
    Remark: see the 10002:localhost:22 username@host.com -p 10001 part ? It says, forward teh port 22 of localhost to the port 10002 of the host, to do this, connect to the host by SSH through port 10001 (SSH container SSH port)

    [Unit]
    Description=Enable ssh tunnel to remote container, forward port 22
    After=network.target
    
    [Service]
    Environment="AUTOSSH_GATETIME=0"
    ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o "ExitOnForwardFailure=Yes" -NR 10002:localhost:22 username@host.com -p 10001
    User=xvin3t
    
    [Install]
    WantedBy=multi-user.target
    

    Enable this service and check if it worked
    It will show you the log. Make sure no issues. You can check on the logs of the SSH container, the connection should show.

    sudo cp fwd22.service /etc/systemd/system
    sudo systemctl enable fwd22.service
    sudo systemctl start fwd22.service
    sudo systemctl status fwd22.service
    

    ====> At this point, you can connect to the remote computer using ssh username@host.com -p 10002 and you will directly be directed to the remote computer.

  3. Enable forwarding to Remote computer nginx
    This is more advanced and not needed for now. But the idea is the same => it allows you to connect to the remote computer nginx (apps) through the server. Instead of forwarding port 22 of remote computer, we forward port 80.
    This part is more complex as you need to manage vhost - to be completed later.

    Create a file called fwd80.service on the remote computer at ~/
    Remark: see the 10003:localhost:80 username@host.com -p 10001 part ? It says, forward teh port 80 of localhost to the port 10003 of the host, to do this, connect to the host by SSH through port 10001 (SSH container SSH port)

    [Unit]
    Description=Enable ssh tunnel to remote container, forward port 80
    After=network.target
    
    [Service]
    Environment="AUTOSSH_GATETIME=0"
    ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o "ExitOnForwardFailure=Yes" -NR 10003:localhost:80 username@host.com -p 10001
    User=xvin3t
    
    [Install]
    WantedBy=multi-user.target
    Enable this service.
    
    sudo cp fwd80.service /etc/systemd/system
    sudo systemctl enable fwd80.service
    sudo systemctl start fwd80.service
    sudo systemctl status fwd80.service
    
    1. Connect your remote server on your PC at office!
    ssh Remote_User@Cloud_Server_IP -p 10002
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值