Use SSH Keys to Automatically Backup a Linux PC to a Synology NAS!

Today we are going to look at how you can use SSH keys and Cron to automatically back up a Linux PC to a Synology NAS.

The process will allow you to automatically authenticate from a Linux PC to a Synology NAS and schedule the job to run at the frequency of your choosing.

If you aren’t sure how to back up a Linux PC to a Synology NAS using Rsync, check out our tutorial!

Use SSH Keys to Automatically Backup a Linux PC to a Synology NAS - Instructions

1. First, we need to set up our Synology NAS so that it can use SSH keys. Make sure SSH is enabled.

SSHK.1

2. On your Linux PC, open terminal and create an SSH key by entering the command below. Use the default parameters by pressing “Enter” until the key is created.

ssh-keygen -t rsa

SSHK.6png 1

3. Copy the public key to our NAS.

ssh-copy-id [USERNAME]@[IP ADDRESS]

SSHK.7epng

4. After the key has been copied, SSH into your Synology NAS using the terminal from your Linux PC and enter your SSH password. To SSH into your Synology NAS, the user must be part of the Administrators group. If you don’t have SSH enabled, click here to view our tutorial on how to set it up.

ssh [USERNAME]@[IP ADDRESS]

SSHK.2

5. We now need to make three changes to our NAS so that our user can access the SSH key we created. If you’d like to see what permission changes we are making, you can find them here: 711, 600

chmod 600 ~/.ssh/authorized_keys
chmod 711 ~/.ssh
chmod 711 ~

SSHK.9png

6. After the permission changes have been made, “exit” the SSH session and try and authenticate again. You should not need a password.

exit
ssh [USERNAME]@[IP ADDRESS]

SSHK.10png

Automate the Rsync Backup Command with Cron

7. The final step is to create a cron job so that the Rsync command will execute automatically. We created our Rsync command in our last tutorial and will add the same command to automate the process. Edit the cron file.

crontab -e

SSHK.11

8. Cron is VERY powerful. There are many different ways to set up this command, but I have simplified it as best as I can. In my example, I backup my home folder to my Synology NAS at 2:00 AM every night. If you’re interested in customizing this command to run at different intervals, you can see a bunch of examples here.

0 2 * * * rsync -ax ~ [USERNAME]@[IP ADDRESS]:/[VOLUME]/Backups/[PC NAME]

SSHK.12

9. The process will now run automatically at 2 AM every night.

Conclusion

Using SSH keys and Cron to automatically backup a Linux PC to a Synology NAS is the best and easiest way that you can ensure your files are being backed up frequently. Rsync commands are very easy to write and execute, but automating them takes the process one step further and puts simplicity at the forefront. Thanks for reading! Leave any questions you might have in the comments!

This Post Has 10 Comments

  1. On step 7 between 2 and 3, I’m getting a
    “Could not chdir to home directory /var/services/homes/[USER]: No such file or directory
    sh: line 0: cd: /var/services/homes/[USER]: No such file or directory
    mkdir: cannot create directory ‘.ssh’: Permission denied”
    after I put in my password. Mine also says 1 key remaining to be installed. I’m assuming this is an issue on my the NAS side of things at the moment. Any suggestions?

    1. Do you know where the 2 and 3 are in step 7? I am trying to determine the issue location from the guide but can’t find step 7 between 2 and 3.

      If you open a new terminal window and try and run an Rsync command (outside of cron) does it allow you, and does it require a password? If it doesn’t require a password, the SSH keys are setup properly. If it does require a password, the issue is somewhere with the SSH keys.

      Let me know and we can continue troubleshooting!

      1. Sorry, I was unclear about the numbering, I’m not even close to the cron portion on this one – Your guide is numbered 1, 2, 7, 3, 4, 5, 6, 7, 8. So I was stuck in the 7 that is between 2 and 3 (ssh-copy-id). When I run Rsync on it’s own, it still requires my password for my Synology account. From the errors I get (below), it looks to me like the ssh-copy-id command is attempting to put the keys in a folder that doesn’t exist and it doesn’t have permissions to create that directory. But I’m using an account with admin level permissions so that doesn’t make sense… I’m lost. Here’s my error:
        “Could not chdir to home directory /var/services/homes/[USER]: No such file or directory
        sh: line 0: cd: /var/services/homes/[USER]: No such file or directory
        mkdir: cannot create directory ‘.ssh’: Permission denied”

        1. Totally my fault and thank you for pointing it out! I just fixed the numbering, so I apologize for the confusion.

          When you run the ssh-copy-id command, you’re running it FROM the device that you’re trying to back up, TO the Synology NAS, correct? Does that command succeed?

          The chmod commands need to be run on your Synology NAS. So basically, you’re copying the ID from your local device to your Synology NAS. Then, you need to SSH into your Synology NAS and run those commands. My guess is that you’re trying to run those commands on your local device rather than the Synology NAS, but if I’m wrong, let me know and we can continue troubleshooting!

          1. I have the same SSH problem as Domenick. your explanation above still does not make sense to me. Can you please add some instructions as to how to SSH into the NAS and run the comments as lines of command because otherwise I am totally lost. Thank you.

          2. What is the exact problem that you’re facing?

  2. Hey I just spend a good chunk of time solving an issue I had, and I figured I should leave a comment here just in case anyone runs into a similar problem.

    After following your tutorial I was able to ssh into my NAS without a password (i.e. the ssh key was working properly) but my rsync command would always prompt me for the password. Well it turns out that because I was using sudo in my rsync command it was being called as the root user, meaning that it was looking for keys belonging to root rather than my regular user. To fix this all I had to do was move ~/.ssh/id* files to /root/.ssh/. All credit goes to this guy:
    superuser.com/questions/810815/ssh-connection-with-key-not-working-with-rsync

    Also just wanted to say I’m a big fan of your content, WunderTech! I’m pretty new to the NAS world and your videos and articles have been incredibly helpful.

    1. This is great feedback for others, thanks so much for sharing!

      Really happy that the tutorials have helped you out – thanks a lot for watching/reading! If you ever need anything, let me know!

  3. when i run chmod 711 ~. I am getting chmod: /home/shares: Operation not permitted and may for this my automatic backup not work. Can you please guide me to resolve this issue.

    1. Can you try adding “sudo” to the front of the command?

Leave a Reply

Close Menu