首页/网站代维/使用key登陆管理linux服务器/免密码登陆linux服务器
网站代维

使用key登陆管理linux服务器/免密码登陆linux服务器

ssh除了密码验证外,还有一种比较常用的验证方式:key;他的好处除了安全外,还可以实现linux之间的免密码登陆 ,方便管理或者批量维护。

本文介绍一下windows/linux下如何配置key免密码登陆linux服务器

01 [root@A ~]# ssh-keygen -t rsa
02
03 Generating public/private rsa key pair.
04
05 Enter file in which to save the key (/root/.ssh/id_rsa):
06 //key保存的路径和文件名
07
08 Enter passphrase (empty for no passphrase):
09
10 //key的密码,免密码登陆的话留空
11
12 Enter same passphrase again:
13
14 Your identification has been saved in /root/.ssh/id_rsa
15
16 //生成了私钥/root.ssh/id_rsa
17
18 Your public key has been saved in /root/.ssh/id_rsa.pub
19
20 //生成了公钥 /root/.ssh/id_rsa.pub
21
22 The key fingerprint is:
23
24 82:8c:2c:52:06:c6:f3:c4:1c:1c:35:b7:cd:5b:42:a2 root@A
25
26 [root@A ~]# ls /root/.ssh/ //查看一下目录,已经有了这两个文件
27
28 authorized_keys id_rsa id_rsa.pub known_hosts
29
30 [root@A ~]#

查看一下服务器ssh服务公钥存放的路径

1 [root@A ~]# cat /etc/ssh/sshd_config |grep AuthorizedKeysFile
2
3 #AuthorizedKeysFile .ssh/authorized_keys

默认服务器保存公钥的位置是.ssh/AuthorizedKeysFile

所以需要将生成的公钥文件通过scp或者任何方法拷贝到你需要远程登录的服务器的这个文件中

01 [root@B ~]# cd .ssh/
02
03 [root@B .ssh]# cat id_rsa.pub >authorized_keys
04
05 //服务器本地ssh客户端默认读取的私钥路径已经包含了/.ssh/id_rsa
06
07 [root@A .ssh]# cat /etc/ssh/ssh_config |grep IdentityFile
08
09 # IdentityFile ~/.ssh/identity
10
11 # IdentityFile ~/.ssh/id_rsa
12
13 # IdentityFile ~/.ssh/id_dsa

至此你可以直接从A 执行命令ssh ipB

登录到服务器B了,就这么简单。

有一种简单的方法:使用命令ssh-copy-id 直接将本机的key加入到对方服务器的验证文件中

01 [root@A .ssh]# ssh-copy-id
02
03 Usage: /usr/bin/ssh-copy-id [-i [identity_file]] [user@]machine
04
05 [root@A .ssh]# ssh-copy-id -i /root/.ssh/id_rsa root@ipB
06
07 The authenticity of host 'ipB (ipB)' can't be established.
08
09 RSA key fingerprint is 17:21:32:02:da:3b:a1:d1:a2:69:12:36:a2:d7:59:ad.
10
11 Are you sure you want to continue connecting (yes/no)? yes
12
13 root@ipB's password:
14
15 Now try logging into the machine, with "ssh 'root@ipB'", and check in:
16
17 .ssh/authorized_keys
18
19 [root@A ~]# ssh ipB
20
21 Last login: Sun Dec 15 19:33:07 2013 from ipA
22
23 [root@B ~]#

这样执行之后,就可以直接登陆服务器了。

上面都是通过linux的ssh客户端来连接,如果通过本地来使用key登陆的话方法大同小异;

将私钥id_rsa下载到本地,然后导入到客户端的密钥认证配置的地方即可;

以SecureCRT为例,配置位置如下图选择下载下来的私钥文件即可。

131

SERVICE

相关服务与公开报价

以下为三家云市场官方店铺的在售商品,价格取自平台页面、可核验。 点击直达商品页,由平台完成下单与交付。

按本文主题推荐的云市场在售服务与公开报价
商品图 服务 / 商品 云市场 公开报价 操作
linux windows 服务器维护 网站运行环境配置 安全加固 linux windows 服务器维护 网站运行环境配置 安全加固 阿里云 ¥500 去商品页
公司企业站长Linux环境安装配置 公司企业站长Linux环境安装配置 阿里云 ¥100 去商品页
网站维护 Windows/Linux服务器维护 重装系统环境配置 杀毒木马 网站维护 Windows/Linux服务器维护 重装系统环境配置 杀毒木马 阿里云 ¥1000 去商品页
CentOS系统迁移TencentOS服务 CentOS系统迁移TencentOS服务 腾讯云 ¥500 / 次 去商品页
Discuz! X3.5 集成最新2024-4月安全补丁(AlmaLinux) Discuz! X3.5 集成最新2024-4月安全补丁(AlmaLinux) 腾讯云 ¥0 / 小时 去商品页
linux windows虚拟主机网站域名301重定向跳转 linux windows虚拟主机网站域名301重定向跳转 腾讯云 ¥1 / 次 去商品页
磁盘扩容 · 数据盘分区挂载(Linux / Windows) 磁盘扩容 · 数据盘分区挂载(Linux / Windows) 华为云 ¥200 / 次 去商品页
BT 宝塔 Linux 面板安装服务 BT 宝塔 Linux 面板安装服务 华为云 ¥100 / 次 去商品页
软件安装与部署服务(Apache / Nginx / Tomcat / 自建应用) 软件安装与部署服务(Apache / Nginx / Tomcat / 自建应用) 华为云 ¥3000 / 次 去商品页
FREE CHECKUP

先做一次免费服务器体检

我们会在 1 个工作日内给出问题清单与处置建议 —— 做不做代维都没关系。

135 410 16684
工作日 Mon – Fri 10:00 – 22:00 · 紧急故障 7×24 响应
立即拨打
CONTACT

在线留言

有什么可以帮您?

带 * 为必填