1. Overview
How passwordless login works: you log in with key-based authentication. First, generate a public key and a private key on your own server, then upload the public key to the remote server. When a connection to the remote server is being established, the remote server first checks whether it holds the public key of the requesting server. If it doesn't, the user logging in remotely has to enter a password.
2. Steps
- First, add the public key to the server's authorized public keys
- Step 1: Edit the file ~/.ssh/authorized_keys and add the public key to it
echo 你的公钥内容 >> ~/.ssh/auth

If you get the error "No such file or directory", run the following command and then repeat the previous step:
mkdir ~/.ssh
- Step 2: Change the permissions: set the file to 600 and the folder to 700
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
- Enable key-based login on the server
- Step 1: Edit the sshd_config file
vi /etc/ssh/sshd_config
- Step 2: Change these settings in the file
# 是否允许 root 远程登录,如果只有用户root此项请设置为yes
PermitRootLogin no
# 密码登录是否打开
PasswordAuthentication yes
# 开启公钥认证
RSAAuthentication yes
PubkeyAuthentication yes
# 存放登录用户公钥的文件位置
# 位置就是登录用户名的家目录下的 .ssh
# root 就是 /root/.ssh
# ks 就是 /home/ks/.ssh
AuthorizedKeysFile .ssh/authorized_keys
- Step 3: Restart the sshd service
systemctl restart sshd
- Step 4: Test logging in with the key. If you can log in normally, the configuration worked
ssh -i ~/.ssh/id_rsa username@ip
- For server security, disable password login
- Step 1: Edit the sshd_config file
vi /etc/ssh/sshd_config
- Step 2: Change this setting in the file
# 是否允许密码登录
PasswordAuthentication no
Finally, restart the sshd service and you're done.
systemctl restart sshd