Skip to main content

Overview

The hosts command manages /etc/hosts entries for .localhost subdomains. This is needed for Safari, which relies on the system DNS resolver and may not handle .localhost subdomains automatically.

Why This is Needed

  • Chrome, Firefox, Edge: .localhost subdomains auto-resolve to 127.0.0.1 (no configuration needed)
  • Safari: Relies on system DNS, which may not handle .localhost on all configurations
If Safari can’t resolve your .localhost URL, use this command to add entries to /etc/hosts.

Syntax

Subcommands

sync

Add all currently registered routes to /etc/hosts.
Output:
Requires sudo because /etc/hosts is a system file.

clean

Remove all portless-managed entries from /etc/hosts.
Output:

Examples

Safari Not Resolving URLs

If Safari shows “can’t find server” for .localhost URLs:

Clean Up After Development

When you’re done with your project:

Auto-Sync on Route Changes

To automatically update /etc/hosts whenever routes change:
Add to .bashrc or .zshrc to make it permanent:

How It Works

Sync

  1. Reads all routes from the proxy state
  2. Generates /etc/hosts entries (e.g. 127.0.0.1 myapp.localhost)
  3. Adds entries between marker comments:
  4. Preserves other entries in /etc/hosts

Clean

  1. Reads /etc/hosts
  2. Removes lines between # BEGIN portless and # END portless
  3. Removes marker comments
  4. Preserves all other entries

Requirements

  • sudo: Required on all platforms
  • Writable /etc/hosts: Standard on macOS and Linux

Errors

Permission Denied

Output:
Solution: Run with sudo.

No Routes to Sync

Output:
Solution: Start some apps first:

Auto-Sync Without Sudo Proxy

If you set PORTLESS_SYNC_HOSTS=1 but start the proxy without sudo:
The proxy starts but can’t update /etc/hosts. You’ll need to run sudo portless hosts sync manually.

Auto-Sync Environment Variable

Set PORTLESS_SYNC_HOSTS=1 to enable automatic /etc/hosts updates:
Now:
  • Adding a route (portless myapp next dev) updates /etc/hosts
  • Removing a route (process exits) updates /etc/hosts
  • Proxy watches the routes file and syncs on changes

Exit Codes

  • 0 - Success
  • 1 - Error (permission denied, invalid subcommand, no routes)

See Also