
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
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
PermitOpenlimits which destinations the user may tunnel to. Remove it (or useany) only if the user really needs to reach anything.PermitTTY noandForceCommand /bin/falsemake sure no shell or command can run, even if the shell setting changes.- For SOCKS (dynamic) tunnels, leave
PermitOpenout, 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).
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.
Step 5: Test and keep the tunnel running
- Check the tunnel on your computer:
ss -ltn | grep 3306(Linux/macOS) ornetstat -an | findstr 3306(Windows). - Confirm the user can't get a shell:
ssh tunnel@SERVER_IPshould disconnect immediately. - For a permanent tunnel, add
-o ServerAliveInterval=30 -o ExitOnForwardFailure=yes, or useautosshor a systemd service that restarts it.
Security tips
- Use key authentication and disable password login for tunnel users (
PasswordAuthentication noinside the Match block). - Keep the tunnelled services (databases, admin panels) bound to
127.0.0.1on 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.


