SSH通过密钥登陆

1.背景介绍

平时我们都是通过输入账号和密码的方式登陆远程终端,那有没有其他的玩法呢?答案是:当然有!下面就介绍一种通过密钥的方式登陆SSH。

下面开始动手操作。

2.实验步骤

假设你已经通过ssh工具登陆上了linux终端。

(1)生成公钥和私钥

ssh-keygen -t rsa

Enter file in which to save the key (/root/.ssh/id_rsa): ##直接回车使用默认路径

Created directory '/root/.ssh'.

Enter passphrase (empty for no passphrase): ##输入密码

Enter same passphrase again: ##重复密码

在/root/.ssh/目录下会生成两个文件,id_rsa为私钥,id_rsa.pub为公钥。私钥自己下载到本地电脑妥善保存,公钥则可以任意公开。

(2)下载私钥到windows

sz /root/.ssh/id_rsa

如果没有请安装软件包lrzsz(yum install -y lrzsz)

(3)导入公钥

cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

(4)更改SSH配置文件

修改SSH 的配置文件/etc/ssh/sshd_config,找到下面3行:

#RSAAuthentication yes

#PubkeyAuthentication yes

#AuthorizedKeysFile .ssh/authorized_keys

将前面的#去掉后保存。重启SSH 服务,运行命令:systemctl restart sshd

(5)xshell配置(看图)

1.新建一个会话

2.选择身份验证方式

3.导入私钥

4.输入自己设定的密码

5.确定用户密钥

6.输入密码

7.选择相应的会话

8.成功登陆

(6)关闭SSH密码登陆

修改SSH的配置文件/etc/ssh/sshd_config,找到下面1 行:

PasswordAuthentication yes

修改为:

PasswordAuthentication no

重启SSH服务,并运行命令:

systemctl restart sshd

下面关于SSH相关的文章您也可能喜欢,不妨参考下:

猜你喜欢

转载自www.linuxidc.com/Linux/2017-06/144997.htm