Rsync is a real boon for local and remote file transfers.
But rsync operation often ends up in the “Permission denied (publickey)” error.
Usually, this error shows up when there are problems with rsync permissions.
At Bobcares, we often get requests to fix rsync errors, as a part of our Server Management Services.
Today, let’s discuss various reasons for Rsync permission denied error and see how our Support Engineers fix them.
What is Rsync error: permission denied (publickey)?
Rsync stands for remote sync. This a local and remote file synchronization tool.
For instance, rsync minimizes the amount of data transfer by copying portions of a file that have changed.
But in many situations, improper usage can show up errors. A typical rsync error message due to improper user query is as:
Permission denied (publickey).
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: unexplained error (code 255) at io.c(226) [sender=3.1.1]
The error code indicates that rsync cannot operate over an ssh connection due to a lack of permission.
Our customers often approach us with such error messages. In such situations, our Support Engineers check the customer query and fix the error.
How we fix the Rsync error?
The rsync error we discuss here is mostly a sender-side error.
Usually, this happens when the customer’s query contains improper rsync option or has syntax errors.
Our Dedicated Engineers with expertise over a decade checks the customer query and fixes the error. Today, let’s see a few such cases in detail.
1. Improper usage of rsync switch
Recently, one of our customers approached us with an rsync problem. He was using the rsync command as:
rsync -i "XXX.pem" -anv webapps ubuntu@xx.xx.xxx.xx:/home/ubuntu/
Unfortunately, it ended up in the Permission denied (publickey) error.
Our Support Engineers checked the query and we noticed an improper usage of -i in the query.
We checked if the user can ssh into the specified server. We also verified the permissions of the XXX.pem file. Finally, we corrected the query to,
rsync -e "ssh -i XXX.pem" -anv webapps ubuntu@xx.xx.xxx.xx:/home/ubuntu/
This fixed the error and the rsync operation was successful.
2. Wrong user privileges
Similarly, Rsync errors can also occur due to insufficient user privileges too. The customer was executing the following query.
sudo rsync -avz -e "ssh -p <port>" <source> <destination>
This ended up in the rsync permission denied error. Our Dedicated Engineers started debugging the reason for the error.
Finally, we found that the user that was running rsync did not have sudo privileges. But he executed the query as sudo. Thus the query ended up in the error. So, we changed the query to
rsync -avz -e "ssh -p <port>" <source> <destination>
This query could successfully run the operation. And this fixed the error.
3. Overwriting SSH files
In some situations, the rsync operations overwrite the existing SSH file in the destination server. When a user tries to ssh into this server or tries to rsync using ssh it ends up in error.
When our customers approach us with such an error, we check the SSH settings in the server. Our Dedicated Engineers reset back the ssh configuration to fix the error. We make sure to configure the sshd_config file properly. Additionally, we set PermitRootLogin to yes and PubkeyAuthentication to yes for easy remote file exchange.
[Need help in fixing Rsync permission denied error? – We will fix it for you.]
Conclusion
In short, the rsync query ends up in Permission denied publickey error due to improper usage of rsync options, wrong user privileges, overwriting of ssh files, and so on. Today, we saw how our Support Engineers fix these errors for our customers.
A common reason for this error is when public keys are used but then “sudo” is put before the rsync command. When you do that, the /root/.ssh keys are used and may be different from your user’s account keys (/home/username/.ssh) . Make sure the /root/.ssh folder has valid keys for the destination address if you use “sudo rsync user@xxx.xxx.xxx.xxx:/path”