Skip to main content

Overview

The attach command connects a terminal UI (TUI) to an already running OpenCode backend server started via serve or web commands. This allows you to:
  • Use the TUI with a remote OpenCode backend
  • Access the same session from multiple terminals
  • Connect to a server running on another machine
  • Maintain long-running server instances

Usage

If no URL is provided, OpenCode will prompt you to select from recently connected servers or enter a new URL.

Options

url
string
URL of the running OpenCode server (e.g., http://localhost:4096 or http://192.168.1.100:4096)
--dir
string
Working directory to start TUI in. This is the directory context on the remote server.
--session
string
Session ID to continue. Short form: -s

Examples

Basic Usage

Start a server in one terminal:
Attach from another terminal:

Remote Server Connection

Connect to OpenCode running on a remote machine:

Specify Working Directory

Set the working directory context on the remote server:

Continue Specific Session

Attach and continue a specific session:

Use Cases

Development Workflow

Maintain a persistent server while working:

Team Collaboration

Multiple team members can attach to the same server:

Remote Development

Run OpenCode on a powerful remote machine:

Multiple Projects

Run separate servers for different projects:

Authentication

If the server requires authentication, provide credentials:
Avoid including credentials directly in commands or scripts. Use environment variables instead.

Comparison with Other Modes

Detaching

To detach from the server without stopping it:
  1. Press Ctrl+C in the attached terminal
  2. The TUI session ends
  3. The server continues running
  4. Attach again anytime to resume
To stop the server:
  1. Connect to the terminal running serve or web
  2. Press Ctrl+C to stop the server
  3. All attached clients will disconnect

Session Management

When you attach without specifying a session:
  • OpenCode shows you available sessions
  • Select an existing session to continue
  • Or create a new session
To attach to a specific session directly:

Troubleshooting

Connection Refused

Problem: Error: connect ECONNREFUSED Solutions:
  • Verify the server is running
  • Check the URL and port are correct
  • Ensure firewall allows the connection
  • Confirm the server uses --hostname 0.0.0.0 for network access

Authentication Failed

Problem: Error: 401 Unauthorized Solutions:
  • Verify OPENCODE_SERVER_PASSWORD matches the server
  • Check OPENCODE_SERVER_USERNAME if customized
  • Ensure credentials are set in environment variables

Session Not Found

Problem: Session ID doesn’t exist Solutions:
  • Omit --session to see available sessions
  • Verify the session ID is correct
  • Check if the session was deleted

Network Timeout

Problem: Connection times out Solutions:
  • Check network connectivity: ping <server-ip>
  • Verify server is reachable: curl http://<server-ip>:<port>
  • Check firewall rules and network segmentation
  • Try using IP address instead of hostname

Advanced Configuration

SSH Tunneling

Securely connect to a remote server over SSH:

Reverse Proxy

Use a reverse proxy (nginx, Apache) for TLS and advanced routing:
Then attach:

serve

Start headless backend server

web

Start server with web interface

run

Run commands non-interactively

Network Setup

Configure network access