通过 Systemd 设置持久 SSH 隧道时出现问题

通过 Systemd 设置持久 SSH 隧道时出现问题

我正在尝试在 Ubuntu 22.04 上运行持久 SSH 隧道。

我的命令在命令行中运行良好,但通过 systemctl 服务运行时失败?

这是我的(已编辑的)rtunnel.service...

[Unit]
Description=Remote ssh tunnel
Wants=network-online.target
After=network-online.target 

[Service]
User=<username>
Group=<username>
Type=simple
ExecStart=/usr/bin/ssh -gnNT -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o PasswordAuthentication=no -o PubkeyAuthentication=yes -i /home/<username>/.ssh/id_rsa -R <remote_port>:localhost:<local_port> username@<remotehost>
RestartSec=5
Restart=always

[Install]
WantedBy = multi-user.target

失败并出现以下错误journalctl |grep ssh

 <username>@<remotehost>: Permission denied (publickey,keyboard-interactive).

如果我只是从命令行运行命令,就像这样......

/usr/bin/ssh -gnNT -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o PasswordAuthentication=no -o PubkeyAuthentication=yes -i /home/<username>/.ssh/id_rsa -R <remote_port>:localhost:<local_port> username@<remotehost>

一切都很顺利!?

如果我以 sudo 或 root 身份运行该命令,如下所示...

sudo /usr/bin/ssh -gnNT -o ExitOnForwardFailure=yes -o ServerAliveInterval=30 -o PasswordAuthentication=no -o PubkeyAuthentication=yes -i /home/<username>/.ssh/id_rsa -R <remote_port>:localhost:<local_port> username@<remotehost>

(<username@remotehost>) Password:尽管用户名和私钥都应该在命令本身中设置,但我还是收到输入密码的提示。

谢谢你的帮助!

账单

答案1

谢谢steeldriver的回复。

我知道发生了什么事。

我正在通过 ssh 连接我试图设置隧道的计算机。我通过 ssh 连接的计算机有一个有效密钥。该密钥正在被转发,以便我能够从命令行(通过 ssh)成功运行命令,但当我尝试通过 systemd(而不是通过我的 ssh 会话)运行命令时,本地没有可用的有效密钥。

感谢您的帮助。

相关内容