Warning: Attempt to read property "license" on null in /home/hostingwiki/public_html/wp-content/themes/zimeiti-1/functions.php on line 1
使用SSH Keygen进行SSH免密码登录,只需4个步骤 – HostingWIKI

使用SSH Keygen进行SSH免密码登录,只需4个步骤

广告位

SSH(Secure SHELL)是一种开源且最受信任的网络协议,默认的端口以及密码验证方式容易遭受黑客密码暴力破解,本文将通过启用密钥验证的方式来提升你的Linux操作系统安全性。

SSH(Secure SHELL)是一种开源且最受信任的网络协议,用于登录远程服务器以执行命令和程序。它还用于使用安全副本(SCP)协议通过网络将文件从一台计算机传输到另一台计算机。

在本文中,我们将向您展示如何使用ssh密钥在RHEL / CentOS 7.x / 6.x / 5.x和Fedora上设置无密码登录,以便在不输入密码的情况下连接到远程Linux服务器。使用SSH密钥进行无密码登录将增加两台Linux服务器之间的信任,以便轻松进行文件同步或传输。

第一步:在 CentOS 上创建 SSH 密钥

在生成新的 SSH 密钥对之前,最好检查 CentOS 客户端计算机上的现有 SSH 密钥。

为此,请运行以下ls 命令 ,列出所有公钥(如果有):

[root@johnhao ~]# ls -l ~/.ssh/id_*.pub
ls: cannot access /root/.ssh/id_*.pub: No such file or directory

如果命令的输出返回类似的内容,No such file or directory或者no matches found这意味着您的客户端计算机上没有 SSH 密钥,您可以继续下一步并生成 SSH 密钥对。

如果存在现有密钥,您可以使用这些密钥并跳过下一步,或者备份旧密钥并生成新密钥。

首先生成一个新的 4096 位 SSH 密钥对,并使用您的电子邮件地址作为注释:

[tecmint@tecmint.com ~]$ ssh-keygen -t rsa
# 系统会提示制定文件名,如果接受文件位置和文件名,直接按回车即可
Enter file in which to save the key (/home/yourusername/.ssh/id_rsa):
# 接下来系统会要求您输入安全密码,是否要使用密码短语取决于您。如果您选择使用密码短语,您将获得额外的安全层。如果不想使用密码,只需要按回车
Enter passphrase (empty for no passphrase):
Generating public/private rsa key pair.
Enter file in which to save the key (/home/tecmint/.ssh/id_rsa): [Press enter key]
Created directory '/home/tecmint/.ssh'.
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Press enter key]
Your identification has been saved in /home/tecmint/.ssh/id_rsa.
Your public key has been saved in /home/tecmint/.ssh/id_rsa.pub.
The key fingerprint is:
5f:ad:40:00:8a:d1:9b:99:b3:b0:f8:08:99:c3:ed:d3 tecmint@tecmint.com
The key's randomart image is:
+--[ RSA 2048]----+
|        ..oooE.++|
|         o. o.o  |
|          ..   . |
|         o  . . o|
|        S .  . + |
|       . .    . o|
|      . o o    ..|
|       + +       |
|        +.       |
+-----------------+

要验证生成了新的 SSH 密钥对,请键入:

[root@johnhao ~]# ls -l ~/.ssh/id_*.pub
-rw-r--r-- 1 root root 381 Aug 18  2019 /root/.ssh/id_rsa.pub

第二步:将公钥复制到 CentOS 服务器

既然生成了 SSH 密钥对,下一步就是将公钥复制到您要管理的服务器上。

将公钥复制到远程服务器的最简单且推荐的方法是使用名为ssh-copy-id. 在您的本地机器终端类型上:

[root@johnhao ~]$ ssh remote_username@server_ip_address 
The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established.
RSA key fingerprint is 45:0e:28:11:d6:81:62:16:04:3f:db:38:02:la:22:4e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.11' (ECDSA) to the list of known hosts.
remote_username@server_ip_address's password: 
# 输入密码不会显示任何字符,输入完成后回车
输入密码,一旦用户通过身份验证,公钥~/.ssh/id_rsa.pub将附加到远程用户~/.ssh/authorized_keys文件中。连接将被关闭。
Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'username@server_ip_address'"
and check to make sure that only the key(s) you wanted were added.

如果该ssh-copy-id实用程序在您的本地计算机上不可用,请使用以下命令复制公钥:

cat ~/.ssh/id_rsa.pub | ssh remote_username@server_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

第三步:使用 SSH 密钥登录到您的服务器

完成上述步骤后,您应该能够登录远程服务器而不会提示输入密码。

要验证它,请尝试通过SSH登录到您的服务器 :

[root@johnhao ~]$ ssh remote_username@server_ip_address 

如果您没有为私钥设置密码,您将立即登录。否则,您将被要求输入密码。

第四步:禁用 SSH 密码认证

要为远程服务器添加额外的安全层,您可以禁用 SSH 密码身份验证。

在继续之前,请确保您可以在没有密码的情况下以具有sudo 权限的用户身份登录到您的服务器 。

请按照以下步骤禁用 SSH 密码验证:

登录到您的远程服务器:

ssh sudo_user@server_ip_address

/etc/ssh/sshd_config使用文本编辑器打开 SSH 配置文件 :

sudo nano /etc/ssh/sshd_config

搜索以下指令并修改如下:

PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no


完成后保存文件并通过键入以下内容重新启动 SSH 服务:

sudo systemctl restart ssh

此时,基于密码的身份验证被禁用。

总结

在本教程中,您学习了如何生成新的 SSH 密钥对并设置基于 SSH 密钥的身份验证。您可以将相同的密钥添加到多个远程服务器。

我们还向您展示了如何禁用 SSH 密码身份验证并向您的服务器添加额外的安全层。

默认情况下,SSH 侦听端口 22。更改默认 SSH 端口 可降低自动攻击的风险。

如果您经常连接到多个系统,则可以通过在SSH 配置文件中定义所有连接来简化工作流程 。

如果您有任何问题或反馈,请随时发表评论。

关于作者: Harrison K

为您推荐

广告位

Leave a Reply

Your email address will not be published. Required fields are marked *

2条评论