Skip to content

服务器命令行命令(Server CLI commands)#

服务器 CLI 是一个内置的命令行接口,运行在与你的 n8n 安装相同的机器上。它为管理任务提供直接的数据库访问,并且即使 n8n 未运行,也可以执行大多数命令。

🌐 The Server CLI is a built-in command-line interface that runs on the same machine as your n8n installation. It provides direct database access for administrative tasks and can execute most commands even when n8n isn't running.

n8n 命令行接口

想要从远程机器以编程方式与 n8n 互动或与 AI 代理集成?查看 n8n CLI

何时使用服务器 CLI 与 n8n CLI(When to use Server CLI vs n8n CLI)#

功能 服务器 CLI n8n CLI
运行位置 与 n8n 相同的机器 任何有网络访问的机器
认证方式 直接数据库访问 API 密钥
需要运行 n8n 不需要(大多数命令) 需要
适合对象 实例运算符、备份、迁移 程序员、AI 代理、远程管理
安全模型 绕过访问控制 遵循用户权限和 API 密钥范围
使用案例示例 备份/恢复、许可证管理、紧急密码重置 通过代码进行工作流自动化、凭证管理

运行 CLI 命令(Running CLI commands)#

你可以在自托管的 n8n 中使用 CLI 命令。根据你选择安装 n8n 的方式,运行命令的方法会有所不同:

🌐 You can use CLI commands with self-hosted n8n. Depending on how you choose to install n8n, there are differences in how to run the commands:

  • npm:n8n 命令可以直接使用。文档在下面的示例中使用了它。
  • Docker:n8n 命令可以在你的 Docker 容器中使用:

    1
    docker exec -u node -it <n8n-container-name> <n8n-cli-command>
    

启动工作流(Start a workflow)#

你可以使用 CLI 直接启动工作流。

🌐 You can start workflows directly using the CLI.

按 ID 执行已保存的工作流:

🌐 Execute a saved workflow by its ID:

1
n8n execute --id <ID>

发布或取消发布工作流(Publish or unpublish a workflow)#

你可以使用 CLI 发布或取消发布工作流。在 n8n 2.0 中,以前的激活/非激活切换 已被发布/取消发布模型取代。使用 publish:workflowunpublish:workflow 可以从 CLI 更改工作流的发布状态。

🌐 You can publish or unpublish a workflow using the CLI. In n8n 2.0, the previous active/inactive toggle was replaced by a publish/unpublish model. Use publish:workflow and unpublish:workflow to change a workflow's published state from the CLI.

需要重新启动

这些命令会作用于你的 n8n 数据库。如果在 n8n 运行时执行,它们的更改在重启 n8n 之前不会生效。

发布工作流(Publish a workflow)#

使用 publish:workflow 通过其 ID 发布工作流。你可以通过传入其 versionId 可选择性地发布特定的历史版本。

🌐 Use publish:workflow to publish a workflow by its ID. You can optionally publish a specific historical version by passing its versionId.

命令标志:

🌐 Command flags:

标志 描述
--help 帮助提示。
--id 要发布的工作流 ID。必填。
--versionId 可选的要发布的版本 ID。如果省略,将发布当前草稿。

没有 --all 标志

与已弃用的 update:workflow 命令不同,publish:workflow 不支持 --all。这是故意为之:它可以防止在生产环境中意外批量发布工作流。请通过 ID 单独发布工作流。

通过ID发布当前的工作流草稿:

🌐 Publish the current draft of a workflow by ID:

1
n8n publish:workflow --id=<ID>

发布工作流的特定历史版本:

🌐 Publish a specific historical version of a workflow:

1
n8n publish:workflow --id=<ID> --versionId=<VERSION_ID>

取消发布工作流(Unpublish a workflow)#

使用 unpublish:workflow 通过其 ID 取消发布工作流,或一次取消发布所有工作流。

🌐 Use unpublish:workflow to unpublish a workflow by its ID, or all workflows at once.

命令标志:

🌐 Command flags:

标志 描述
--help 帮助提示。
--id 要取消发布的工作流ID。不能与 --all 一起使用。
--all 取消发布所有工作流。不能与 --id 一起使用。

通过其ID取消发布工作流:

🌐 Unpublish a workflow by its ID:

1
n8n unpublish:workflow --id=<ID>

