Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set ssh key on Linux to avoid password login

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

How to set the ssh key on Linux to avoid password login? This article uses an example to show, the steps are very detailed, which can be used as a reference for friends in need. I hope it will be helpful to you.

Ssh (secure shell) is widely used for remote login to Linux servers. When we log in to the remote system using ssh, it prompts for a password and then only allows us to log in to the server. Sometimes we need to configure applications or scripts (mainly shell scripts) to automate the tasks to be performed after ssh is performed on the remote system. However, if we do not configure a key-based ssh, the script will prompt for a password each time we run, at which point we need to enter the password manually. To solve this problem, we can choose to use the public key / private key concept. The remote server allows other systems to ssh based on the key.

Step 1: generate ssh key pair

First, you need to generate a key pair (rsa or dsa), and you can use the "- t" command line switch to specify the option rsa or dsa key. If you do not pass the-t argument, it creates a RSA key by default.

$ssh-keygen-t rsaGenerating public/private rsa key pair.Enter file in which to save the key (/ home/rahul/.ssh/id_rsa): Created directory'/ home/rahul/.ssh'.Enter passphrase (empty for no passphrase): Enter same passphrase again:Your identification has been saved in / home/rahul/.ssh/id_rsa.Your public key has been saved in / home/rahul/.ssh/id_rsa.pub.The key fingerprint is:SHA256:GZQ3tJffEUimdMZHIG3LcpvdkOaogwXBtWeaM2ejzYY rahul@tecadminThe key's randomart image is:+ -[RSA 2048]-+ |.. + oo+*+o | |. + oob *.. | | .ooB oo | | .ooB = +.. | | S.X. + | | oE.+ |. O. | |. | | +-[SHA256]-+ |

The above command will create two files in the ~ / .ssh directory, as shown below.

1. ~ / .ssh / id_rsa [private key]

2. ~ / .ssh / id_rsa.pub [public key]

Step 2: copy the public key to the remote system

Let's copy the public key of the system to the key file of the remote system ~ / .ssh / authorized_key. We can do this manually or using the ssh-copy-id command line tool.

$ssh-copy-id-I ~ / .ssh/id_rsa.pub 192.168.10.20

Output:

21root@192.168.10.20's password:Now try logging into the machine, with "ssh '192.168.10.20", and check in: .ssh / authorized_keysto make sure we haven't added extra keys that you weren't expecting.

It will prompt for the password of the remote system. Enter the remote machine password and press Enter.

Step 3: verify the SSH without a password

Now that we've done all the work, we just need to try ssh the remote system. You will log in to the remote system without entering a password.

$ssh root@192.168.10.20

The above command does not prompt for a login password. In any case, if the ssh command prompts for a password, it means that your settings are not configured correctly. Please retry all steps.

After reading the above, have you mastered the method of setting the ssh key? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report