3

I wrote the following command in /etc/rc.local:

ssh -R 32400:192.168.0.100:32400 root@marcin.com -N

but it won't start.

Any idea why?

Eric Carvalho
  • 54,385

1 Answers1

3

Maybe the network isn't ready yet, when rc.local is executed.

The following code will wait for the network before connecting:

(
until ping -nq -c3 W.X.Y.Z; do
   # Waiting for network
   sleep 5
done
ssh -R 32400:192.168.0.100:32400 root@marcin.com -N
)&

W.X.Y.Z is an IP address that is reachable and replies to pings. It maybe be the default gateway of your network, the Google DNS (8.8.8.8), etc.

Code between parenthesis runs in a new shell, and the & makes it run in background. It won't block the execution of rc.local.

The commands between until and done will be repeated until the ping is successful, i.e., it gets a reply (i.e., the network is up).

Eric Carvalho
  • 54,385
  • For info rc.local was placed in /etc for me. I also had to ensure it was executable (sudo chmod 775 /etc/rc.local) and place #!/bin/sh -e at the top: https://askubuntu.com/questions/9853/how-can-i-make-rc-local-run-on-startup/401090#401090 – SharpC Feb 08 '20 at 13:17