Centos7使用密钥登录

制作密钥对

首先在服务器上制作密钥对。首先用密码登录到你打算使用密钥登录的账户,然后执行以下命令:

[root@host ~]$ ssh-keygen  <== 建立密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空
Enter same passphrase again: <== 再输入一遍密钥锁码
Your identification has been saved in /root/.ssh/id_rsa. <== 私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公钥
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host

密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。

现在,在 root 用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥。

在服务器上安装公钥

键入以下命令,在服务器上安装公钥:

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys

如此便完成了公钥的安装。为了确保连接成功,请保证以下文件权限正确:

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh

设置 SSH,打开密钥登录功能

vi /etc/ssh/sshd_config

RSAAuthentication yes # 开启密钥登入的认证方式

PubkeyAuthentication yes # 开启密钥登入的认证方式

PermitRootLogin yes # 此处请留意 root 用户能否通过 SSH 登录,默认为yes

# 可以正常用密钥登录了,再把这里改为no
PasswordAuthentication yes #当我们完成全部设置并以密钥方式登录成功后,可以禁用密码登录。这里我们先不禁用,先允许密码登陆

重启服务之前,先把私钥放到本地

[root@localhost .ssh]# cd /root/.ssh/
[root@localhost .ssh]# ll
总用量 16
-rw------- 1 root root 1617 8月   9 09:47 authorized_keys
-rw------- 1 root root 1675 8月   9 09:46 id_rsa # 下载这个到本地
-rw-r--r-- 1 root root  408 8月   9 09:46 id_rsa.pub
-rw-r--r-- 1 root root 1527 8月   6 15:55 known_hosts

重启sshd服务

systemctl restart sshd
声明:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@starryfrp.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA打赏
共{{data.count}}人
人已打赏
折腾闲聊

Ubuntu20.04 LTS下Minecraft纯净服开服流程

2022-2-16 15:30:21

折腾闲聊

玩客云如何开MC服(同样适用于树莓派)

2022-3-29 22:37:27

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索