How to Setup SSH keys for SSH “public/private key” Login On Linux – Tec Geeks
1. Login as the user that you want to setup the ssh keys, in this case we are using user test1.
2. Create a private and public key:
[[email protected] ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/test1/.ssh/id_rsa): Created directory '/home/test1/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/test1/.ssh/id_rsa. Your public key has been saved in /home/test1/.ssh/id_rsa.pub. The key fingerprint is:
[email protected] The key's randomart image is: +--[ RSA 2048]----+ | .o... oo.| | . . .Eooo| | o . oo+.| | . + ...+.| | S o *| | *o| | =| | .| | | +-----------------+ [[email protected] ~]$
You can specify an option on the ssh-keygen like the size and the type. More information on the man ssh-keygen
-b bits – Specifies the number of bits in the key to create. For RSA keys, the minimum size is 768 bits and the default is 2048 bits. Generally, 2048 bits is considered sufficient. DSA keys must be exactly 1024 bits as specified by FIPS186-2.
3. Move to you .ssh directory were the key was created and list the files in the directory:
4. Copy the public key to the target server (server3):
[[email protected] .ssh]$ ssh-copy-id -i id_rsa.pub [email protected] The authenticity of host 'server3 ([ip])' can't be established. RSA key fingerprint is [RSA_KEY]. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'server3,[ip]' (RSA) to the list of known hosts. [email protected]'s password:
5. Now try logging into the machine, with “ssh ‘[email protected]′”, and check in:
to make sure we haven’t added extra keys that you weren’t expecting.
6. Now test your key, you should login directly yo target server.
Note: if your server has not installed openssh-clients package another alternative will be:
$ cat id_rsa.pub | ssh [email protected] "cat >> ~/.ssh/authorized_keys"