Since 2016 · 50,000+ customers · ★ 5.0 on Google Live chatWhatsAppTelegramOpen a ticket
Get started
Linux

How to Set Up an SSH Tunnel on Ubuntu with a Tunnel-Only User

How to set up an SSH tunnel on Ubuntu guide by 99RDP

An SSH tunnel carries other traffic, such as a database connection, a web admin panel or your browser, inside an encrypted SSH connection. It's a quick, secure way to reach services on a server without opening their ports to the internet. This guide shows how to enable tunnelling on an Ubuntu server, create a dedicated user that can only tunnel (no shell), and use local, remote and dynamic (SOCKS) tunnels from Linux, macOS and Windows.

Quick answerCreate a user without a shell, allow forwarding for that user in sshd_config, then tunnel from your computer:

# server
sudo adduser --shell /usr/sbin/nologin tunnel

# client: reach the server's local port 3306 on your localhost:3306
ssh -N -L 3306:127.0.0.1:3306 tunnel@SERVER_IP

Prerequisites

  • An Ubuntu server (22.04, 24.04 or 26.04) with sudo access.
  • OpenSSH server installed (it is on most VPS images): sudo apt install openssh-server.
  • Port 22 (or your SSH port) allowed in the firewall: sudo ufw allow OpenSSH.

Step 1: Check that SSH forwarding is allowed

OpenSSH allows TCP forwarding by default. Make sure nothing in your config turned it off:

sudo sshd -T | grep -Ei 'allowtcpforwarding|gatewayports|permittunnel'

allowtcpforwarding yes is what you want. If it says no, find and change the line in /etc/ssh/sshd_config or a file in /etc/ssh/sshd_config.d/.

Step 2: Create a tunnel-only user

Don't give tunnel users a full shell. Create one with the nologin shell:

sudo adduser --shell /usr/sbin/nologin --gecos "" tunnel

Better still, use an SSH key instead of a password. On your computer, generate a key (ssh-keygen -t ed25519) and put the public key on the server:

sudo mkdir -p /home/tunnel/.ssh
sudo nano /home/tunnel/.ssh/authorized_keys      # paste the public key
sudo chown -R tunnel:tunnel /home/tunnel/.ssh
sudo chmod 700 /home/tunnel/.ssh
sudo chmod 600 /home/tunnel/.ssh/authorized_keys
sshd_config.d tunnel.conf Match User block with PermitOpen, PermitTTY no and ForceCommand
The tunnel-only Match block

Step 3: Restrict what the tunnel user can do

Create /etc/ssh/sshd_config.d/tunnel.conf with a Match block:

Match User tunnel
    AllowTcpForwarding yes
    PermitOpen 127.0.0.1:3306 127.0.0.1:8080
    X11Forwarding no
    AllowAgentForwarding no
    PermitTTY no
    ForceCommand /bin/false
  • PermitOpen limits which destinations the user may tunnel to. Remove it (or use any) only if the user really needs to reach anything.
  • PermitTTY no and ForceCommand /bin/false make sure no shell or command can run, even if the shell setting changes.
  • For SOCKS (dynamic) tunnels, leave PermitOpen out, because the destinations aren't known in advance.

Test the config and restart SSH:

sudo sshd -t && sudo systemctl restart ssh

On Ubuntu 24.04 and later, SSH may be socket-activated; sudo systemctl restart ssh still applies the change.

Step 4: Create the tunnel from your computer

Local forwarding (-L): reach a service on the server

ssh -N -L 3306:127.0.0.1:3306 tunnel@SERVER_IP

Now connect your database tool to 127.0.0.1:3306 on your computer; the traffic arrives at MySQL on the server as if it were local. -N means "don't run a command", which is what a tunnel-only user needs.

Dynamic forwarding (-D): a SOCKS proxy

ssh -N -D 1080 tunnel@SERVER_IP

Set your browser's SOCKS5 proxy to 127.0.0.1:1080 to browse through the server's IP address.

Remote forwarding (-R): expose a local service through the server

ssh -N -R 8080:127.0.0.1:3000 tunnel@SERVER_IP

This makes your local port 3000 reachable on the server's port 8080 (localhost only, unless GatewayPorts is enabled for that user).

Diagram of an SSH tunnel in PuTTY: a local port on your PC forwards through the Ubuntu server to the destination
How a PuTTY tunnel works

From Windows

Windows 10 and 11 include the same ssh command in PowerShell. In PuTTY, go to Connection → SSH → Tunnels, enter the source port and destination (for example 3306 and 127.0.0.1:3306), click Add, and tick Don't start a shell or command at all under Connection → SSH.

Diagram of an SSH local port forward: localhost:3306 on your computer is carried through SSH to 127.0.0.1:3306 on the server
How a local tunnel works

Step 5: Test and keep the tunnel running

  • Check the tunnel on your computer: ss -ltn | grep 3306 (Linux/macOS) or netstat -an | findstr 3306 (Windows).
  • Confirm the user can't get a shell: ssh tunnel@SERVER_IP should disconnect immediately.
  • For a permanent tunnel, add -o ServerAliveInterval=30 -o ExitOnForwardFailure=yes, or use autossh or a systemd service that restarts it.

Security tips

  • Use key authentication and disable password login for tunnel users (PasswordAuthentication no inside the Match block).
  • Keep the tunnelled services (databases, admin panels) bound to 127.0.0.1 on the server so they're only reachable through the tunnel.
  • Limit destinations with PermitOpen, and remove the user when access is no longer needed.
  • Protect SSH itself with Fail2ban or firewall rules for your known IPs.

Frequently asked questions

How do I create an SSH tunnel on Ubuntu?

From your computer run ssh -N -L localport:127.0.0.1:remoteport user@server. Forwarding is enabled by default in Ubuntu's OpenSSH server.

How do I create an SSH user that can only tunnel?

Create the user with /usr/sbin/nologin as the shell, and add a Match User block with PermitTTY no, ForceCommand /bin/false and optionally PermitOpen.

What's the difference between -L, -R and -D?

-L forwards a local port to a destination via the server, -R forwards a server port back to your machine, and -D creates a SOCKS proxy.

Why does my tunnel user get "This account is currently not available"?

That's the nologin shell working as intended. Connect with -N so SSH doesn't try to start a shell.

Nishant Kumar, Sr. Server Engineer at 99RDP
Written byNishant KumarSr. Server Engineer, 99RDP. Part of the team that runs our servers and answers support.
Keep reading

More Linux guides

All Linux guides

Need a server for this?

✓ Setup in minutes✓ Full admin access✓ 7-day refund on new services