DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Clash 报错 unmarshal errors YAML 配置文件语法修复

排查启动 Clash 核心提示 Fatal: configuration file parse failed: yaml: unmarshal errors 错误,使用 yamllint 校验缩进与排查非法制表符。

问题现象

在启动 Clash 核心(或 Clash.Meta / Mihomo)服务时程序秒退并抛出致命错误:

FATAL: Parse config error: configuration file /root/.config/clash/config.yaml 
tparse failed: yaml: line 45: unmarshal errors:
  line 45: cannot unmarshal !!str `...` into []string

或者提示 yaml: line 12: found character that cannot start any token。

触发根因

YAML 是一种对缩进和数据类型极度敏感的格式:

  1. 使用了制表符(Tab \t)缩进:YAML 规范严禁使用 Tab 缩进,必须全部使用纯空格(Space);
  2. 数据类型不匹配:字段定义要求列表([]string),而配置中写成了裸字符串;
  3. 特殊字符未加双引号包裹(例如密码或 URL 中包含了冒号 : 、井号 # 等)。

解决步骤

1. 使用 sed 强制将所有制表符替换为双空格

在终端执行格式清洗:

sed -i 's/\t/  /g' config.yaml

2. 使用 yamllint 工具定位具体错误行号

安装并运行静态语法分析:

python3 -m pip install yamllint
yamllint -d "{extends: relaxed, rules: {line-length: disable}}" config.yaml

根据工具输出的精确行号与列号,修正非法的键值缩进。

3. 将包含特殊字符的字符串使用双引号包裹

对于容易引发歧义的字段加上引号:

proxies:
  - name: "Node: #01 [HongKong]"
    type: ss
    server: 203.0.113.1
    port: 8388
    cipher: chacha20-ietf-poly1305
    password: "pass:word#123"

验证确认

使用 Clash 校验参数测试配置文件:

clash -t -f config.yaml

输出展示 configuration file test is successful,核心服务可平稳启动。