首页 > 系统相关 >使用公钥登录 Linux 服务器

使用公钥登录 Linux 服务器

时间:2024-09-01 19:16:07浏览次数:5  
标签:公钥 xxx VM ssh Linux 服务器 09 root id

使用公钥登录 Linux 服务器

Linux 上使用公钥登录

  1. 在客户端上通过 ssh-copy_id​ 将公钥写入到服务器的 authorized_keys:
[root@VM-4-11-centos ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
[email protected]'s password: 

Number of key(s) added: 1

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

[root@VM-4-11-centos ~]# 
  1. 通过 ssh 登录:
[root@VM-4-11-centos ~]# ssh [email protected]
Last login: Fri Aug 31 09:49:48 2024 from 222.64.93.61
[root@VM-4-4-opencloudos ~]#

Windows 上使用公钥登录

  1. 把 Windows 上生成的 id_rsa.pub 文件上传到服务器上:
C:\Users\zhpj\.ssh>dir
 Volume in drive C is OS
 Volume Serial Number is DE62-06F3

 Directory of C:\Users\zhpj\.ssh

2024-08-30  09:26    <DIR>          .
2024-08-28  09:13    <DIR>          ..
2022-02-10  17:37    <DIR>          .ssh
2023-05-16  09:08             2,245 config
2021-08-02  09:58             1,679 id_rsa
2021-08-02  09:58               405 id_rsa.pub
2022-07-20  13:43             2,602 id_rsa_zhpj07
2022-07-20  13:43               569 id_rsa_zhpj07.pub
2024-08-30  09:26             9,141 known_hosts
2024-08-30  09:26             8,395 known_hosts.old
               7 File(s)         25,036 bytes
               3 Dir(s)  27,532,115,968 bytes free

C:\Users\zhpj\.ssh>
C:\Users\zhpj\.ssh>scp .\id_rsa_zhpj07.pub [email protected]:/root/
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
ED25519 key fingerprint is SHA256:dcPCSvTa3Mk4BpeTG2Or6i1dOC+Y300WhKGL773lJfk.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added 'xxx.xxx.xxx.xxx' (ED25519) to the list of known hosts.
[email protected]'s password:
id_rsa_zhpj07.pub                                                                     100%  569    61.3KB/s   00:00

C:\Users\zhpj\.ssh>
  1. 在服务器上,将上传的公钥内容追加到 .ssh/authorized_keys​ 文件的最后:
[root@VM-4-4-opencloudos ~]# ll
total 4
-rw-r--r-- 1 root root 569 Aug 31 09:26 id_rsa_zhpj07.pub
[root@VM-4-4-opencloudos ~]#
[root@VM-4-4-opencloudos ~]# cat id_rsa_zhpj07.pub >> .ssh/authorized_keys 
[root@VM-4-4-opencloudos ~]#
  1. 检查 .ssh/authorized_keys​ 的权限,确认是 600:
[root@VM-4-4-opencloudos ~]# ll .ssh/
total 4
-rw------- 1 root root 569 Aug 31 09:30 authorized_keys
[root@VM-4-4-opencloudos ~]#
  1. 检查 /etc/ssh/sshd_config​ 配置文件:
# 启用证书登录
PubkeyAuthentication yes
RSAAuthentication yes

# 禁用密码登录
PasswordAuthentication no
  1. 重启 sshd​ 服务:
[root@VM-4-4-opencloudos ~]# systemctl restart sshd
[root@VM-4-4-opencloudos ~]#
  1. windows 上登录证书登录:
C:\Users\zhpj>ssh -i ~\.ssh\id_rsa_zhpj07 [email protected]
Last login: Fri Aug 31 09:36:27 2024 from xxx.xxx.xxx.xxx
[root@VM-4-4-opencloudos ~]#
  1. 修改 Windows 上的 .ssh/config​ 文件,避免每次 ssh 时都要通过 -i​ 指定证书路径:
Host zhpj07
	hostname xxx.xxx.xxx.xxx
	user root
	port 22
	identityfile ~/.ssh/id_rsa_zhpj07
	ServerAliveCountMax 5
	ServerAliveInterval 5
  1. 在终端中通过 ssh zhpj07​ 登录:
C:\Users\zhpj>ssh zhpj07
Last login: Fri Aug 31 09:39:28 2024 from xxx.xxx.xxx.xxx
[root@VM-4-4-opencloudos ~]#

标签:公钥,xxx,VM,ssh,Linux,服务器,09,root,id
From: https://www.cnblogs.com/zhpj/p/18391596/use-the-public-key-to-board-the-linux-server-z1lqr

相关文章

  • 使用公钥登录 Linux 服务器
    使用公钥登录Linux服务器‍Linux上使用公钥登录在客户端上通过ssh-copy_id​将公钥写入到服务器的authorized_keys:[root@VM-4-11-centos~]#[email protected]/usr/bin/ssh-copy-id:INFO:Sourceofkey(s)tobeinstalled:"/root/.ssh/id_rsa.pub"/usr......
  • Linux目录详解
     一.树状目录结构图:二.树状目录结构介绍: 1./bin目录 /bin目录包含了引导启动所需的命令或普通用户可能用的命令(可能在引导启动后)。这些命令都是二进制文件的可执行程序(bin是binary--二进制的简称),多是系统中重要的系统文件。2./sbin目录 /sbin目......
  • Linux性能调优大作战:从零到英雄,手把手教你打造极速系统!让你的服务器快如闪电!
    第一章引言Linux系统性能调优在信息技术领域具有不可忽视的重要性。随着Linux操作系统的广泛应用,从桌面环境到大型服务器集群,其性能优化变得尤为关键。调优不仅可以提升系统的响应速度和吞吐量,还能降低资源消耗,从而延长硬件使用寿命,减少总体拥有成本。本文研究旨在深入探讨Li......
  • 零知识证明-公钥分发方案DH((六)
    前言椭圆曲线配对,是各种加密构造方法(包括确定性阀值签名、zk-SNARKs以及相似的零知识证明)的关键元素之一。椭圆曲线配对(也叫“双线性映射”)有了30年的应用历史,然而最近这些年才把它应用在密码学领域。配对带来了一种“加密乘法”的形式,这很大的拓展了椭圆曲线协议的应......
  • 正点原子Linux C应用编程:移植tslib并使其适配7寸LCD1024*600的GT911触摸驱动
    正点原子LinuxC应用编程:移植tslib并使其适配7寸LCD1024*600的GT911触摸驱动作者在学习【正点原子】I.MX6U嵌入式LinuxC应用编程指南V1.4时,发现移植tslib后,触摸事件触发不正常。使用的硬件版本:正点原子I.MX6UALPHAV2.4版本底板,LCD:正点原子7寸1024*600,型号ATK-MD0700R-102460......
  • 正点原子Linux Framebuffer编程:解决示例程序在开发板上LCD显示错位和颜色异常
    正点原子LinuxFramebuffer编程:解决示例程序在开发板上运行7寸LCD显示错位和颜色异常作者在学习【正点原子】I.MX6U嵌入式LinuxC应用编程指南V1.4时,发现其配套的程序在开发板上运行不正常。使用的硬件版本:正点原子I.MX6UALPHAV2.4版本底板,LCD:正点原子7寸1024*600,型号ATK-MD0......
  • Linux权限管理
    Linux权限管理1.Linux权限概述1.1rwx概述Linux权限含义r#read读取,是否可以查看文件内容w#write修改,是否可以修改文件内容x#exec可执行,一般是给命令或系统脚本使用,运行命令1.2查看文件,目录权限[root@Kylin-V10......
  • CPU核心不同,对服务器性能有哪些影响?
    CPU核心数量对服务器性能有显著影响,尤其在处理多任务和并发工作负载时。以下是不同核心数量对服务器性能的具体影响:1.并行处理能力1)多任务处理:多核心CPU可以同时处理多个任务,因为每个核心都可以独立执行线程。这意味着服务器可以更高效地处理并发请求,减少响应时间。对于......
  • 服务器出现故障怎么办?快速排查与解决方法
    当服务器出现故障时,可能会导致业务中断,给企业或个人带来不小的损失。为了尽快恢复服务,我们需要迅速定位问题并采取相应的解决措施。下面,我们将介绍一些服务器故障的快速排查与解决方法。常见服务器故障及原因服务器故障可能由多种原因引起,包括但不限于硬件故障、软件问题、网......
  • Linux抓包神器 tcpdump 使用指南
    tcpdump是一款强大的网络抓包工具,它使用libpcap库来抓取网络数据包,这个库在几乎所有的Linux/Unix系统中都有。熟悉tcpdump的使用能够帮助用户分析调试网络数据。以下是tcpdump的详细使用指南:一、安装tcpdump在Linux系统中,可以通过包管理器安装tcpdump。对于Debian系系统,可以......