Create private / public keys for passwordless rsync over ssh

First we start off by making a public key on the local machine

ssh-keygen -t rsa

ssh-keygen will ask you first where to store the public key. The default location is usually fine, that is unless you want to manage multiple public keys. Unless multiple public keys is what you need, simply hit enter to accept the default location. Next you will be asked for a password. You must hit enter twice without typing in a password or else every time you use this public key you will have to enter that password instead. This should result in the creation of the key and the public key pair. If you chose the default values, ~/.ssh/id_rsa and ~/.ssh/

Now, there are several ways to do this, ftp, ssh, vi, it doesn’t matter which method you choose but you need to get the information from the public key file that you just created to your authorized_keys file on the machine you want to ssh into without a password. The authorized_key file on the remote server must be owned by the user who’s account you are going to be logging in as and must be set with permissions 600. If you’ve already set up any keys previously then you can simply copy the new public key as a new line in the existing file.
If you haven’t set up a key previously on the remote host then you can use ssh to copy the file from the machine that we just created the keys on

ssh-copy-id -i ~/.ssh/ username@remote_host

In the above example, username and remote_host should be replaced by their respective values. After executing this, it will ask you for your password, this is just the ssh password to the remote machine for the username that you used. Upon completion, there should be a file on the remote machine ~/.ssh/authorized_keys that contains the public key that you just generated.

You can test out if you were successful now by ssh’ing to the remote machine, you should no longer be asked for a password. If it works remember to type exit when your done to return to your current machine’s terminal!

ssh user@remotehost

You can now use rsync to send data securely over ssh without the need for a password.

This means that you can also schedule the backup command using cron.

Print Friendly, PDF & Email

More Like This


CentOS Linux Security Web Hosting
  • Post a comment