Recently, I received about ten emails from my NAS within a few days, warning me that multiple IPs were being blocked due to multiple failed SSH login attempts per IP. Concerned about security I decided to look into how to harden my setup. And so I went to see if I could use SSH public key authentication instead of password authentication when using SSH.
A quick search led me to: How do I sign in to DSM with RSA key pairs via SSH? – Synology Knowledge Center
It was a great starting point to get this working! While the guide explains how to set up the SSH keys, it has some gaps and is somewhat hard to read as it covers both the old DSM version and Windows native SSH client and PuTTY (or KiTTY), and more important it doesn’t it explain how to disable password authentication entirely. It simply shows you how to enable using SSH keys for login in alongside password authentication, which doesn’t improve security at all.
Here is my guide to use SSH keys instead of password authentication on a DSM 7.0+ with using PuTTY (or KiTTY) on Windows.
Setup DSM to use SSH
- Sign in to your NAS using an admin account
- Go to Control Panel > Terminal & SNMP > tick Enable SSH service
- I also suggest to change the default port to something else
- In Advanced settings change the encryption algorithm setting to High
(or customize the encryption mode if you know what you are doing) - Don’t forget to setup auto block, but I won’t go into details here
- Go to Control Panel > User & Grooups > Advanced > tick Enable user home service
Generate a RSA key pair
You can do this with PuTTY Key Generator but I opted to use the default windows tool and convert it to a .ppk file for use in PuTTY
- Open powershell (you can use terminal)
- Run the command
ssh-keygen -t rsa
- Enter the path where you want to save the RSA key, or just press enter to use the default path
- Enter a passphrase for the private key when prompted. I don’t recommend skipping this step.
Upload your RSA key to your NAS
- Go to File Station > home
- Create a sub-folder names .ssh
- Upload your id_rsa.pub file to the .ssh folder (this file can be found where you saved it before)
- Rename the file to authorized_keys
- You can add more keys, instead of renaming you have to append the file in the knowledge center they use
cat id_rsa.pub >> authorized_keys
this will append to the existing file if it exists
- You can add more keys, instead of renaming you have to append the file in the knowledge center they use
At this point your NAS is ready to allow login via RSA public keys
Convert private RSA Key to use with PuTTY
As mentioned before, for PuTTY you have to convert the private key file to a .ppk file
- Launch PuTTYGen
- Conversion > Import Key > Enter Passphrase
- Save private key
Login with PuTTY
- Launch PuTTY
- Session: accountname@ip
- Port: 22 (or the one you changed it too)
- Connection > SSH > Auth > Browse and select your .ppk file
- Open (you should now be prompted for your passphrase and be logged in)
Disable password authentication
- Edit
sudo vi /etc/ssh/sshd_config
(this should ask your password, for sudo this still works) - Change
PasswordAuthentication yes
toPasswordAuthentication no
- You can move to this line and press
i
to start insert / edit mode and useesc
to stop editing - You can also search for this line by typing
/PasswordAuth
(and enter)
- You can move to this line and press
- Save and quit by pressing
:wq
- Now you have to restart the SSH deamon (Easiest way to disable, and enable the SSH service in the web portal, don’t forget to click apply)
- If you now try to login using password again you should get a message like “Permission denied (publickey)”
In the end this was pretty easy to set this up, however I wished Synology integrated this in their GUI, allowing users to upload private keys, and let admins enable/disable Password Authentication for SSH; 2 small features that would make this a more secure feature.
I hope you found this useful and if I missed something or made an error, please let me know 🙂