Home/Server & Website Managed Operations/Logging into Linux Servers via SSH Key Authentication
Server & Website Managed Operations

Logging into Linux Servers via SSH Key Authentication

In addition to password authentication, SSH offers another commonly used method: key-based authentication. Its advantages include enhanced security and the ability to enable passwordless login between Linux systems, facilitating management or batch maintenance.

This article explains how to configure key-based passwordless login for Linux servers on both Windows and Linux.

01 [root@A ~]# ssh-keygen -t rsa
02
03 Generating public/private rsa key pair.
04
05 Enter file in which to save the key (/root/.ssh/id_rsa):
06 // Path and filename where the key will be saved
07
08 Enter passphrase (empty for no passphrase):
09
10 // Passphrase for the key; leave empty for passwordless login
11
12 Enter same passphrase again:
13
14 Your identification has been saved in /root/.ssh/id_rsa
15
16 // Private key generated at /root/.ssh/id_rsa
17
18 Your public key has been saved in /root/.ssh/id_rsa.pub
19
20 // Public key generated at /root/.ssh/id_rsa.pub
21
22 The key fingerprint is:
23
24 82:8c:2c:52:06:c6:f3:c4:1c:1c:35:b7:cd:5b:42:a2 root@A
25
26 [root@A ~]# ls /root/.ssh/ // Check the directory; these two files should now exist
27
28 authorized_keys id_rsa id_rsa.pub known_hosts
29
30 [root@A ~]#

Check the path where the server’s SSH service stores public keys:

1 [root@A ~]# cat /etc/ssh/sshd_config |grep AuthorizedKeysFile
2
3 #AuthorizedKeysFile .ssh/authorized_keys

The default location for storing public keys on the server is .ssh/authorized_keys.

Therefore, you need to copy the generated public key file to this file on the target server using scp or any other method.

01 [root@B ~]# cd .ssh/
02
03 [root@B .ssh]# cat id_rsa.pub >authorized_keys
04
05 // The default private key path read by the local SSH client includes /.ssh/id_rsa
06
07 [root@A .ssh]# cat /etc/ssh/ssh_config |grep IdentityFile
08
09 # IdentityFile ~/.ssh/identity
10
11 # IdentityFile ~/.ssh/id_rsa
12
13 # IdentityFile ~/.ssh/id_dsa

At this point, you can directly execute ssh ipB from Server A to log in to Server B. It’s that simple.

A simpler method is to use the ssh-copy-id command to directly add your local key to the verification file on the remote server:

01 [root@A .ssh]# ssh-copy-id
02
03 Usage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine
04
05 [root@A .ssh]# ssh-copy-id -i /root/.ssh/id_rsa root@ipB
06
07 The authenticity of host 'ipB (ipB)' can't be established.
08
09 RSA key fingerprint is 17:21:32:02:da:3b:a1:d1:a2:69:12:36:a2:d7:59:ad.
10
11 Are you sure you want to continue connecting (yes/no)? yes
12
13 root@ipB's password:
14
15 Now try logging into the machine, with "ssh 'root@ipB'", and check in:
16
17 .ssh/authorized_keys
18
19 [root@A ~]# ssh ipB
20
21 Last login: Sun Dec 15 19:33:07 2013 from ipA
22
23 [root@B ~]#

After executing this, you can log in to the server directly.

The above methods involve connecting via the Linux SSH client. If you want to use key-based login from a local machine, the process is similar.

Download the private key (id_rsa) to your local machine and import it into the key authentication configuration of your client.

For example, in SecureCRT, select the downloaded private key file as shown in the configuration below.

131

FREE CHECKUP

Free server checkup

We will send a problem checklist and remediation advice within one business day — no obligation to engage.

135 410 16684
Weekdays Mon – Fri 10:00 – 22:00 (UTC+8) · 7×24 emergency response
Call now
CONTACT

Contact Us

How can we help?

* Required