Overview
Thehosts 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:
.localhostsubdomains auto-resolve to127.0.0.1(no configuration needed) - Safari: Relies on system DNS, which may not handle
.localhoston all configurations
.localhost URL, use this command to add entries to /etc/hosts.
Syntax
Subcommands
sync
Add all currently registered routes to /etc/hosts.
/etc/hosts is a system file.
clean
Remove all portless-managed entries from /etc/hosts.
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:
.bashrc or .zshrc to make it permanent:
How It Works
Sync
- Reads all routes from the proxy state
- Generates
/etc/hostsentries (e.g.127.0.0.1 myapp.localhost) - Adds entries between marker comments:
- Preserves other entries in
/etc/hosts
Clean
- Reads
/etc/hosts - Removes lines between
# BEGIN portlessand# END portless - Removes marker comments
- Preserves all other entries
Requirements
- sudo: Required on all platforms
- Writable /etc/hosts: Standard on macOS and Linux
Errors
Permission Denied
No Routes to Sync
Auto-Sync Without Sudo Proxy
If you setPORTLESS_SYNC_HOSTS=1 but start the proxy without sudo:
/etc/hosts. You’ll need to run sudo portless hosts sync manually.
Auto-Sync Environment Variable
SetPORTLESS_SYNC_HOSTS=1 to enable automatic /etc/hosts updates:
- 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
Related Commands
portless list- See which routes will be syncedportless proxy start- Start the proxy
Exit Codes
- 0 - Success
- 1 - Error (permission denied, invalid subcommand, no routes)
See Also
- Environment Variables - Full list including
PORTLESS_SYNC_HOSTS