取消发布所有工作流:

🌐 Unpublish all workflows:

1
n8n unpublish:workflow --all

更新:工作流(已弃用)(update:workflow (deprecated))#

在 n8n 2.0 中已弃用

update:workflow 命令已被弃用,将被移除。请改用 publish:workflowunpublish:workflow。详情请参见 n8n v2.0 重大更改

将工作流 ID 的活动状态设置为 false:

🌐 Set the active status of a workflow by its ID to false:

1
n8n update:workflow --id=<ID> --active=false

将工作流 ID 的活动状态设置为 true:

🌐 Set the active status of a workflow by its ID to true:

1
n8n update:workflow --id=<ID> --active=true

将所有工作流的活动状态设置为 false:

🌐 Set the active status to false for all the workflows:

1
n8n update:workflow --all --active=false

将所有工作流的活动状态设置为 true:

🌐 Set the active status to true for all the workflows:

1
n8n update:workflow --all --active=true

导出实体(Export entities)#

你可以使用 CLI 从 n8n 导出数据库实体。该工具允许你将一种数据库类型(例如 SQLite)的所有实体类型导出,并导入到另一种数据库类型(例如 Postgres)中。

🌐 You can export your database entities from n8n using the CLI. This tooling allows you to export all entity types from one database type, such as SQLite, and import them into another database type, such as Postgres.

命令标志:

🌐 Command flags:

标志 描述
--help 帮助提示。
--outputDir 输出目录路径
--includeExecutionHistoryDataTables 包含执行历史数据表,默认情况下不包括它们,因为它们可能非常大
1
n8n export:entities --outputDir=./outputs --includeExecutionHistoryDataTables=true

导出工作流和凭据(Export workflows and credentials)#

你可以使用 CLI 从 n8n 导出工作流和凭据。

🌐 You can export your workflows and credentials from n8n using the CLI.

命令标志:

🌐 Command flags:

标记 描述
--help 帮助提示。
--all 导出所有工作流/凭据。
--backup 设置 --all --pretty --separate 用于备份。你可以选择设置 --output。
--id 要导出的工作流的 ID。
--output, -o 输出文件名或如果使用单独文件则为目录。
--pretty 以更易读的格式输出。
--separate 每个工作流导出一个文件(适用于版本管理)。必须使用 --output 设置目录。
--decrypted 以纯文本格式导出凭据。(仅凭据)
--version 要导出的特定历史版本的版本 ID。(仅工作流,不能与 --all--published 一起使用。)
--published 导出工作流的已发布/活动版本,而不是当前草稿。与 --all 结合使用时,未发布的工作流将被跳过。(仅工作流,不能与 --version 一起使用。)

工作流(Workflows)#

将所有工作流导出到标准输出(终端):

🌐 Export all your workflows to the standard output (terminal):

1
n8n export:workflow --all

按 ID 导出工作流并指定输出文件名:

🌐 Export a workflow by its ID and specify the output file name:

1
n8n export:workflow --id=<ID> --output=file.json

将所有工作流导出到指定目录的单个文件中:

🌐 Export all workflows to a specific directory in a single file:

1
n8n export:workflow --all --output=backups/latest/file.json

使用 --backup 标志将所有工作流导出到指定目录(详情见上文):

🌐 Export all the workflows to a specific directory using the --backup flag (details above):

1
n8n export:workflow --backup --output=backups/latest/

导出特定的工作流版本(Export a specific workflow version)#

你可以通过传递其 versionId--version 来导出工作流的特定历史版本:

🌐 You can export a specific historical version of a workflow by passing its versionId with --version:

1
n8n export:workflow --id=<ID> --version=<VERSION_ID> --output=workflow-v1.json

导出工作流的已发布版本(Export the published version of a workflow)#

使用 --published 导出工作流的当前已发布/活动版本,而不是当前草稿:

🌐 Use --published to export the currently published/active version of a workflow rather than the current draft:

1
n8n export:workflow --id=<ID> --published --output=published.json

你可以将 --published--all 结合使用来导出每个工作流的已发布版本。没有已发布版本的工作流将被跳过:

🌐 You can combine --published with --all to export every workflow's published version. Workflows that don't have a published version are skipped:

1
n8n export:workflow --all --published --output=workflows.json

版本元数据

