How to set up SSH passwordless login in Linux

SSH (Secure SHELL) is an open source and trusted network protocol used to log in to remote servers to execute commands and programs.

It is also used to transfer files from one computer to another over the network using the Secure Copy (SCP) command and the rsync command.

In this article [1] , we will show you how to set up passwordless login on RHEL-based Linux distributions such as CentOS, Fedora, Rocky Linux, and AlmaLinux, as well as Debian-based distributions such as Ubuntu and Mint, using ssh key to connect to a remote Linux server without entering a password.

Using a passwordless login with an SSH key will increase trust between the two Linux servers for easy synchronization or transfer of files.

My setup environment

SSH Client : 192.168.0.12 ( Fedora 36 )
SSH Remote Host : 192.168.0.11 ( CentOS 8 )

If you are dealing with multiple Linux remote servers, SSH passwordless login is one of the best ways to automate tasks such as automated backups using scripts, file synchronization using SCP commands, and remote command execution.

In this example, we will set up SSH passwordless automatic login from server 192.168.0.12 as user tecmint to 192.168.0.11 as user sheena.

1. Create an authentication SSH-Keygen key

First log in to the server 192.168.0.12 using user tecmint, and use the following command to generate a pair of public keys.

$ ssh-keygen -t rsa
alt

2. Upload SSH key

Use SSH from server 192.168.0.12 and upload the newly generated public key (id_rsa.pub) to the .ssh directory of sheena on server 192.168.0.11. The file name is authorized_keys.

$ ssh-copy-id [email protected]

Make sure the correct permissions are set on the ~/.ssh directory and ~/.ssh/authorized_keys file on the remote server.

$ ssh [email protected] "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"

3. Disable password verification (optional)

为了提高安全性,您可以在远程服务器上禁用密码身份验证,仅允许 SSH 密钥身份验证。为此,请打开远程服务器上的 SSH 服务器配置文件:

$ sudo nano /etc/ssh/sshd_config
OR
$ sudo vi /etc/ssh/sshd_config

找到包含PasswordAuthentication 的行并将其设置为no。

PasswordAuthentication no

保存文件并重新启动 SSH 服务。

$ sudo systemctl restart sshd

4. 测试 SSH 无密码登录

从现在开始,您可以以 sheena 用户身份从服务器 192.168.0.12 以 howtoing 用户身份登录 192.168.0.11,无需密码。

$ ssh [email protected]
alt

在本文中,您学习了如何使用 ssh 密钥设置 SSH 无密码登录。我希望这个过程很简单。如果您有任何疑问,请在下面的评论部分发表。

Reference

[1]

Source: https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/

本文由 mdnice 多平台发布

Guess you like

Origin blog.csdn.net/swindler_ice/article/details/132529230