How to SSH to device which is connected via VPN to server?

15,843

Since the PC has a dynamic address, it must be assigned a hostname.

  1. If the dynamic IP is internal to your network and you control the DHCP/DNS server such as Dnsmasq, you can make it assign a hostname.
  2. If the dynamic IP is ISP-assigned, use a DDNS service such as DynDNS to assign your PC a hostname.

Once your PC has a proper hostname, you can setup your server as the SSH jumphost:

  1. Install your laptop's public SSH key in the server and the PC.
  2. In your laptop's ~/.ssh/config, add the following

    Host <PC_hostname>
    ProxyJump <server_hostname>
    

There are 2 alternatives that don't use DDNS.

Make the PC dump its IP in the server:

  1. PC dumps its IP in user@server:~/pc-ip-addr.txt at 1-minute intervals.
  2. In the laptop, read the file and SSH to the PC's IP.

Make PC maintain a permanent SSH connection to server:

  1. In the PC, ssh user@server -R 2222:localhost:2222
  2. In the laptop, ssh user@server "ssh user@localhost -p2222"
Share:
15,843

Related videos on Youtube

Артур Мурадянц
Author by

Артур Мурадянц

Updated on September 18, 2022

Comments

  • Артур Мурадянц
    Артур Мурадянц almost 2 years

    I have a laptop, a server (static IP) and a PC (dynamic IP).

    The PC is connected to the server via OpenVPN service. I want to use this to SSH from my laptop to this PC via server. I assume this is the easiest way to SSH to this computer given it uses a dynamic IP address.

    Is it possible? If yes, how can it be done?

  • Артур Мурадянц
    Артур Мурадянц almost 7 years
    I heard that connecting via VPN is similar to connecting device to your local network. So I thought I can use VPN connection instead of dynamic dns. Is it possible? Is it possible to do with out services like dynnds?
  • Eugene Chow
    Eugene Chow almost 7 years
    It's true that a VPN connection makes your device "local". Since your IP is dynamic, your your laptop needs a way to identify the PC. I've edited the answer above to include 2 DDNS-free methods. Tell me which you prefer and I'll provide more details.