在导出工作流时,n8n 会包含一个 versionMetadata 属性,其中包含该版本的工作流历史名称和描述。导入命令在导入时会将这些数据保存在工作流历史表中。当前工作流的名称和描述不会被覆盖。

凭据(Credentials)#

将所有凭据导出到标准输出(终端):

🌐 Export all your credentials to the standard output (terminal):

1
n8n export:credentials --all

按 ID 导出凭据并指定输出文件名:

🌐 Export credentials by their ID and specify the output file name:

1
n8n export:credentials --id=<ID> --output=file.json

将所有凭据导出到指定目录的单个文件中:

🌐 Export all credentials to a specific directory in a single file:

1
n8n export:credentials --all --output=backups/latest/file.json

使用 --backup 标志将所有凭证导出到指定目录(详情见上文):

🌐 Export all the credentials to a specific directory using the --backup flag (details above):

1
n8n export:credentials --backup --output=backups/latest/

以明文格式导出所有凭据。你可以使用此方法将数据从一个安装迁移到另一个配置文件中具有不同密钥的安装。

🌐 Export all the credentials in plain text format. You can use this to migrate from one installation to another that has a different secret key in the configuration file.

敏感信息

所有敏感信息都可以在文件中看到。

1
n8n export:credentials --all --decrypted --output=backups/decrypted.json

导入实体(Import entities)#

你可以使用此命令从先前的 export:entities 命令导入实体,它允许将实体导入到与导出数据库类型不同的数据库类型。当前支持的数据库类型包括:SQLite、Postgres。

🌐 You can import entities from a previous export:entities command using this command, it allows importing of entities into a database type that differs from the exported database type. Current supported database types include: SQLite, Postgres.

导入之前预计数据库是空的,可以使用 --truncateTables 参数强制清空。

🌐 The database is expected to be empty prior to import, this can be forced with the --truncateTables parameter.

命令标志:

🌐 Command flags:

标志 描述
--help 帮助提示
--inputDir 保存用于导入的输出文件的输入目录
--truncateTables 导入前清空表格
1
n8n import:entities --inputDir ./outputs --truncateTables true

导入工作流和凭证(Import workflows and credentials)#

你可以使用 CLI 从 n8n 导入工作流和凭据。

🌐 You can import your workflows and credentials from n8n using the CLI.

更新ID

导出工作流和凭证时,n8n 也会导出它们的 ID。如果你在现有数据库中已有相同 ID 的工作流和凭证,它们将被覆盖。为避免这种情况,请在导入前删除或更改 ID。

可用的标志:

🌐 Available flags:

标志 描述
--help 帮助提示。
--input 输入文件名或目录(如果使用 --separate)。
--projectId 将工作流或凭证导入到指定项目。不能与 --userId 一起使用。
--separate 从 --input 提供的目录导入 *.json 文件。
--userId 将工作流或凭证导入到指定用户。不能与 --projectId 一起使用。
--skipMigrationChecks 跳过迁移验证检查。
--activeState 控制导入工作流的激活状态。接受 false(默认,停用所有导入的工作流)或 fromJson(使用每个工作流 JSON 中的 active 字段;仅多主模式)。

迁移到 SQLite

n8n 将工作流和凭证名称限制为 128 个字符,但 SQLite 并不强制大小限制。

这可能会在导入过程中导致诸如 列名称数据过长 的错误。

在这种情况下,你可以从 n8n 界面编辑名称后再次导出,或者在导入前直接编辑 JSON 文件。

工作流(Workflows)#

已知问题:导入后 cron 触发器仍然运行

导入先前处于活动状态的工作流的行为取决于你正在运行的模式。这是一个已知的错误。

在多主和队列模式实例中,导入时先前处于活动状态的工作流的 cron 触发器会被停用。

在非多主实例中,先前处于活动状态的工作流的 cron 触发器将保持运行,直到你重启 n8n 实例。

从指定文件导入工作流:

🌐 Import workflows from a specific file:

1
n8n import:workflow --input=file.json

从指定目录导入所有工作流文件(JSON 格式):

🌐 Import all the workflow files as JSON from the specified directory:

1
n8n import:workflow --separate --input=backups/latest/

导入时的版本元数据

如果导入的文件包含 versionMetadata 属性(由针对特定版本或已发布版本的导出添加),n8n 会在工作流历史记录表中保留该历史名称和描述。当前工作流实体的名称和描述保持不变。

