Sometimes to run a cron backup directory which usually uses the “rsync” tool via SSH, we need login access to the remote host without a password. Therefore we need a way to remote SSH without password for login host server.
OK, lets go ahead.
Create Public and Private Keys Using ssh-key-gen
At first, we need create public and private keys using ssh-key-gen on our localhost.
habibza@habibza-local:~$[execute this command on your localhost]
habibza@habibza-local:~$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/habibza/.ssh/id_rsa):[press ENTER]
Enter passphrase (empty for no passphrase):[press ENTER]
Enter same passphrase again:[press ENTER]
Your identification has been saved in /home/habibza/.ssh/id_rsa
Your public key has been saved in /home/habibza/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:39HGVqXeX0WUIUOrGiWI/tt5B76wni4tYrnsmfm19Ko habibza@habibza-lnv
Short explain about command above.
ssh-keygen
tool creates the public and private keys.-t
: Type
Copy the Public Key to Remote Host using ssh-copy-id
Second, copy public key from localhost to remote-host using ssh-copy-id.
habibza@habibza-local:~$ssh-copy-id -i ~/.ssh/id_rsa.pub REMOTE-HOST
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password:[ENTER password here]
Number of key(s) added: 1
ssh-copy-id
copies the localhost public key to the destination remote-host’s authorized_keys file.ssh-copy-id
also assigns proper permission to the remote-host’s home, ~/.ssh, and ~/.ssh/authorized_keys.
If your remote host using custom port SSH, use this command.
habibza@habibza-local:~$ ssh-copy-id -i ~/.ssh/id_rsa.pub "-p 2727" REMOTE-HOST
-p 2727
: Your custom SSH port (in this case port ssh remote host is using port 2727)
Test Login to Remote Host without Input the Password
And now, you can test login to remote host without input password.
habibza@habibza-local:~$ssh remote-host
Last login: Fri Aug 20 13:00:00 2021 from 192.168.1.60
[Note: SSH did not ask for password.]
habibza@remote-host$ [Note: You are on remote-host here]
If using custom port SSH, please remote like below.
habibza@habibza-local:~$ssh -p 2727 REMOTE-HOST
That is simply how to connect remote host with ssh without password. Might be it’s helpful, please feel free to leave a comment if you have any questions and I’ll appreciate it.