当前位置: 代码网 > 服务器>服务器>Linux > Linux实现免密登录的配置方法

Linux实现免密登录的配置方法

2024年05月26日 Linux 我要评论
需求描述:192.168.31.10服务器的yunwei账号,想要免密登陆到192.168.31.15服务器上。直接ssh root@192.168.31.15这样登陆,不用输入密码。实现:1、在10

需求描述:
192.168.31.10服务器的yunwei账号,想要免密登陆到192.168.31.15服务器上。
直接ssh root@192.168.31.15这样登陆,不用输入密码。

实现:
1、在10机器上,创建运维账号。

[root@docker01 ~]# id yunwei				检查yunwei账号是否存在
id: yunwei: no such user
[root@docker01 ~]# useradd yunwei			创建yunwei账号
[root@docker01 ~]# su - yunwei				切换到yunwei账号
[yunwei@docker01 ~]$ pwd
/home/yunwei

2、在yunwei账号下创建密钥

[yunwei@docker01 ~]$ ssh-keygen		创建密钥,一路回车
generating public/private rsa key pair.
enter file in which to save the key (/home/yunwei/.ssh/id_rsa):
created directory '/home/yunwei/.ssh'.
enter passphrase (empty for no passphrase):
enter same passphrase again:
your identification has been saved in /home/yunwei/.ssh/id_rsa.
your public key has been saved in /home/yunwei/.ssh/id_rsa.pub.
the key fingerprint is:
sha256:klxarvzggoqf62rygwkguekspd39l0pudqbt1mqp3nu yunwei@docker01
the key's randomart image is:
+---[rsa 2048]----+
|  ..   +o=.o..   |
| o.   .+=.=   e  |
|++ .  +o=.       |
|oo= .o o.o       |
|..+.+.+ so.      |
| o o =o.+ .      |
|  . bo + .       |
|   *  +          |
|    ..           |
+----[sha256]-----+
检查密钥是否创建成功
[yunwei@docker01 ~]$ pwd
/home/yunwei
[yunwei@docker01 ~]$ ll -a
total 12
drwx------.  5 yunwei yunwei 103 mar 25 23:18 .
drwxr-xr-x. 16 root   root   177 mar 25 23:17 ..
-rw-r--r--.  1 yunwei yunwei  18 mar 31  2020 .bash_logout
-rw-r--r--.  1 yunwei yunwei 193 mar 31  2020 .bash_profile
-rw-r--r--.  1 yunwei yunwei 231 mar 31  2020 .bashrc
drwxrwxr-x.  3 yunwei yunwei  18 mar 25 23:17 .cache
drwxrwxr-x.  3 yunwei yunwei  18 mar 25 23:17 .config
drwx------.  2 yunwei yunwei  38 mar 25 23:18 .ssh
[yunwei@docker01 ~]$ cd .ssh/
[yunwei@docker01 .ssh]$ ls
id_rsa  id_rsa.pub

3、复制密钥到15服务器

[yunwei@docker01 .ssh]$ ssh-copy-id root@192.168.31.15		复制密钥到15机器
/bin/ssh-copy-id: info: source of key(s) to be installed: "/home/yunwei/.ssh/id_rsa.pub"
the authenticity of host '192.168.31.15 (192.168.31.15)' can't be established.
ecdsa key fingerprint is sha256:v3zhw/rvst+t7qfanidiihhbalrlnilzl8hg3tazqca.
ecdsa key fingerprint is md5:cf:b8:e1:f6:a5:61:60:f0:77:aa:f3:76:ab:d2:ce:9b.
are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: info: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: info: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.31.15's password:
number of key(s) added: 1
now try logging into the machine, with:   "ssh 'root@192.168.31.15'"
and check to make sure that only the key(s) you wanted were added.

4、验证免密登陆

[yunwei@docker01 .ssh]$ ssh root@192.168.31.15
last login: sun mar 26 11:21:02 2023 from 192.168.31.1

补充:优化密钥创建方式,免交互创建密钥

