Score:0

Trouble setting up persistent SSH tunnel via Systemd

om flag

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

hr flag
Does `/home/<username>/.ssh/id_rsa` have an empty passphrase, or are you not being asked to supply a passphrase because the key is cached by an ssh-agent in your user session?
Score:0
om flag

Thanks for your reply steeldriver.

I figured out what was going on.

I was ssh'ing into the computer I was trying to set the tunnel up on. The computer I was ssh'ing from had a valid key. That key was getting forwarded so that I was able to run the command from the command line (via ssh) successfully, but when I tried to run the command via systemd (not through my ssh session) there was no valid key available locally.

Thanks for your help.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.