# 在云服务器上跑 Agent

> **Last verified**: 2026-10-19 · 📄 Per qcode.cc and official docs (tmux / OpenSSH / Ubuntu Server)

> ℹ️ 云服务器的选配页和控制台目前**只有中文和英文界面**。产品页：<https://qcode.cc/cloud>

想在一台**一直开着**的机器上跑 Claude Code 或 Codex：任务跑几十分钟甚至几小时，SSH 断了不能停，还得随时能回去看进度——云服务器就是干这个的。一台机器装好工具、配好 Key，之后的重点就变成两件事：**让任务在断开 SSH 后继续跑**，以及**把登录方式换成密钥**。

机器怎么买见 [云服务器购买与使用](/docs/getting-started/cloud-servers)，计费与退款规则见 [云服务器计费与规则](/docs/reference/cloud-billing)。

## 在服务器上装好 Claude Code / Codex

云服务器页面上给的就是这两条命令，直接在服务器上执行：

```bash
curl -fsSL https://qcode.cc/install/claude-code.sh | bash
```

```bash
curl -fsSL https://qcode.cc/install/codex.sh | bash
```

脚本会测速选接入域、向你要 `cr_` 开头的 Key、写入配置、需要时装好 Node.js 与 CLI，最后发一条最小请求验证连通。完整的参数与回滚方式见 [一键配置脚本](/docs/getting-started/one-click-install)。

## 填 QCode Key 与端点

一键脚本已经代你写好了配置。想手工写、或要核对写对了没有，就看这两处（照 [环境变量配置](/docs/getting-started/environment) 与 [Codex 快速上手](/docs/getting-started/codex-quick-start) 原文）：

**Claude Code**（Anthropic 协议）：

```bash
export ANTHROPIC_BASE_URL="https://api.qcode.cc/api"
export ANTHROPIC_AUTH_TOKEN="cr_你的密钥"
```

**Codex**（OpenAI Responses 协议）——先建目录，再写 `~/.codex/config.toml`：

```bash
mkdir -p ~/.codex
```

```toml
model_provider = "qcode"
model = "gpt-6.1-sol"
model_reasoning_effort = "high"

[model_providers.qcode]
name = "qcode"
base_url = "https://api.qcode.cc/openai"
wire_api = "responses"
requires_openai_auth = true
```

`~/.codex/auth.json`：

```json
{
  "OPENAI_API_KEY": "cr_你的密钥"
}
```

改完记得确认 `base_url` 为 `https://api.qcode.cc/openai`、`ANTHROPIC_BASE_URL` 为 `https://api.qcode.cc/api`，密钥以 `cr_` 开头。手写的 `export` 只对当前这个 shell 有效；要让已经在跑的 tmux 会话也用上，请在 tmux 会话里执行，或者写进 `~/.bashrc`。

## 用 tmux 让任务在断开 SSH 后继续跑

直接 `ssh root@<IP>` 跑任务的话，SSH 一断，前台进程就没了。tmux 的会话是持久的——官方手册的原话是：

> Each session is persistent and will survive accidental disconnection (such as ssh(1) connection timeout) or intentional detaching (with the ‘C-b d’ key strokes).

也就是说：SSH 意外断了也好，你按 `C-b d` 主动脱离也好，会话都还在。

三条常用命令（提示找不到 tmux 时，先用系统的包管理器装上；Ubuntu 用 `sudo apt install tmux`）：

```bash
tmux new -s agent      # 新建一个名为 agent 的会话并进入
tmux ls                # 列出服务器上的所有会话
tmux attach -t agent   # 重新接回刚才那个会话
```

进入会话后想临时走开，按 `Ctrl-b` 再按 `d` 脱离（手册里的默认键位就是 `C-b` 前缀，`d` 对应 "Detach the current client."，意思是「脱离当前客户端」）。任务在会话里照跑，回来时 `tmux attach` 接上就行。手册给出的重新挂载方式也是 `$ tmux attach`。

## SSH 安全：先换成密钥登录，确认能登了再关密码登录

顺序很重要。Ubuntu 官方文档在讲改 OpenSSH 配置时提醒过：如果你唯一的访问方式就是 SSH，配置改错了，重启之后可能把自己锁在门外。所以**先验证、再关闭**。

**下面的命令以 Ubuntu 为例**；其它发行版的服务名和配置路径以对应发行版自己的文档为准，思路（先验证密钥、再关密码、改完先检查）是一样的。

1. **在本机生成密钥对**（Ubuntu Server 文档原文）：

   ```bash
   ssh-keygen -t ed25519
   ```

