IoT Remote Access: Reaching Devices Behind NAT and Deciding Where VNC Fits

Most connected devices sit behind a site router or a carrier's NAT that you don't control, so IoT remote access starts with the device dialing out: a reverse SSH tunnel to a bastion you run, a WireGuard overlay, or a vendor relay. Inbound port forwarding only works where you own the router, and it puts whatever you forward on the internet.

Most of what you do on a gateway or an embedded board (logs, config, updates) is shell work, and SSH covers it. VNC earns its place only on a unit that already draws a desktop, such as a kiosk or a board driving a screen, and there it rides the same tunnel with its listener kept on loopback.

Who opens the connection decides which model works

A purple hub panel joins a field device, a router and a person working at a laptop with thin connecting lines.

Four parts are in play: the device, the site router doing NAT, a host on the public internet (a bastion, a VPN hub or a relay), and your workstation. Your inbound connection normally dies at the site router, so one side has to open the path from the inside:

  • Inbound port forward: you connect in through a NAT rule on the site router, which only works where you control that router.
  • VPN or overlay: the device dials a hub (WireGuard here) and gets an address you can reach from your side.
  • Reverse SSH: the device opens an SSH connection to a bastion and asks it to listen on a port that leads back to the device.
  • Relay: the device and your workstation both dial out to a service that joins the two streams.

What travels over the path is a separate decision. SSH carries shell sessions, remote commands and forwarded TCP connections over one encrypted connection, which covers almost everything a gateway needs. VNC's protocol, RFB, carries a graphical session's screen, keyboard and pointer. On a device you normally run a server that shares the session already on screen, so a unit with no graphical session gives VNC nothing to show.

Check what the device actually runs before you pick

A field device with three small check tiles beside it, showing a power switch, an empty window and a row of sockets.

Three things decide the plan: whether SSH is up, whether a graphical session exists, and what is already listening. Run these on the device:

check what the device actually runs before you pick
systemctl is-active ssh
loginctl list-sessions
loginctl show-session <session-id> -p Type
sudo ss -tlnp

systemctl is-active ssh prints active when the SSH server runs; the unit is ssh on Debian and Raspberry Pi OS. Raspberry Pi OS ships with the SSH server disabled, so turn it on under Interface Options in sudo raspi-config first.

loginctl show-session prints a line such as Type=wayland. A session Type of x11, wayland or mir is a graphical login, and tty is a text login. A unit with only tty sessions is a shell-only unit, and Raspberry Pi OS Lite belongs there: VNC is incompatible with the Lite versions.

ss -tlnp lists every listening TCP socket. Read the local address column for anything on 5900 and up: 127.0.0.1 or [::1] is loopback only, while 0.0.0.0, * or [::] means the port answers on every interface, which is the first thing to fix on a device you're about to put on a tunnel.

Port forward, WireGuard, reverse SSH or a relay: what each one needs

A router and a laptop sit behind a firewall panel crossed by four connection paths, with a cloud arc above them.

Model Who dials What has to be true What it means for VNC
Inbound port forward You, from outside A port forward needs a NAT rule and a matching firewall rule on a router you control, and it leaves the forwarded service’s protection to the device itself. It puts RFB on the internet. Don’t forward 5900.
WireGuard overlay The device, to the hub’s Endpoint The device has to send first. Keepalive is off by default, so a device behind NAT needs PersistentKeepalive = 25 to stay reachable while idle. You reach the device’s overlay address. Keep VNC on loopback and forward it over SSH across the overlay.
Reverse SSH to a bastion The device, with ssh -R Outbound SSH from the site and one host with a public address. The bastion binds the forwarded port to its own loopback unless GatewayPorts allows more, and its default is no. Loopback at both ends, walked through below.
Relay service Both ends, outbound AWS IoT Secure Tunneling needs an outbound port the firewall allows, typically 443, and its client access tokens work once, so each reconnect needs rotate-tunnel-access-token. Raspberry Pi Connect goes direct where it can and through an encrypted relay where it can’t. The Greengrass secure tunneling component lists VNC on 5900. Pi Connect shares a screen only on Wayland, and its Lite client gives a shell only.

For a fleet you run yourself, start with reverse SSH: the device needs only the OpenSSH client, the bastion is one small host, and per-key options pin each device to its own port. Move to WireGuard when operators need several ports or protocols on each device, and to a relay when you can't run a public host or the devices already carry that vendor's agent.

If you go the overlay route, a Debian 13 device needs the wireguard-tools package and one file, /etc/wireguard/wg0.conf:

port forward, wireguard, reverse ssh or a relay: what each
[Interface]
PrivateKey = <device-private-key>
Address = 10.8.0.2/24

[Peer]
PublicKey = <hub-public-key>
Endpoint = <hub-host>:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25

