How to use rsa password-free login in Linux

  

To log in to a Linux system, you need to use a password. Is there any way to log in to the system without using a password? The following small series will introduce you how to use rsa password-free login in ssh to log in to Linux, let's learn together.

A local host (i.e., for controlling other host machines);

(i.e. Server machine control) B is a remote host, if ip is 172.24.253.2;

Commands on A:

The code is as follows:

ssh-keygen -t rsa

(Three consecutive carriage returns, which are generated locally Public key and private key, no password is set, the generated public key private key is in the ~/.ssh directory, id_rsa is the private key, id_rsa.pub is the public key)

The code is as follows:

ssh "a href=“mailto:[email protected]”"[email protected]"/a"

mkdir .ssh

chmod 700 .ssh

(You need to enter a password, Note: You must set the permissions of .ssh to 700)

The code is as follows:

scp ~/.ssh/id_rsa.pub "a href=“mailto :[email protected]:~/.ssh”"[email protected]:~/.ssh "/a"

Then create the authorized_keys file on the server:

The code is as follows:

touch ~/.ssh/authorized_keys

Chmod 600 ~/.ssh/authorized_keys

(Note: The permissions of ~/.ssh/authorized_keys must be changed to 600. This file is used to save the public key generated by the ssh client. You can modify the ssh service of the server. End configuration file /etc/ssh/sshd_config to change the file name, the default is called authorized_keys)

The code is as follows:

cat id_rsa.pub 》 authorized_keys

# (will The content of id_rsa.pub is appended to authorized_keys, be careful not to use ", otherwise the original content will be emptied so that others cannot log in using the original key."

Back to A:

Code is as follows:

# ssh "a href=“mailto:[email protected]”"[email protected]"/a" (No password required, login successful)

From the surface, a brief understanding of the login process,

First, the ssh-keygen -t rsa command generates a key and a public key, and the key can set its own password

can understand the key as a key, the public key is understood as this The key corresponding to the lock,

put the lock (public key) on the server you want to control, lock the server, only the person with the key (key) can open the lock, enter the server and Control

And for the person who owns the key, you must know the password of the key itself to use the key (unless the key is not set), so that the key can be prevented. The key is copied.)

More concise way:

Step 1. If there is no authorized_keys file on the server: (If there is, skip this step)

Code As follows:

ssh "a href=“mailto:[email protected]”"[email protected]"/a"

mkdir .ssh

chmod 700 .ssh

touch ~/.ssh/authorized_keys

chmod 600 ~/.ssh/authorized_keys

Step 2, in the machine:

Code As follows:

ssh-keygen -t rsa

ssh-copy-id -i ~/.ssh/id_rsa.pub "a href=“mailto:[email protected] .253.2”"[email protected]"/a"

ssh-keygen -t rsa #Next three times, no password. Just fine.

Note: ssh-copy-id automatically appends the key to the remote host's .ssh/authorized_key

The above is the way to use rsa password-free login in ssh under Linux. Using this method, you can log in to the machine. Is it convenient? Try it now.

Copyright © Windows knowledge All Rights Reserved