默认情况下,import:workflow 会停用每个导入的工作流。要改为保留每个 JSON 文件中的 active 字段,请传入 --activeState=fromJson(仅在多主模式和队列模式下支持):

🌐 By default, import:workflow deactivates every imported workflow. To preserve the active field from each JSON file instead, pass --activeState=fromJson (only supported in multi-main & queue mode):

1
n8n import:workflow --separate --input=backups/latest/ --activeState=fromJson

凭据(Credentials)#

从指定文件导入凭据:

🌐 Import credentials from a specific file:

1
n8n import:credentials --input=file.json

从指定目录导入所有凭据文件(JSON 格式):

🌐 Import all the credentials files as JSON from the specified directory:

1
n8n import:credentials --separate --input=backups/latest/

许可证(License)#

清除(Clear)#

从 n8n 数据库中清除你现有的许可证,并将 n8n 重置为默认功能:

🌐 Clear your existing license from n8n's database and reset n8n to default features:

1
n8n license:clear

如果你的许可证包含浮动许可,运行此命令也会尝试将其释放回许可池,使其可供其他实例使用。

🌐 If your license includes floating entitlements, running this command will also attempt to release them back to the pool, making them available for other instances.

信息(Info)#

显示有关现有许可证的信息:

🌐 Display information about the existing license:

1
n8n license:info

用户管理(User management)#

你可以使用 n8n CLI 重置用户管理。这会将用户管理恢复到设置前的状态,并删除所有用户账户。

🌐 You can reset user management using the n8n CLI. This returns user management to its pre-setup state. It removes all user accounts.

如果你忘记了密码,并且没有设置 SMTP 以通过电子邮件重置密码,请使用此选项。

🌐 Use this if you forget your password, and don't have SMTP set up to do password resets by email.

1
n8n user-management:reset

为用户禁用多因素身份验证 (MFA)(Disable MFA for a user)#

如果用户丢失了恢复代码,你可以使用此命令为用户禁用多因素认证(MFA)。然后用户将能够重新登录并重新设置 MFA。

🌐 If a user loses their recovery codes you can disable MFA for a user with this command. The user will then be able to log back in to set up MFA again.

1
n8n mfa:disable --email=johndoe@example.com

禁用 LDAP(Disable LDAP)#

你可以使用以下命令重置 LDAP 设置。

🌐 You can reset the LDAP settings using the command below.

1
n8n ldap:reset

卸载社区节点和凭证(Uninstall community nodes and credentials)#

你可以使用 n8n CLI 管理社区节点。目前,你只能卸载社区节点和凭据,这在社区节点导致不稳定时非常有用。

🌐 You can manage community nodes using the n8n CLI. For now, you can only uninstall community nodes and credentials, which is useful if a community node causes instability.

命令标志:

🌐 Command flags:

Flag Description
--help Show CLI help.
--credential The credential type. Get this value by visiting the node's <NODE>.credential.ts file and getting the value of name.
--package Package name of the community node.
--uninstall Uninstalls the node.
--userId The ID of the user who owns the credential. On self-hosted, query the database. On cloud, query the API with your API key.

节点(Nodes)#

按包名卸载社区节点:

🌐 Uninstall a community node by package name:

1
n8n community-node --uninstall --package <COMMUNITY_NODE_NAME>

例如,要卸载 Evolution API 社区节点,请输入:

🌐 For example, to uninstall the Evolution API community node, type:

1
n8n community-node --uninstall --package n8n-nodes-evolution-api

凭据(Credentials)#

卸载社区节点凭证:

🌐 Uninstall a community node credential:

1
n8n community-node --uninstall --credential <CREDENTIAL_TYPE> --userId <ID>

例如,要卸载 Evolution API 社区节点凭证,请访问 仓库 并导航到 credentials.ts 文件 查找 name

🌐 For example, to uninstall the Evolution API community node credential, visit the repository and navigate to the credentials.ts file to find the name:

1
n8n community-node --uninstall --credential evolutionApi --userId 1234

安全审计(Security audit)#

你可以对你的 n8n 实例运行一次安全审计,以检测常见的安全问题。

🌐 You can run a security audit on your n8n instance, to detect common security issues.

1
n8n audit