[yunwei@docker01 .ssh]$ ssh-keygen -p '' -f id_rsa		免交互方式,创建密钥
generating public/private rsa key pair.
your identification has been saved in id_rsa.
your public key has been saved in id_rsa.pub.
the key fingerprint is:
sha256:hxusbtv1o1d1pfiyg/+ic1ifnzh8q3ngf5eiuq8iexq yunwei@docker01
the key's randomart image is:
+---[rsa 2048]----+
|       eooob=+b  |
|      .. .=o=* +o|
|      ... ..o+ o*|
|       ..+ .o + +|
|       os oo + . |
|       .o+. . *  |
|        ...o . . |
|         . .o . .|
|            o+ ..|
+----[sha256]-----+
[yunwei@docker01 .ssh]$ ls
id_rsa  id_rsa.pub  known_hosts

参数说明:
-t 指定要创建的密钥类型
dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa
可能的值为“dsa”、“ecdsa”、“ecdsa-sk”、“ed25519”、“ed25519-sk”或“rsa”。
当使用 rsa ca 密钥签署证书时,此标志还可用于指定所需的签名类型。可用的 rsa 签名变体是“ssh-rsa”(sha1 签名,不推荐)、“rsa-sha2-256”和“rsa-sha2-512”(默认值)

-p 密码
提供(旧)密码。
这里的密码,是密钥的密码,不是远程主机的密码,随便设置。但是,这就失去了免密登陆的意义。因为,设置了这个后,登陆远程主机时,就必须输入密钥密码。
所以,一般这个指指定为空即可。

-f 文件名
指定密钥文件的文件名
这里的文件名,必须指定为id_rsa,不然,把密钥推送到目标机器,依然无法实现免密登陆。

总结:

就三个命令

cd							进入当前账号家目录
ssh-keygen					连续三次回车		
ssh-copy-id 192.168.31.15	复制公钥到hadoop104服务器,这样,就可以免密访问hadoop104服务器

这里用户账号省略,则使用当前账号进行免密登陆
比如,当前账号是test

ssh-copy-id 192.168.31.15 等价与 ssh-copy-id test@192.168.31.15

实现的效果是,当前服务器的test账号可以免密登陆15服务器的test账号

参考资料:https://www.cnblogs.com/dirigent/p/16636545.html 

到此这篇关于linux实现免密登录的配置方法的文章就介绍到这了,更多相关linux免密登录内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

  • Linux手把手教你实现udp服务器的详细过程

    前言上一篇文章中我们讲到了很多的网络名词以及相关知识,下面我们就直接进入udp服务器的实现。一、udp服务器的实现首先我们需要创建五个文件(文件名可以自己命名也可以和我一样),分别…

    2024年05月26日 服务器
  • Linux服务器磁盘空间清理方法汇总

    Linux服务器磁盘空间清理方法汇总

    一、引言在长时间运行过程中,linux服务器上的磁盘空间可能会被各种文件和目录占用,导致磁盘空间不足。为了确保服务器的稳定性和性能,定期清理磁盘空间是非常必要的... [阅读全文]
  • linux服务器磁盘满了的三种解决方案

    方法一步骤一:遇到磁盘空间不足的报错时候,首先使用df -h查看磁盘空间使用情况,如图/home/zhang目录磁盘空间达到100%。步骤二:进入目录/home/zhang,查找磁…

    2024年05月26日 服务器
  • 在Linux中安装Git的详细流程

    在Linux中安装Git的详细流程

    本文对在ubuntu系统中实现git的下载、安装的方法介绍。首先,我们现在终端中输入如下代码,查看当前电脑中是否已经有了git。git --version运行上... [阅读全文]
  • Linux系统配置静态IP地址的详细步骤

    前言在安装linux后,系统的网络ip地址默认是自动分配的,这将导致每次启动linux系统后,系统的ip地址都会发生改变,这使在开发过程中及其不方便,给系统配置一个固定的ip地址显…

    2024年05月26日 服务器
  • Linux关于Sudo的隐晦bug引发的一次业务问题排查

    写在前面记录一次生产环境sudo启动进程频繁被kill且不报错的异常处理过程,如果遇到同样的问题只想要解决方案,直接跳到处理方案部分即可。问题描述这次记录一个比较特殊的问题,先说一…

    2024年05月26日 服务器

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com