hi,欢迎访问本站!
当前位置: 首页学习笔记正文

linux-ssh免密登录

用户投稿 学习笔记 27阅读

问题:在日常运维工作中,在服务器间来回切换是运维人员必不可少的工作内容,每次切换服务器都需要输入密码时运维人员很苦恼的事情,那么通过怎样的方式来减少工作量呢?

ssh简介 在Linux中,取得远程系统的Shell 最常见的方法是使用Secure shell(SSH),主流的Linux发现版本,包括macOS提供OpenSSH命令行来实现。 通过SSH客户端我们可以连接到运行了SSH服务器的远程机器上,也就是说,我们可以通过ssh来远程控制我们的电脑或者服务器。

Example: 1号虚拟机ip 192.168.13.130 2号虚拟机ip 192.168.13.129 当1号虚拟机想要通过ssh登录到2号虚拟机工作时,提示需要通过输入密码才能登录。

[root@rac19c1 ~]$ssh root@192.168.13.129The authenticity of host '192.168.13.129 (192.168.13.129)' can't be established.ECDSA key fingerprint is SHA256:qQhxgKWN8b6NVmL+LFlCYJnENpismazrERxH7WZTViE.ECDSA key fingerprint is MD5:dd:c0:08:cc:10:10:00:c8:02:de:97:36:dc:34:d7:89.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '192.168.13.129' (ECDSA) to the list of known hosts.root@192.168.13.129's password:

思考:怎么才能让1号虚拟机在登录2号虚拟机的时候不输入密码呢?

解决方式原理分析:

一些远程系统为了安全性不允许直接使用密码验证。在不输入密码的情况下可以对公钥进行认真从而进行身份认证 使用公私钥进行身份验证时,用户拥有私钥文件,相当于密码,在远程服务器上配置相匹配的公钥,公钥是不需要保密的。在使用ssh登录的时候提供公钥,如果远程服务器的对应账户中安装了匹配的公钥,就会在不询问密码的情况下运行登录。

在尝试登录的时候,SSH服务器通过公钥发出一个只能私钥能正确解答的问题,通过这种方式验证身份。

使用 ssh-keygen 命令可以创建于身份验证的私钥与公钥,默认情况下私钥和公钥存放以下位置

·/.ssh/id_rsa 私钥·/.ssh/id_rsa.pub 公钥 ssh-keygen 生成密钥的时候可以指定密码,此密码是使用密钥的密码,对私钥进行保护。ssh-keygen -f ssh_key_file 可以指定保存密码的文件,私钥和公钥的权限必须为600和644。在使用基于密码的身份认证之前,需要将公钥复制到目标系统上,ssh-copy-id 命令将SSH公钥复制到目标远程系统上。 默认使用的是是/home/.ssh/id_rsa.pub文件。可以通过-i选项来指定。

实现步骤: 1.生成密钥对ssh-keygen

[root@rac19c1 ~]$ssh-keygenGenerating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): 默认生成私钥路径Enter passphrase (empty for no passphrase): 输入密码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:SHA256:VLtC5TVmFNn/635rApmX/JVQcZxVaG+tkNAa+tn3qSc root@rac19c1The key's randomart image is:+---[RSA 2048]----+| ooB+ +O|| +o=o.+oo|| o.o+ o.o.|| o. ..o. =|| S..o+.ooo|| .o+.+o.o|| o...+|| E *o|| .O+o|+----[SHA256]-----+

2.将公钥拷贝到192.168.13.129服务器上

[root@rac19c1 ~]$ssh-copy-id root@192.168.13.129/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 keysroot@192.168.13.129's password: 输入192.168.13.129的密码root@192.168.13.129's password:Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@192.168.13.129'"and check to make sure that only the key(s) you wanted were added.添加成功

再次尝试1号虚拟机连接2号虚拟机

[root@rac19c1 ~]$ssh 192.168.13.129Last login: Sat Apr 9 23:18:26 2022 from 192.168.13.130

登录成功 提示:其它用户也可以通过上面的方式进行免密登录,只需要将公钥拷贝给对应的用户就可以了 如;

ssh-copy-id student@192.168.13.129
标签:
声明:无特别说明,转载请标明本文来源!
发布评论
正文 取消