sudo wg-quick up wg0 reads that file and brings the interface up, and sudo wg show lists the peer and its last handshake once the hub answers. The keepalive interval is what keeps the NAT mapping open, so the hub can reach the device between its own transmissions.

Set up reverse SSH from the device, with VNC on loopback

A device, a central bastion block and a laptop in a row, linked by two connectors with arrowheads pointing toward the laptop.

The worked run uses a Debian 13 bastion and a Debian 13 device with an X session (OpenSSH 10.0, TigerVNC 1.15.0). The device shares display :0, so its VNC server takes port 5900. The bastion gives this device port 22001, and your workstation uses local port 5901. Change the port numbers per device, not the pattern.

On the bastion: an account that can only hold a forward

Create a tunnel account and limit it with a drop-in. Debian's sshd_config includes /etc/ssh/sshd_config.d/*.conf, so the block lands without touching the main file:

on the bastion: an account that can only hold a forward
sudo useradd -m tunnel
sudo tee /etc/ssh/sshd_config.d/device-tunnels.conf <<'CONF'
Match User tunnel
    AllowTcpForwarding remote
    ForceCommand /usr/sbin/nologin
CONF
sudo sshd -t && sudo systemctl reload ssh

AllowTcpForwarding remote allows ssh -R and refuses -L for this account, and ForceCommand answers any login or remote command with This account is currently not available. The tunnel itself runs no command, so it isn't affected.

Then add each device's public key (the contents of its ~/.ssh/id_ed25519.pub, created in the next step) with options in front of it:

on the bastion: an account that can only hold a forward
sudo install -d -m 700 -o tunnel -g tunnel /home/tunnel/.ssh
echo 'restrict,port-forwarding,permitlisten="localhost:22001" <device-public-key>' | sudo -u tunnel tee -a /home/tunnel/.ssh/authorized_keys
sudo chmod 600 /home/tunnel/.ssh/authorized_keys

restrict turns off forwarding, the PTY and ~/.ssh/rc, port-forwarding turns forwarding back on, and permitlisten holds this key to one listener. A device that asks for any other port, or for *:22001, gets Error: remote port forwarding failed for listen port and nothing binds.

On the device: key, VNC server on loopback, tunnel

on the device: key, vnc server on loopback, tunnel
ssh-keygen

With no arguments ssh-keygen creates an Ed25519 key at ~/.ssh/id_ed25519. Leave the passphrase empty if a service will start the tunnel unattended.

Install the scraping server and the password tool, then set the VNC password as the user who owns the X session:

on the device: key, vnc server on loopback, tunnel
sudo apt install tigervnc-scraping-server tigervnc-tools
tigervncpasswd

tigervncpasswd writes ~/.config/tigervnc/passwd. Version 1.15.0 takes 6 to 8 characters and refuses anything longer with Password should not be greater than 8 characters.

Start the server from a terminal inside the X session, where DISPLAY and the X authority are already set:

on the device: key, vnc server on loopback, tunnel
echo $DISPLAY
x0tigervncserver -localhost yes

With no display argument, x0tigervncserver shares the display in $DISPLAY (:0 on this unit), starts in the background and reports its port, which is 5900 plus the display number. -localhost yes keeps it on loopback, and sudo ss -tlnp should now show port 5900 on 127.0.0.1 and [::1] and nowhere else. Debian's wrapper already picks loopback when you leave the option out and keep the default VncAuth, but it opens every interface as soon as the security types include a TLS or X509 type, so spell it out.

Now open the tunnel. Run it by hand once so you can accept the bastion's host key:

on the device: key, vnc server on loopback, tunnel
ssh -N -o ExitOnForwardFailure=yes -i ~/.ssh/id_ed25519 -R localhost:22001:localhost:5900 tunnel@<bastion>

-N runs no remote command, and ExitOnForwardFailure=yes makes ssh quit with an error when the bastion refuses the listener, instead of staying connected with nothing forwarded. The command holds the terminal; stop it with Ctrl+C once the service below takes over.

To keep the tunnel up across reboots and dropped links, run the same command from /etc/systemd/system/vnc-tunnel.service:

on the device: key, vnc server on loopback, tunnel
[Unit]
Description=Reverse SSH tunnel to the bastion
Wants=network-online.target
After=network-online.target

[Service]
User=<device-user>
ExecStart=/usr/bin/ssh -N -o BatchMode=yes -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -i /home/<device-user>/.ssh/id_ed25519 -R localhost:22001:localhost:5900 tunnel@<bastion>
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
on the device: key, vnc server on loopback, tunnel
sudo systemctl daemon-reload
sudo systemctl enable --now vnc-tunnel.service

