Overview
Theattach 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
Options
URL of the running OpenCode server (e.g.,
http://localhost:4096 or http://192.168.1.100:4096)Working directory to start TUI in. This is the directory context on the remote server.
Session ID to continue. Short form:
-sExamples
Basic Usage
Start a server in one 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:Comparison with Other Modes
| Feature | TUI (default) | attach | serve | web |
|---|---|---|---|---|
| Terminal interface | ✓ | ✓ | ✗ | ✗ |
| Web interface | ✗ | ✗ | ✗ | ✓ |
| API access | ✗ | ✓ | ✓ | ✓ |
| Remote access | ✗ | ✓ | ✓ | ✓ |
| Multiple clients | ✗ | ✓ | ✓ | ✓ |
| Background server | ✗ | Required | ✓ | ✓ |
Detaching
To detach from the server without stopping it:- Press
Ctrl+Cin the attached terminal - The TUI session ends
- The server continues running
- Attach again anytime to resume
- Connect to the terminal running
serveorweb - Press
Ctrl+Cto stop the server - 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
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.0for network access
Authentication Failed
Problem:Error: 401 Unauthorized
Solutions:
- Verify
OPENCODE_SERVER_PASSWORDmatches the server - Check
OPENCODE_SERVER_USERNAMEif customized - Ensure credentials are set in environment variables
Session Not Found
Problem: Session ID doesn’t exist Solutions:- Omit
--sessionto 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:Related Commands
serve
Start headless backend server
web
Start server with web interface
run
Run commands non-interactively
Network Setup
Configure network access