SSH on boot via rc.local

I am trying to ssh into a server on boot. I have written a simple bash script to perform this action. The script works fine if I run it from the command line, issue is with rc.local. The script /home/RPi_1/ is below:

sleep 20

while true; do ssh [email protected] \
       -R 22:localhost:22 -N -o ServerAliveInterval=10; \
       sleep 5; done


/home/RPi_1/ > /home/RPi_1/autossh.log 2>&1 &
exit 0 

The autossh.log file is filled with this error: Host key verification failed.

I have two users on the my Raspberry Pi, pi and RPi_1. I think I have identified the issue being that when rc.local is being executed it is executed as root or pi? Thus, when the key is being searched for it is looking in the wrong directory? The key for the server is in /home/RPi_1/.ssh/

Similar issue at:

3B running Raspbian GNU/Linux 8 (jessie)

Answers 2

  • To further expand on the question and the replies:

    If you need to automate a startup script indeed rc.local is not the right way to do it. Not even a cron. A systemd service is preferable and you can set up dependencies. In your case the script should not start until networking is ready. Waiting for an arbitrary number of seconds is wasteful and does not guarantee that the network will be ready. Make the service run as your user RPi_1 (or pi ?): example.

    I would also suggest to use autossh. If the connection is interrupted for some reason, then your script will reconnect automatically. You while loop does more or less the same but there is a tool for that.

  • rc.local is run as root.

    This is only the start of your problems.

    You should NOT use rc.local on systemd - there must be hundreds of posts on this site discussing the issue. I will not waste time on the problems with the script you are trying to run.

    To make matters worse you are using an obsolete OS, which makes it even harder for anyone to help.

Related Questions