3

I created following service:

[Unit]
Description=Server Tunneling
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=ssh -R server-login:80:localhost:8003 serveo.net
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target


Then, I ran sudo systemctl daemon-reload followed by sudo systemctl start server-tunnels. However, when I run sudo systemctl status server-tunnels, it says the service failed:

server-tunnels.service - Server Tunneling
   Loaded: loaded (/lib/systemd/system/server-tunnels.service; enabled; vendor preset: enabled)
   Active: activating (auto-restart) (Result: exit-code) since Thu 2019-07-25 17:43:40 CEST; 2s ago
  Process: 17139 ExecStart=/usr/bin/ssh -R server-login:80:localhost:8003 serveo.net (code=exited, status=255/EXCEPTION)
 Main PID: 17139 (code=exited, status=255/EXCEPTION)

Of course, running ssh -R server-login:80:localhost:8003 serveo.net in a normal terminal works.


Changes I made according to @kuzeyron:

[Unit]
Description=Server Tunneling
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=
ExecStart=ssh -R server-login:80:localhost:8003 serveo.net
Restart=always
RestartSec=30

[Install]
WantedBy=multi-user.target

Now running systemctl status server-tunnels says it failed due to bad setting.


I'm very new to services and the Raspberry PI, so maybe I missed something obvious.
Thanks for your help!

2
  • Try adding ExecStart= before the ExecStart you have in the service file. And perhaps change the Type=simple to Type=oneshot. Honestly I cannot remember why to use oneshot.
    – kuzeyron
    Jul 25, 2019 at 20:30
  • Please dont't give the solution in the question. This does not finish the question. Instead you should write an answer. Just move the solution from the question into an answer.
    – Ingo
    Jul 26, 2019 at 15:54

2 Answers 2

2

First, you should place your unit file in /etc/systemd/system/server-tunnels.service, not /lib/systemd/system/server-tunnels.service. The directory under /etc is intended for the local administrator while the directory under /lib is intended for the distribution.

The reason why the command doesn't work it that it doesn't know how to log in. Commands started as a service don't have access to the terminal, so ssh can't ask for a password. They also have a limited environment, so ssh doesn't know how to connect to your ssh-agent if you have it running. It can access your unencrypted SSH keys in the standard locations, but you don't seem to have them. f you have a key in a non-standard location, you can use option -i to specify the file. If your key is encrypted you again have the problem that ssh can't ask for the decryption password.

So if you want this to work, you need an unencrypted SSH key to log in to the server.

3
  • This ssh server doesn't require login
    – Tobi696
    Jul 25, 2019 at 17:40
  • What does that mean? No user name? No password?
    – RalfFriedl
    Jul 26, 2019 at 16:33
  • Just run the comand
    – Tobi696
    Jul 26, 2019 at 17:28
2

How I solved it: I installed the npm module localtunnel which allows me to tunnel my localhost to network without ssh. The new service looks like this:

[Unit]
Description=Server Tunneling
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
ExecStart=lt --port 8003 --subdomain server-login
Restart=always

[Install]
WantedBy=multi-user.target

Thanks to everyone who tried to help me!

1
  • You do not need Type=simple. That's default. Your program will go to the background as as service. So why to use Restart=always? Do you suspect that your started program is buggy and fails to run? Then to restart it always is a very bad idea.
    – Ingo
    Jul 26, 2019 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.