I'm trying to get a persistent SSH tunnel running on Ubuntu 22.04.
My command works fine from the command line, but fails when run via a systemctl service?
Here is my (redacted) 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
This fails with the following error from journalctl |grep ssh
<username>@<remotehost>: Permission denied (publickey,keyboard-interactive).
If I just run the command from my command line as like so...
/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>
everything works great!?
If I run the command in sudo or as root, like so...
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>
I get a prompted for a password (<username@remotehost>) Password:
despite the fact that both the username and the private key ought to be set within the command itself.
Thanks you for you help!
Bill