2. **把公钥装到服务器上**（在 macOS / Linux，或 Windows 的 WSL 里执行；`chmod` 那条要在服务器上、登录后的家目录下执行）：

   ```bash
   ssh-copy-id root@<IP>
   ```

   Ubuntu 文档写的是把 `id_ed25519.pub` 的内容追加到目标机器的 `~/.ssh/authorized_keys`，并提醒检查该文件的权限——只有认证用户可写，不对就执行 `chmod go-w .ssh/authorized_keys`。

3. **验证密钥能登上**。文档的原话是：*"You should now be able to SSH to the target_machine without being prompted for a password."*（Ubuntu 页面里的 `target_machine` 指你的服务器地址；这句话的意思是：不用再问你密码就能登进去。）这一步**禁止回退到密码**，否则密钥没装好也会被密码放行：

   ```bash
   ssh -o PasswordAuthentication=no -o PreferredAuthentications=publickey -i ~/.ssh/id_ed25519 root@<IP>
   ```

   加上 `-o PasswordAuthentication=no` 后，密钥不对会直接报 `Permission denied`，不会再问密码；被拒绝就**不要往下做**，回到第 2 步。这时**别关掉现在这个密码登录的会话**，另开一个终端窗口。

4. **确认能登之后**，再关掉密码登录。先查是哪些文件在管这个选项——Ubuntu 的 `/etc/ssh/sshd_config` 顶部有 `Include /etc/ssh/sshd_config.d/*.conf`，`sshd_config.d/` 里的设置会覆盖主文件：

   ```bash
   sudo grep -rEi '^\s*PasswordAuthentication' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
   ```

   把**每一处**命中的行都改成 `PasswordAuthentication no`（包括 `sshd_config.d/` 里写着 `yes` 的文件）。一处都没命中时，才在 `/etc/ssh/sshd_config` 末尾加一行 `PasswordAuthentication no`。原因是 sshd_config(5) 写明："Unless noted otherwise, for each keyword, the first obtained value will be used."（除非另有说明，每个关键字取最先读到的那个值），`Include` 的文件按字典序展开处理，排在前面的值先生效。手册对这两个选项的说明是：`PasswordAuthentication` —— "Specifies whether password authentication is allowed."（是否允许密码认证）；`PubkeyAuthentication` —— "Specifies whether public key authentication is allowed."（是否允许公钥认证）。

5. **先检查语法、再看生效值、最后重启**：

   ```bash
   sudo sshd -t
   sudo sshd -T | grep -Ei '^(passwordauthentication|pubkeyauthentication) '
   ```

   `sshd -T` 打印的是 sshd 实际采用的配置（sshd(8) 的 "extended test mode"：配置会写到标准输出）。必须看到 `passwordauthentication no` 和 `pubkeyauthentication yes` 才继续重启；如果 `passwordauthentication` 还是 `yes`，说明还有别的文件在覆盖，回到上一步。`sshd -t` 报错就先别重启。

   ```bash
   sudo systemctl restart ssh.service
   ```

6. **重启后再验一次，老会话先别关**。另开一个新终端，用第 3 步那条命令再用密钥登录一次；再试一次只用密码的登录，应当被拒绝：

   ```bash
   ssh -o PubkeyAuthentication=no root@<IP>
   ```

   看到 `Permission denied` 说明密码登录已关；两条都符合预期，才关闭最初那个会话。

> 💡 到这一步为止，你已经有一台可以长期跑 Agent 的机器：工具装好了、Key 和端点填好了、任务掉线不掉、登录方式也换成了密钥。

## 费用：两部分

- **服务器**：按你下单时选的时长（1、3、6 或 12 个月）从余额扣费，时长越长折算到每月越便宜；起价见 qcode.cc/cloud 的线路对照表。
- **模型**：按 token 计费，各模型单价见 [qcode.cc/models](https://qcode.cc/models)。

两笔费用从**同一个账户余额**里扣。

## 相关页面

- [云服务器购买与使用](/docs/getting-started/cloud-servers) — 选线路、1 天试用、下单与交付
- [一键配置脚本](/docs/getting-started/one-click-install) — 装好工具并写入配置
- [环境变量配置](/docs/getting-started/environment) — 各个工具分别读哪些变量
- [Codex 快速上手](/docs/getting-started/codex-quick-start) — Codex 的 config.toml 与 auth.json
- [云服务器计费与规则](/docs/reference/cloud-billing) — 时长、自动续费、退款与可接受使用政策