BatchMode=yes stops ssh from waiting at a prompt nobody will answer. ServerAliveInterval=30 with ServerAliveCountMax=3 drops a dead link after 90 to 120 seconds without a reply, and Restart=always brings the tunnel back.

If the device runs Wayland

A session Type of wayland rules out x0tigervncserver, which shares an X display. Raspberry Pi OS uses WayVNC and keeps its configuration in /etc/wayvnc/. Its /etc/wayvnc/config sets address=::, so the server answers on every interface. Change that line to:

if the device runs wayland
address=127.0.0.1

If VNC isn't switched on yet, turn it on under Interface Options > VNC in sudo raspi-config first, which also starts it at every boot. Then restart the service:

if the device runs wayland
sudo systemctl restart wayvnc.service

sudo ss -tlnp should then show 127.0.0.1:5900 and nothing on [::]:5900. WayVNC also defaults to port 5900, so the tunnel command doesn't change. Raspberry Pi OS's config sets enable_pam=true, so the viewer asks for the Pi account's user name and password rather than a VNC password.

From your workstation

Check on the bastion that the device's listener is there and stays on loopback:

from your workstation
sudo ss -tlnp | grep 22001
sudo sshd -T | grep -i gatewayports

You want 127.0.0.1:22001 and [::1]:22001, and gatewayports no. Your own account on the bastion sits outside the Match User tunnel block, so it can still make local forwards. Forward a local port to the device's listener:

from your workstation
ssh -N -L localhost:5901:localhost:22001 <you>@<bastion>

Then, from a second terminal, start the viewer:

from your workstation
xtigervncviewer localhost::5901

Debian installs the TigerVNC viewer as xtigervncviewer, with vncviewer pointing to it. The double colon means a TCP port rather than a display number. The viewer asks for the password you set with tigervncpasswd, then opens the device's screen. If the device side never connects, run its ssh command by hand with -v added to see which step fails.

Taking a device off the air

On the device, stop the tunnel service, then the VNC server as the session user; x0tigervncserver -kill prints success! when the server is gone:

taking a device off the air
sudo systemctl disable --now vnc-tunnel.service
x0tigervncserver -kill :0

Where VNC falls short on IoT devices

A field device with one captured display, three lines fanning out to an open padlock, a ring and a crossed circle.

  • No graphical session. A tty-only unit or Raspberry Pi OS Lite has nothing for a scraping server to share, so stay on the shell. If you really need a GUI there, tigervnc-standalone-server runs its own virtual X display with no monitor attached, but you also need a desktop or window manager installed, or the viewer shows an empty screen.
  • One existing display. x0tigervncserver shares an X display that's already running and creates none, and it can't share a Wayland session.
  • Compositor support on Wayland. WayVNC exits on a compositor without the screencopy protocol, which wlroots-based compositors such as Sway and Wayfire implement. On a session with several outputs it captures one, the first by default; wayvncctl output-list shows which, and wayvncctl output-set <output-name> switches it.
  • Weak password, no encryption. VNC Authentication sends the session unencrypted after a DES challenge on a password of at most eight characters. That's the reason the listener stays on loopback and SSH carries the traffic.
  • Fleet work. VNC is for looking at and driving one device's screen. Config, updates and log collection across many devices stay shell work over the same tunnel.

Put every unit on a device-opened reverse SSH tunnel first and treat the shell as the default way in. Add a VNC server only where loginctl reports an x11 or wayland session, keep it on loopback so the tunnel is the only route to that screen, and never forward 5900 through a site router.

FAQs

Can I get a shell on the device through the same tunnel?

Yes. Allow a second listener in the device's key line on the bastion, then add a second remote forward for the device's own SSH port:

can i get a shell on the device through the same tunnel
restrict,port-forwarding,permitlisten="localhost:22001",permitlisten="localhost:22101" <device-public-key>
can i get a shell on the device through the same tunnel
ssh -N -o ExitOnForwardFailure=yes -i ~/.ssh/id_ed25519 -R localhost:22001:localhost:5900 -R localhost:22101:localhost:22 tunnel@<bastion>

From your workstation, jump through the bastion to that port:

can i get a shell on the device through the same tunnel
ssh -J <you>@<bastion> -p 22101 <device-user>@localhost

The device needs its SSH server running for this. Your known_hosts stores the device's key as [localhost]:22101, so give every device its own port or the next device trips the host key check.

How do I keep the ports straight across a fleet?

Give every device its own key line on the bastion with its own permitlisten ports, and keep the device-to-port map with the rest of your inventory. A device that boots with the wrong port in its unit fails with remote port forwarding failed instead of taking another device's listener, because each key can only bind the ports in its own line.

community thread

Discuss this in the community

Questions about this, or a setup it does not cover? Ask in Developer and Integration Topics.

Developer and Integration Topics group

last active 2 Sep 2026, 11:07