How to Specify SSH Password in rsync Command

When using rsync over SSH for remote file transfers, you can avoid typing your password each time by setting up RSA key authentication. If this isn’t possible in your situation, the next best thing is to pass the SSH password in the rsync command. We’ll look at how to do that securely in this guide.

Installing sshpass

There’s no native way to supply an SSH password in an rsync command. However, you can install sshpass very easily from any system’s package manager. This will allow us to put the SSH password in our rsync command. Use the appropriate command below to install it.

Ubuntu, Debian, and Linux Mint:

$ sudo apt install sshpass

Fedora, AlmaLinux, CentOS, and RHEL:

$ sudo dnf install sshpass

Arch Linux and Manjaro:

$ sudo pacman -S sshpass

Specify SSH password in rsync command

Here’s how you would use sshpass in your rsync command. The quotes around your password aren’t strictly necessary, unless your password contains special characters.

$ sshpass -p 'password' rsync -ave ssh /src/ user@hostname:/dst/

But wait, this isn’t very secure. Anyone looking over your shoulder or browsing the system’s command history can see your SSH password. To avoid this, create a text file that contains your password, and put proper permissions on it so that only your user can view its contents.

$ echo "password" > ~/.sshpass.txt
$ chmod 400 ~/.sshpass.txt

Now we can reference this file from our rsync command whenever we want to specify the password.

$ sshpass -p $(cat ~/.sshpass.txt) rsync -ave ssh /src/ user@hostname:/dst/

With this method, your password won’t appear in the system’s command history, and you don’t have to be wary of anyone watching your screen.

2 thoughts on “How to Specify SSH Password in rsync Command”

  1. quick question how about if you want to use this method and also run rsync as sudo on the destination host while also passing the password to sudo and avoid any prompts

Leave a Comment

Your email address will not be published. Required fields are marked *