如何解决code tunnel安装systemd服务时的Unit未找到错误?
问题场景
执行code tunnel service install命令时,流程提示“成功注册服务”“成功启用单元文件”,但最终报错:
[2023-05-10 17:46:44] error error starting service: org.freedesktop.systemd1.NoSuchUnit: Unit code-tunnel.service not found
系统环境:Linux <hostname> 4.19.0-24-cloud-amd64 #1 SMP Debian 4.19.282-1 (2023-04-29) x86_64 GNU/Linux
已尝试操作:直接执行命令、sudo运行、添加--verbose --log trace参数(未获取有效额外信息)
排查与修复步骤
1. 检查systemd单元文件位置
systemd服务单元文件通常存放在以下路径,手动排查是否存在code-tunnel.service:
- 用户级路径:
~/.config/systemd/user/ - 系统级路径:
/etc/systemd/system/、/usr/lib/systemd/system/
如果上述路径均无该文件,说明安装命令未正确生成单元文件,需手动创建。
2. 手动创建code-tunnel.service单元文件
根据需求选择创建用户级或系统级单元文件(推荐用户级,避免sudo权限依赖):
用户级单元文件(~/.config/systemd/user/code-tunnel.service)
[Unit] Description=VS Code Tunnel Service After=network.target [Service] Type=simple ExecStart=$(which code) tunnel Restart=always RestartSec=5 Environment="PATH=/usr/bin:/bin:/usr/local/bin" [Install] WantedBy=default.target
注:$(which code)会自动获取code命令的绝对路径,也可手动替换为实际路径(如~/.local/bin/code)
系统级单元文件(/etc/systemd/system/code-tunnel.service)
需以sudo权限创建,适合需要系统级运行的场景:
[Unit] Description=VS Code Tunnel Service After=network.target [Service] Type=simple User=<你的用户名> ExecStart=$(which code) tunnel Restart=always RestartSec=5 Environment="PATH=/usr/bin:/bin:/usr/local/bin" [Install] WantedBy=multi-user.target
注:替换<你的用户名>为实际系统用户名
3. 重新加载systemd并启用服务
创建文件后执行对应命令:
- 用户级:
systemctl --user daemon-reload systemctl --user enable --now code-tunnel.service - 系统级:
sudo systemctl daemon-reload sudo systemctl enable --now code-tunnel.service
4. 验证服务状态
检查服务是否正常运行:
- 用户级:
systemctl --user status code-tunnel.service - 系统级:
sudo systemctl status code-tunnel.service
5. 清理残留安装信息(可选)
如果之前的安装存在残留,先彻底清理再重新操作:
code tunnel service uninstall # 删除残留单元文件 rm -f ~/.config/systemd/user/code-tunnel.service sudo rm -f /etc/systemd/system/code-tunnel.service # 重载systemd配置 systemctl --user daemon-reload sudo systemctl daemon-reload
之后重新执行code tunnel service install(推荐使用用户级,不加sudo)
内容的提问来源于stack exchange,提问作者william_grisaitis

