SSH into a server with no public IP
A server behind NAT or in a private subnet has no address that the internet can reach. This guide explains each way to get an SSH session to that server, with the cost of each option. It also shows how to do it with no inbound port.
The private side dials out
The Gateway makes an outbound connection to the control plane. The NAT router permits it, as it permits any outbound traffic.
You sign in
The Client signs in, and the control plane checks that a Policy gives you access to the server.
The two sides meet
The Client and the Gateway punch a hole through the NAT router and set up a WireGuard tunnel.
SSH uses the private address
You run ssh with the private address of the server. The Gateway forwards TCP/22 and no other port.
Why a server has no public IP address
SSH needs a route from your laptop to port 22 on the server. A server with a public IP address has that route. A server with no public IP address does not.
There are three common causes:
- NAT at home or in an office. The router has the one public address. The server has a private address such as
192.168.1.20. - Carrier-grade NAT. The internet provider shares one public address between many customers. You cannot forward a port, because you do not control that router.
- A private subnet in a cloud. The server has no public address by design, so that the internet cannot reach it.
NAT permits connections that start inside the network. It drops connections that start outside, because it does not know which machine must get them. SSH behind NAT fails for that reason.
Each option in this guide solves the same problem. Either you open a path in from the internet, or the private side makes a connection out.
Options that open an inbound port
Port forwarding
You tell the router to send a public port to port 22 on the server. Port forwarding takes two minutes and costs nothing.
The cost comes later. The SSH server is now on the internet, and scanners find it fast. The logs fill with password attempts. You also need a static address or a dynamic DNS name, and port forwarding does not work behind carrier-grade NAT.
If you use port forwarding, disable password sign-in, use SSH keys only, and restrict the source addresses in the firewall.
A bastion host
A bastion host is one small server with a public IP address. You connect to it first, and then you jump to the private server with ssh -J.
A bastion host keeps the private servers off the internet, which is a real improvement. But the bastion host has an open SSH port, and you must patch it, monitor it, and manage its keys. It also needs a network route to the private server. It works for a private subnet in a cloud, and it does not work for a machine at home.
Options that connect outbound
A reverse SSH tunnel
The private server starts the connection. It connects out to a public server that you control and asks that server to listen on a port:
ssh -N -R 2222:localhost:22 user@relay.example.com
You then connect to port 2222 on the public server, and the traffic goes back through the tunnel to the private server. A reverse SSH tunnel works through any NAT, with no router change.
It has limits. You must run and secure the public server. The tunnel drops when the network changes, so you need a tool such as autossh or a systemd unit to start it again. Each private server needs its own tunnel and its own port.
A session manager from a cloud provider
Most large clouds have a session manager. An agent on the virtual machine connects out to the cloud provider. You start a shell through the cloud console or the command line tool. Cloud identity controls who can connect.
This option is good when all servers are in one cloud. It does not help with a machine at home, in an office, or in a different cloud.
An overlay network
An overlay network puts an agent on each side. Each agent connects out to a coordination service, and the two agents then set up an encrypted tunnel through NAT. The server gets a stable private address that works from any network.
Overlay networks and zero trust access products use this design. They differ in how precisely you can limit access, and in whether each server needs an agent.
How to choose
The correct option depends on how many servers you have and who needs access.
- One machine and one person. A reverse SSH tunnel or an overlay network is enough. Do not use port forwarding unless you can restrict the source addresses.
- Servers in one cloud. The session manager of that cloud is a good first choice.
- A team, many servers, or more than one network. Use an overlay network or zero trust access with a rule for each person and each server.
For all options, keep these SSH rules:
- Use SSH keys, and disable password sign-in.
- Do not let the root user sign in.
- Keep the SSH server up to date.
- Remove the key of each person who leaves.
How to SSH without a public IP in Firezone
Firezone is an open source zero trust access platform built on WireGuard. Clients and Gateways make outbound connections only, and they use NAT hole punching to reach each other. You open no inbound port. Firezone gives you two ways to SSH without a public IP address.
A Gateway in the network
Use this way for servers in a private subnet, or for many machines on one LAN. The servers need no agent.
- Deploy a Gateway on a Linux host in the same network as the servers. A small virtual machine or a Raspberry Pi is enough.
- Add an IP Resource or a DNS Resource for the server.
- Add a traffic restriction of TCP/22, so that only SSH passes.
- Write a Policy that gives your Group access to the Resource.
You then run ssh user@10.0.1.15 from any network. The remote host guide shows each step.
A Device Pool with no Gateway
Use this way for a single machine, such as a workstation at home. You install a Firezone Client on the two machines and add the target to a Device Pool. A headless Client is available for Linux.
Each Client gets a stable address and a name such as build-box.firezone.network. You limit the Device Pool to TCP/22 and write a Policy for the Group that can connect. The Device Pool guide shows each step.
In the two cases, traffic goes direct between the two ends when the network permits it. If a direct connection is not possible, the encrypted traffic goes through a Firezone relay. The SSH server still authenticates you with your key. Firezone controls who can reach port 22, and it does not manage SSH keys.
Port forward, bastion host, reverse tunnel, and Firezone
The four approaches differ in what stays open to the internet and in how much you must run.
| Port forward | Bastion host | Reverse tunnel | Firezone | |
|---|---|---|---|---|
| No inbound port at the private network | NoPort 22 is public | NoPort 22 on the bastion host | Yes | Yes |
| No public server for you to run | Yes | NoThe bastion host | NoThe relay server | YesFirezone runs the relays |
| Works behind carrier-grade NAT | No | No | Yes | Yes |
| Works with no agent on the server | Yes | Yes | NoThe server starts the tunnel | YesWith a Gateway in the network |
| A rule for each person and each server | No | PartialKeys on each server | No | YesOne Policy for each Resource |
| Connects again after a network change | Yes | Yes | PartialNeeds a restart tool | Yes |
| Free to use | Yes | PartialYou pay for the host | PartialYou pay for the relay server | PartialFree Starter plan |
The table describes the usual design of each category. A specific product or setup can be different. Read the documentation before you decide.
Common questions
Can I SSH into a computer behind NAT without port forwarding?
Yes. The computer behind NAT must start the connection. A reverse SSH tunnel, a session manager from a cloud provider, an overlay network, and zero trust access all work this way. None of them needs a change on the router.
How do I SSH into a server in a private subnet?
You need a hop that can reach the private subnet. The hop can be a bastion host in a public subnet, or the session manager of your cloud. It can also be a Gateway inside the subnet that connects out. The last two options keep port 22 closed to the internet.
Is a reverse SSH tunnel safe?
A reverse SSH tunnel is encrypted, and it opens no port on the private network. The risk is on the public server. By default, the forwarded port listens on the loopback address of that server only. Keep that default, and use SSH keys on the two hops.
Does port forwarding work behind carrier-grade NAT?
No. With carrier-grade NAT, the public address belongs to the internet provider, and many customers share it. You cannot add a rule to that router. Use an option that connects outbound.
Do I need a static IP address to SSH into my server?
Not with an outbound option. A reverse tunnel, an overlay network, and Firezone find the server through a service that the server connects to. The public address of the network can change at any time.
Does Firezone replace my SSH keys?
No. Firezone decides which machines can reach port 22 on the server. The SSH server still checks your key or your password. Firezone does not manage SSH keys or SSH certificates.