Setting up SSH (or SCP/SFTP) to remember your password
Linux No Comments »If you are tired of inputting your password each time for a SSH or SCP connection or if you're setting up a cronjob to transfer files, this will guide you through the process:
The LOCAL machine is the machine that inits the connection for SSH / SCP.
The REMOTE machine is the machine that accepts the connection.
The following command generates a private and a public key. The private key is for your eyes only,
the public key goes to the remote machine.
LOCAL $ ssh-keygen -t rsa -N ""
Now execute:
LOCAL $ scp ~/.ssh/id_rsa.pub USER@REMOTE:.ssh/
This will copy the public key to the relative .ssh/ dir in your remote user's homedir.
(And ofcourse you have to substitute the needed fields.)
Now login to you remote server with SSH. I assume you can handle this step yourself.
Then execute:
REMOTE $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
That will add the id_rsa.pub file to the authorized keys-file on the remote machine.
You can safely remove the id_rsa.pub file now if you wish
REMOTE $ rm ~/.ssh/id_rsa.pub
TROUBLESHOOTING
Well, in fact, it's not always that easy. If you've completed these steps, and the remote server still keeps asking for a password, you might want to pay attention to the following:
In some cases (and it must've been annoying me for at least two hours now,) people forget to chmod the authorized_keys file or .ssh directory.
That can be easily fixed with this command:
REMOTE $ chmod -R 700 ~/.ssh
In my case, my homedir was chmodded to 707 (weird uh..). However, the SSH server didn't seem to like that and therefor I changed it back to 755.
REMOTE $ chmod 755 ~
Depending on your SSH version (or if compatibility mode is not enabled in the SSH server settings), you might have to use authorized_keys2 instead of authorized_keys. Newer SSH-server versions will look in both of these files, I've been told.
My last remarks: if you're trying to login as root, you have to explicitly enable this in the SSH server on the remote machine. Open etc/ssh/sshd_config and change PermitRootLogin to yes. Some users using empty passwords will also have to set PermitEmptyPasswords to yes.