autossh 在终端运行良好,但机器人导致 systemctl 的权限被拒绝

autossh 在终端运行良好,但机器人导致 systemctl 的权限被拒绝

我正在尝试在树莓派上集成 autossh。

如果我单独启动命令

autossh -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" -N -R 22002:localhost:22 user@myserver

一切正常,但从 systemctl 启动相同的命令

[Unit]
Description=AutoSSH tunnel service Remote port 22002 to local 22
After=network.target
[Service]
Environment="AUTOSSH_GATETIME=0"
ExecStart=/usr/bin/autossh -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" -N -R 22002:localhost:22 [email protected]
[Install]
WantedBy=multi-user.target

返回

Jun 25 13:43:22 pi autossh[995]: Permission denied, please try again.
Jun 25 13:43:22 pi autossh[995]: Permission denied, please try again.
Jun 25 13:43:22 pi autossh[995]: Permission denied (publickey,password).
Jun 25 13:43:22 pi autossh[995]: ssh exited with error status 255; restarting ssh

我可以从我的 pi 无需密码即可 ssh 我的服务器。

/var/log/auth.log 中的日志给了我这个

Jun 26 13:00:51 localhost sshd[9252]: Connection closed by 178.135.1.253 port 41786 [preauth]
Jun 26 13:00:51 localhost sshd[9254]: Connection closed by 178.135.1.253 port 41788 [preauth]
Jun 26 13:00:51 localhost sshd[9256]: Connection closed by 178.135.1.253 port 41790 [preauth]
Jun 26 13:00:51 localhost sshd[9258]: Connection closed by 178.135.1.253 port 41792 [preauth]
Jun 26 13:00:52 localhost sshd[9260]: Connection closed by 178.135.1.253 port 41794 [preauth]
Jun 26 13:00:52 localhost sshd[9262]: Connection closed by 178.135.1.253 port 41796 [preauth]
Jun 26 13:00:54 localhost sshd[9264]: Connection closed by 178.135.1.253 port 41798 [preauth]
Jun 26 13:01:03 localhost sshd[9267]: Connection closed by 178.135.1.253 port 41802 [preauth]
Jun 26 13:01:05 localhost sshd[9269]: Connection closed by 178.135.1.253 port 41804 [preauth]

我应该检查或者做些什么吗?

谢谢,

答案1

好的,找到了,我只需要添加用户,因为密钥是针对用户的,并且 systemd 以 root 身份运行

[Service]
User=myuser

相关内容