> For the complete documentation index, see [llms.txt](https://gushi10546.gitbook.io/aptos-kai-fa-zhe-wen-dang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gushi10546.gitbook.io/aptos-kai-fa-zhe-wen-dang/jie-dian/ben-di-ce-shi-wang/yong-cli-yun-xing-ben-di-ce-shi-wang.md).

# 用 CLI 运行本地测试网

{% hint style="info" %}
💡 **用源代码或 Docker 来运行本地测试网**&#x20;

如果你想用 Docker 或者 `aptos-core` 源代码来启动并运行本地测试网，参阅[用验证器运行本地测试网](/aptos-kai-fa-zhe-wen-dang/jie-dian/ben-di-ce-shi-wang/yong-yan-zheng-qi-yun-xing-ben-di-ce-shi-wang.md)。
{% endhint %}

运行 Aptos 区块链的本地测试网，不会被连接到 Aptos 开发网，它将在你的本地机器上运行，独立于其他的 Aptos 网络。你可以使用这个本地测试网进行测试和开发。本地测试网可以针对已知版本的代码库进行本地开发，而不需要与实时网络互动或处理实时网络的现实成本。

{% hint style="info" %}
💡 **Aptos CLI 文档** 如果你还不了解 Aptos CLI，可以先看看这份全面的 [Aptos CLI 文档](/aptos-kai-fa-zhe-wen-dang/aptos-cli.md)。
{% endhint %}

### 从水龙头开始一个本地测试网

可以用下面这条命令来开始一个本地测试网：

```
aptos node run-local-testnet --with-faucet
```

上面的命令回启动一个本地验证器节点，并且会显示类似下面的终端输出：

```
Completed generating configuration:
        Log file: "/Users/greg/.aptos/testnet/validator.log"
        Test dir: "/Users/greg/.aptos/testnet"
        Aptos root key path: "/Users/greg/.aptos/testnet/mint.key"
        Waypoint: 0:74c9d14285ec19e6bd15fbe851007ea8b66efbd772f613c191aa78721cadac25
        ChainId: TESTING
        REST API endpoint: 0.0.0.0:8080
        Fullnode network: /ip4/0.0.0.0/tcp/6181

Aptos is running, press ctrl-c to exit

Faucet is running.  Faucet endpoint: 0.0.0.0:8081
```

上述命令将使用验证器节点的默认配置。

{% hint style="info" %}
**不要在同一时间使用同一命令的两个实例**

注意，同一命令的两个实例不能同时运行。这会导致验证器节点的端口冲突。
{% endhint %}

### 用本地测试网进行测试

你可以使用 Aptos CLI 进行全方位的本地测试网操作。关于如何配置 CLI，请先看下文。

#### 配置 Aptos CLI 来使用本地测试网

你可以添加一个单独的配置文件，如下所示：

```
aptos init --profile local --rest-url <http://localhost:8080> --faucet-url <http://localhost:8081>
```

会得到如下的输出。在 `Enter your private key...` 命令提示符i啊，按回车键生成一个随机的新密钥。

```
Configuring for profile local
Using command line argument for rest URL <http://localhost:8080/>
Using command line argument for faucet URL <http://localhost:8081/>
Enter your private key as a hex literal (0x...) [Current: None | No input: Generate new key (or keep one if present)]
```

这将创建一个新的账户，并以默认的代币数量为其提供资金，如下所示：

```
No key given, generating key...
Account 7100C5295ED4F9F39DCC28D309654E291845984518307D3E2FE00AEA5F8CACC1 doesn't exist, creating it and funding it with 10000 coins
Aptos is now set up for account 7100C5295ED4F9F39DCC28D309654E291845984518307D3E2FE00AEA5F8CACC1!  Run `aptos help` for more information about commands
{
  "Result": "Success"
}
```

此时，你应该在命令中添加 `--profile local` 以便在本地测试网上运行这些命令。

### 创建帐户并为帐户注资

要在本地测试网上创建新帐户，我们建议使用上述说明并使用不同的配置文件名称：

```
PROFILE=local
aptos init --profile $PROFILE --rest-url <http://localhost:8080> --faucet-url <http://localhost:8081>
```

为帐户注资：

```
aptos account fund --profile $PROFILE --account $PROFILE
```

创建资源帐户：

```
aptos account create-resource-account --profile $PROFILE --seed 1
```

### 将模块发布到本地测试网

可以通过添加 `--profile $PROFILE` 标志来运行任何命令。在这种情况下，我们也使用 `$PROFILE` 来作为 `HelloBlockchain` 示例中的命名地址。

```
aptos move publish --profile $PROFILE --package-dir /opt/git/aptos-core/aptos-move/move-examples/hello_blockchain --named-addresses HelloBlockchain=$PROFILE
{
  "Result": {
    "changes": [
      {
        "address": "7100c5295ed4f9f39dcc28d309654e291845984518307d3e2fe00aea5f8cacc1",
        "data": {
          "authentication_key": "0x7100c5295ed4f9f39dcc28d309654e291845984518307d3e2fe00aea5f8cacc1",
          "coin_register_events": {
            "counter": "1",
            "guid": {
              "id": {
                "addr": "0x7100c5295ed4f9f39dcc28d309654e291845984518307d3e2fe00aea5f8cacc1",
                "creation_num": "0"
              }
            }
          },
          "sequence_number": "4"
        },
        "event": "write_resource",
        "resource": "0x1::account::Account"
      },
    ...
    ],
    "gas_used": 59,
    "success": true,
    "version": 6261,
    "vm_status": "Executed successfully"
  }
}
```

### 重置本地状态

如果你更新了你的代码库，并进行了向后不兼容的修改，或者只是想重新开始，你可以用 `--force-restart` 标志运行命令：

```
aptos node run-local-testnet --with-faucet --force-restart
```

然后，它将提示你是否真的要重新启动链，以确保你不会意外地删除你的工作。

```
Are you sure you want to delete the existing chain? [yes/no] >
```

### 常见问题

#### 提示地址已被使用该怎么办？

如果你得到一个类似这样的错误：

```
'panicked at 'error binding to 0.0.0.0:9101: error creating server listener: Address already in use (os error 48)'
```

这意味着你要么已经在运行一个节点，要么你有另一个进程在该端口上运行。

在 macOS 和 Linux 上，你可以运行以下命令来获得使用该端口的进程的名称和 PID：

```
PORT=9101
lsof -i :$PORT
```

#### 在哪里可以得到关于 run-local-testnet 命令的更多信息？

更多 CLI 帮助可以运行这个命令看到：

```
aptos node run-local-testnet --help
```

这将提供关于该命令的每个标志的信息。
