Table of contents
Step 1: Generate public key (Public key) and private key (Private Key)
Method 2: Using the command line
Step 2: Put the public key (Public Key) to the server
Method 1: (I am using Alibaba Cloud server, so I log in directly to the console)
Method 2: Import the public key directly on the linux server
Step 3: Configure the ssh client to use the key to log in
Introduction
ssh login provides two authentication methods : password (password) authentication method and key authentication method. Among them, the password (password) authentication method is the most commonly used one. Here we introduce the method of logging in to Linux using the key authentication method.
1. Generate a key (public key and private key );
2. Put the public key (Public Key) into the server ~/.ssh/authorized_key file;
3. Configure the ssh client to use the key to log in.Extension: the public key is used to place it on the server for authorization verification, and the private key is the token for the client to access the server
hint
If you already have a private key file with access to the server, you can directly perform steps 3 and 4 to log in
Method steps
Step 1: Generate public key (Public key) and private key (Private Key)
Method 1: Use the xshell tool
Copy the contents of the public key
Method 2: Using the command line
Execute the following command in the cmd terminal, if he prompts, just press Enter
ssh-keygen
Location of public and private keys
Step 2: Put the public key (Public Key) to the server
Method 1: ( I am using Alibaba Cloud server, so I log in directly to the console )
Choose to import an existing key pair, fill in the name of the key pair, paste in the content of the public key you just copied, and confirm
Method 2: Import the public key directly on the linux server
Log in to the server, enter the "/root/.ssh/" directory, run the rz command (if there is no rz command, run yum install lrzsz to install), send key.pub to the server, and then run the following command to copy the public key (Public Key) into the "authorized_keys" file:
[root@localhost ~]# cd /root/.ssh/
[root@localhost .ssh]# rz
rz waiting to receive.
[root@localhost .ssh]# cat me.pub >> authorized_keys
[root@localhost .ssh]# chmod 600 authorized_keys
[root@localhost .ssh]# ls
authorized_keys known_hosts me.pub
[root@localhost .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA4VCOWFrARc1m3MfEAL50v2Z2siavO3Ijpr/LZ00EZah8EjfZhqjAc/agkljyXmNGpVDpRdtlYco8h3P5vegXOEgKcX74fDYm0vNdVABVD1XSD8ElNyLTCCNk7rZJbi3htJox3Q1n0vnMmB5d20d9occkAx4Ac94RWNS33EC5CszNTMgAIn+uZl0FlQklS1oSyWFahSTWyA6b33qG7Y5E4b6J/caObnPx6EgtBrgi97gXJHZWyYlGrpWmUuhPqs5XToRB08CVxAyzewtq1MXv0p+Po4L1pbHLRf+TSoZ5RSBZZjY4/JMAzdXHNtnAyO0j0VNGbBXKUcNSAHZ
Step 3: Configure the ssh client to use the key to log in
Open xshell, fill in the host address, and select port 22
The method selects the public key, the user name is generally root, selects the user key, and fills in the password you just set (!!! If there is no password, you don’t need to enter it)
Step 4: Connect and log in
Double-click to connect to log in