• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Setup SSH keys for SSH “public/private key” Login On Linux

by admin

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:

[test1@server4 ~]$ 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:
 test1@server4.oracle.com
The key's randomart image is:
+--[ RSA 2048]----+
| .o... oo.|
| . . .Eooo|
| o . oo+.|
| . + ...+.|
| S o *|
| *o|
| =|
| .|
| |
+-----------------+
[test1@server4 ~]$

Note:
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.

-t type – Specifies the type of key to create. The possible values are “rsa1” for protocol version 1 and “dsa”, “ecdsa” or “rsa” for protocol version 2. New Keys will be located on /home/test1/.ssh.

3. Move to you .ssh directory were the key was created and list the files in the directory:

[test1@server4 ~]$ cd .ssh
[test1@server4 .ssh]$ ls
id_rsa id_rsa.pub

4. Copy the public key to the target server (server3):

[test1@server4 .ssh]$ ssh-copy-id -i id_rsa.pub test1@server3
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.

test1@server3's password:

5. Now try logging into the machine, with “ssh ‘test1@server3′”, and check in:

.ssh/authorized_keys

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.

[test1@server4 .ssh]$ ssh test1@server3
[test1@server3 ~]$

Note: if your server has not installed openssh-clients package another alternative will be:

$ cat id_rsa.pub | ssh user@server3 "cat >> ~/.ssh/authorized_keys"

Filed Under: Linux

Some more articles you might also be interested in …

  1. test: command not found
  2. CentOS / RHEL : How to Install and Configure OpenSSH Server and Client
  3. which: command not found
  4. How to configure Linux Resource Groups (cgroups) for MySQL
  5. ctrlaltdel Command Examples in Linux
  6. CentOS / RHEL : How to find the creation time of LVM volume
  7. locale Command Examples in Linux
  8. dumpe2fs Command Examples in Linux
  9. i3lock: command not found
  10. namei Command Examples in Linux

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright