SSH之密钥登录linux
1.背景介绍shell
平时咱们都是经过输入帐号和密码的方式登录远程终端,那有没有其余的玩法呢?答案是:固然有!下面就介绍一种经过密钥的方式登录ssh。
ubuntu
工具准备:一台电脑(推荐win10),一台虚拟机(推荐vmware workstation11),ssh登录工具(xshell,securecrt,putty,推荐xshell),linux系统(redhat,centos,debian,ubuntu,推荐centos7.3)。windows
下面开始动手操做。centos
2.实验步骤ssh
假设你已经经过ssh工具登录上了linux终端。ide
(1)生成公钥和私钥工具
ssh-keygen -t rsacentos7
Enter file in which to save the key (/root/.ssh/id_rsa): ##直接回车使用默认路径spa
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