How to Forward Ports in VS Code for Local and Remote Development

Use the VS Code Ports view to expose a local service through a dev tunnel or map a service from an SSH host, Dev Container, or Codespace.

6 min read

You forward a port in VS Code from the Ports view, but the result depends on where the service runs. A local forward uses Microsoft dev tunnels and creates an internet URL. Remote - SSH and Dev Container forwards map the remote service to localhost, while GitHub Codespaces uses a Codespaces URL and visibility policy.

Confirm the service's location before entering its port. The built-in dev-tunnel feature currently exposes only services running on the local VS Code machine; it does not reach through an active remote connection.

Forwarding a local port

Start a trusted local service first. Then open the Ports view in the Panel. If it is hidden, run Ports: Focus on Ports View from the Command Palette.

Click the Forward a Port button. Enter the port number (for example, 3000) and press Enter. You can also give the port a name for easier identification.

The Ports view now shows the port and its Forwarded Address. Hover over the address to copy it, open it in a browser, or use the in-editor preview. Opening a private address prompts for the same GitHub account used to create the forward.

Private and public visibility

For the built-in local dev-tunnel workflow, the port is Private by default. A visitor must sign in with the same GitHub account used to start forwarding.

To share a forwarded service without requiring sign-in, right-click the port in the Ports view and select Port Visibility > Public. Public ports are open to anyone with the link.

Do not set a port to Public if the service exposes sensitive data, accepts unauthenticated commands, or runs with elevated permissions. Public ports are reachable by anyone who has the URL.

Forwarding ports on a remote SSH host

When you are connected to a remote machine through the Remote - SSH extension, the Ports view forwards ports from the remote host.

For temporary forwarding, use the same Forward a Port button. Enter the port number the remote service listens on. VS Code tunnels the remote port to your local machine.

For permanent forwarding, add a LocalForward directive to your SSH config file. This forwards the port every time you connect. Open your SSH config and add:

ssh-configssh-config
Host myserver
    User myuser
    HostName myserver.example.com
    LocalForward 127.0.0.1:3000 127.0.0.1:3000
    LocalForward 127.0.0.1:5432 127.0.0.1:5432

The syntax is LocalForward local-address:local-port remote-address:remote-port. This example maps two remote loopback services to the same port numbers on local loopback.

To make VS Code remember which ports you forwarded in a previous session, enable the setting Remote: Restore Forwarded Ports in the Settings editor.

If the requested local port is already in use, VS Code can assign another port. Check the localhost address in the Ports view. To choose a different local port, right-click the forward and select Change Local Address Port.

Forwarding ports in a Dev Container

In a Dev Container, port forwarding works the same way through the Ports view. For automatic forwarding every time the container starts, add the forwardPorts property to your devcontainer.json:

jsonjson
{
  "image": "mcr.microsoft.com/devcontainers/typescript-node",
  "forwardPorts": [3000, 3001]
}

After changing forwardPorts, reload or reopen the Dev Container window. The Ports view should show a localhost mapping for each configured port when VS Code reconnects.

Forwarding ports in Codespaces

In GitHub Codespaces, a detected localhost URL can trigger automatic forwarding. You can also select Add Port in the Ports view or declare forwardPorts in devcontainer.json.

Codespaces ports are private by default. Depending on organization policy, Port Visibility can also offer organization or public access. A public Codespaces port can be reached by anyone who knows its URL, so do not expose secrets, admin interfaces, or unauthenticated command endpoints.

In a browser-based codespace, do not manually visit localhost:PORT. Select the notification link or globe icon in the Ports view so GitHub can open the generated app.github.dev URL.

Troubleshooting

A local dev-tunnel URL does not load. Confirm the service is running locally and that outbound HTTPS is available. If an organization blocks global.rel.tunnels.api.visualstudio.com, ask its administrator whether the domain is intentionally restricted.

Port already in use locally. VS Code automatically assigns a different local port. Check the Forwarded Address column in the Ports view for the correct URL. You can also change the local port manually by right-clicking the forwarded port.

The wrong machine's service was forwarded. Built-in dev-tunnel forwarding targets the local machine even while VS Code is connected remotely. Use the forwarding feature supplied by Remote - SSH, Dev Containers, or Codespaces for a service running in that remote environment.

Cannot forward port from a Codespace in the browser. Browser-based Codespaces use a different URL scheme for forwarded ports. Look for the port forwarding notification and click the provided link. Do not manually type the localhost URL.

Next, learn how to run and debug web apps with the integrated browser to test your forwarded services, or use VS Code Remote Tunnels for secure access to your entire development machine.

Rune AI

Rune AI

Key Insights

  • Built-in local port forwarding uses Microsoft dev tunnels and creates a shareable URL.
  • Remote - SSH and Dev Containers map remote ports to localhost.
  • Codespaces uses its own forwarded URL and visibility controls.
  • Use LocalForward or devcontainer.json forwardPorts only when the mapping should be recreated.
  • Keep ports private unless the service is safe for unauthenticated internet access.
RunePowered by Rune AI

Frequently Asked Questions

Do I need an extension for port forwarding?

VS Code can expose a locally running service through Microsoft dev tunnels without an extension. Remote SSH, Dev Container, and Codespaces forwarding requires the extension or service that provides that remote connection.

Are forwarded ports secure?

The answer depends on the workflow. Local dev-tunnel and Codespaces ports are private by default and can be made public. SSH and Dev Container forwards normally map a remote service to localhost. Never make an unauthenticated or sensitive service public.

Can I forward ports from a remote SSH machine or Dev Container?

Yes. Remote - SSH and Dev Containers can map a remote or container port to localhost through the Ports view. Use an SSH LocalForward entry or devcontainer.json forwardPorts when the mapping should be recreated automatically.

Conclusion

VS Code uses different port-forwarding mechanisms for different contexts. Use a built-in dev tunnel for a local service that needs an authenticated URL, SSH or Dev Container forwarding for localhost access, and Codespaces port controls for services running in a codespace.