You are not logged in.

#1 2018-01-30 22:26:55

pichlo
Member
Registered: 2018-01-30
Posts: 5

How to start autossh and dhcpcd via systemd in right order

Hi,

I need some help, how to start autossh via systemd. Manually started my autossh systemd service is running without problems. But started via systemd while booting, autossh tries to start before dhcp is ready. As result the start of autossh failed, because the network is not ready.

What I did
1. activate autostart for dhcp long time ago: systemctl enable dhcpcd@eth0 (source: https://wiki.archlinux.de/title/DHCP)
2. install autossh: pacman -S autossh
3. create systemd service (source https://blog.philippklaus.de/2013/03/st … rch-linux)

[Unit]
Description=AutoSSH service for a reverse tunnel from some.example.com to localhost
After=network.target

[Service]
ExecStart=/usr/bin/autossh -M 0 -q -N -o "ServerAliveInterval 60" -o "ServerAliveCountMax 3" someuser@some.example.com -R 1250:localhost:8080

[Install]
WantedBy=multi-user.target

4. test systemd service: systemctl daemon-reload; systemctl restart autossh.service; systemctl status autossh.service
5. reboot system
6. check systemd log: journalctl -b

Jan 30 22:58:40 tux sshd[417]: Server listening on 0.0.0.0 port 22.
Jan 30 22:58:40 tux sshd[417]: Server listening on :: port 22.
Jan 30 22:58:40 tux autossh[419]: port set to 0, monitoring disabled
Jan 30 22:58:40 tux autossh[419]: starting ssh (count 1)
Jan 30 22:58:40 tux autossh[419]: ssh child pid is 422
(...)
Jan 30 22:58:40 tux autossh[419]: ssh exited prematurely with status 255; autossh exiting
(...)
Jan 30 22:58:44 tux dhcpcd[415]: eno1: leased 192.168.0.4 for 3600 seconds
Jan 30 22:58:44 tux dhcpcd[415]: eno1: adding route to 192.168.0.0/24
Jan 30 22:58:44 tux dhcpcd[415]: eno1: adding default route via 192.168.0.1

What I allready tried:
- change systemd service line "After=network.target" to "After=network-online.target" without success
- add systemd service line "After=dhcpcd@eno1"

Who can help? How could I manage it, that autossh is starting after dhcp?

Thanks
Martin

Offline

#2 2018-01-30 23:27:40

loqs
Member
Registered: 2014-03-06
Posts: 19,113

Re: How to start autossh and dhcpcd via systemd in right order

NetworkTarget network-online.target would work if the dhcpcd service supported it.
For the same reason After=dhcpcd@eno1 fails as the service has started successfully so the condition is met but no address has yet been obtained.
Edit:
Oh and welcome to the arch linux forums pichlo.

Last edited by loqs (2018-01-30 23:28:25)

Offline

#3 2018-01-31 07:37:03

pichlo
Member
Registered: 2018-01-30
Posts: 5

Re: How to start autossh and dhcpcd via systemd in right order

Hi loqs

I changed the "after" statement as follows:

After=network.target network-online.target

Unfortunately there is no change while booting

Jan 31 08:30:34 tux autossh[399]: port set to 0, monitoring disabled
Jan 31 08:30:34 tux autossh[399]: starting ssh (count 1)
Jan 31 08:30:34 tux autossh[399]: ssh child pid is 403
Jan 31 08:30:34 tux systemd[1]: Started Permit User Sessions.
Jan 31 08:30:34 tux systemd[1]: Started Getty on tty1.
Jan 31 08:30:34 tux sshd[402]: Server listening on 0.0.0.0 port 22.
Jan 31 08:30:34 tux systemd[1]: Reached target Login Prompts.
Jan 31 08:30:34 tux sshd[402]: Server listening on :: port 22.
(...)
Jan 31 08:30:34 tux autossh[399]: ssh exited prematurely with status 255; autossh exiting
(...)
Jan 31 08:30:38 tux dhcpcd[397]: eno1: leased 192.168.0.4 for 3600 seconds
Jan 31 08:30:38 tux dhcpcd[397]: eno1: adding route to 192.168.0.0/24
Jan 31 08:30:38 tux dhcpcd[397]: eno1: adding default route via 192.168.0.1
Jan 31 08:30:45 tux sshd[602]: Accepted publickey for root from 192.168.0.61 port 49429 ssh2: RSA SHA256:DhnDdi++eESNIXmii+Hd0iqA7mj7nuVbihaAmDWSZsw

Do you have more ideas? How could I find out if dhcpcd supports the mentioned targets? Should I may change the dhcp service?

Thanks
Martin

Offline

#4 2018-01-31 09:46:48

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 78,863

Re: How to start autossh and dhcpcd via systemd in right order

loqs wrote:

network-online.target would work if the dhcpcd service supported it

"subjunctive" - it doesn't.
You'll have to add some network helper, like

while ! ping -c1 some.example.com > /dev/null 2>&1; do sleep 1; done

or some such.

Offline

#5 2018-01-31 10:24:10

pichlo
Member
Registered: 2018-01-30
Posts: 5

Re: How to start autossh and dhcpcd via systemd in right order

This would mean that "network-online.target" is not working on my system in general, right?
Is this a known problem with dhcpcd or do I need to check my system setup?
Is there any other service available for starting dhcp at boottime, which support "network-online.target"?

PS Thanks for the workarround. I will try if it works.

Last edited by pichlo (2018-01-31 10:24:24)

Offline

#6 2018-01-31 11:40:48

Omar007
Member
Registered: 2015-04-09
Posts: 368

Re: How to start autossh and dhcpcd via systemd in right order

You can also try to make network.target activate only after dhcpcd (NetworkManager and systemd-netword already support this using a service file)
This should give you some information on how that works: https://www.freedesktop.org/wiki/Softwa … orkTarget/

EDIT: such a service is already part of dhcpcd-ui apparently: https://github.com/rsmarples/dhcpcd-ui/ … service.in
You could probably just grab that one.

EDIT2: Or, along the lines of seth's suggestion for a network helper, use ExecStartPre to delay until ping succeeds:

ExecStartPre=/bin/sh -c 'until ping -c1 example.com; do sleep 1; done;'

Last edited by Omar007 (2018-01-31 12:06:07)

Offline

#7 2018-01-31 12:06:46

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,543
Website

Re: How to start autossh and dhcpcd via systemd in right order

I'd not bother with all of the above acrobatics; just add `Restart=on-failure` and `RestartSec=5` to the autossh service file.

Why run ping until ping connects and then run autossh.  Just run autossh until autossh connects.

Last edited by Trilby (2018-01-31 12:08:37)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#8 2018-01-31 13:39:52

pichlo
Member
Registered: 2018-01-30
Posts: 5

Re: How to start autossh and dhcpcd via systemd in right order

Thanks for all the responses. I will test it as soon as possible.

Meanwhile a DHCP question regarding this article: https://www.freedesktop.org/wiki/Softwa … orkTarget/

If you use NetworkManager you can do this by enabling NetworkManager-wait-online.service:
# systemctl enable NetworkManager-wait-online.service

If you use systemd-networkd you can do this by enabling systemd-networkd-wait-online.service:
# systemctl enable systemd-networkd-wait-online.service

Which case I am using, when my dhcp is starting as follows?

systemctl enable dhcpcd@en0

Do you have some articles regarding both cases?

Thanks
Martin

Offline

#9 2018-01-31 13:47:54

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: How to start autossh and dhcpcd via systemd in right order

You're using dhcpcd directly, so neither of them. Personally I'd switch to using systemd-networkd, but that's just because it's my preferred solution that I use on all of my machines.


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#10 2018-02-03 19:28:43

pichlo
Member
Registered: 2018-01-30
Posts: 5

Re: How to start autossh and dhcpcd via systemd in right order

Thanks for all of your responses. I finally switched the dhcp management to systemd-networkd, now network-online.target works.

Offline

#11 2018-02-03 19:31:42

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: How to start autossh and dhcpcd via systemd in right order

Glad you got it sorted. Please remember to mark this thread [SOLVED].
CoC - How to post


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

Board footer

Powered by FluxBB