Linux Baidu Cloud Compute Setting Login Method
Background
This document explains how to configure Linux server login methods (key-based and password-based), using CentOS 7.6 as an example.
Set key-based login
Step I: Generate a key pair on the server.
Run "ssh-keygen -t rsa" on the client to generate a key pair. By default, the keys are stored in /root/.ssh, with no password set by default. You can customize the password and directories as needed.
1root@ins8o1pb:~# ssh-keygen -t rsa
2
3Generating public/private rsa key pair.
4Enter file in which to save the key (/root/.ssh/id_rsa):
5Enter passphrase (empty for no passphrase):
6Enter same passphrase again:
7Your identification has been saved in /root/.ssh/id_rsa.
8Your public key has been saved in /root/.ssh/id_rsa.pub.
9The key fingerprint is:
10SHA256:nBctW+aEbuFIDXeYQSmGqHAo4QFe6fWKjjBTBc9hL2Y root@instance-xh28o1pb
11The key's randomart image is:
12+---[RSA 2048]----+
13|++.o+. ...+=. |
14|* +=o+. o+++ |
15|.=.oE o...* = |
16| o+ . + = X |
17| . . . S * . |
18|+ . . o |
19|.oo |
20| . . |
21| |
22+----[SHA256]-----+
Check the files in the /root/.ssh directory. The private key is id_rsa, and the public key is id_rsa.pub.

Step II: Install the public key on the server.
Run the following command to install the public key on the server.
1root@ins8o1pb:~# cd .ssh
2root@ins8o1pb:~# cat id_rsa.pub >> authorized_keys
After completing the public key installation, ensure the file permissions are properly set to guarantee a successful connection.
1root@ins8o1pb:~# chmod 600 authorized_keys
2root@ins8o1pb:~# chmod 700 ~/.ssh
Step III: Set SSH and enable key-based login function.
- Edit the file
/etc/ssh/sshd_config, and conduct the following settings:
RSAAuthentication yes
PubkeyAuthentication yes
Additionally, observe whether the root user can sign in via SSH:
PermitRootLogin yes
- After completing all settings and successfully logging in via Key, disable the password-based login:
PasswordAuthentication no
- Finally, restart the SSH service.
1root@ins8o1pb:~# service sshd restart
Step IV: Download the private key to the client.
Transfer the private key file id_rsa to the client.
- Sign in to a Linux server
using the putty client on Windows. Convert the private key format by opening putty key generator 工具-->File -> Load private key, and clicking save private key. The conversion is completed. Windows private keys typically end with .ppk, e.g., save as id_rsa.ppk. - When signing in with a Putty key, perform the following operations after entering the server address:
Enter the username in Auto-login username in the Connection -> Data interface;
Browse to select the private key file id_rsa.ppk in Connection -> SSh -> Auth -> Private key file for authentication;
Save these settings, and click Open to sign in to the server directly.
Set password-based login
If the password-based login is required, simply change PasswordAuthentication to yes in /etc/ssh/sshd_config.
1PasswordAuthentication yes
