首页 技术 正文
技术 2022年11月16日
0 收藏 748 点赞 3,208 浏览 2152 个字

使用ssh公钥实现免密码登录

ssh 无密码登录要使用公钥与私钥。linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS为例。

有机器A(10.207.160.34),B(10.221.32.234)。现想A通过ssh免密码登录到B。
首先以root账户登陆为例。

1.在A机下生成公钥/私钥对。

ssh-keygen -t rsa -P ''

-P表示密码,-P ” 就表示空密码,也可以不用-P参数,这样就要三车回车,用-P就一次回车。

该命令将在~/.ssh目录下面产生一对密钥id_rsa和id_rsa.pub。

一般采用的ssh的rsa密钥:
id_rsa     私钥
id_rsa.pub 公钥
下述命令产生不同类型的密钥
ssh-keygen -t dsa
ssh-keygen -t rsa
ssh-keygen -t rsa1

执行结果:

[hadoop@VM_32_234_centos ~]$ ssh-keygen -t rsa -P ''
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hadoop/.ssh/id_rsa):
Created directory '/home/hadoop/.ssh'.
Your identification has been saved in /home/hadoop/.ssh/id_rsa.
Your public key has been saved in /home/hadoop/.ssh/id_rsa.pub.
The key fingerprint is:
c6:ae:f2:7b:d1::eb::c3:ee:a1:ea:::da: hadoop@VM_32_234_centos
The key's randomart image is:
+--[ RSA ]----+
| |
| . |
| . . . |
| . * . |
| . S+ = |
| o . +. + . |
| . o E .+ . |
| ..o oo . |
| .+B+ |
+-----------------+

2.把A机下的~/.ssh/id_rsa.pub 复制到B机的 ~/.ssh/authorized_keys文件里,先要在B机上创建好 ~/.ssh 这个目录,用scp复制。

scp ~/.ssh/id_rsa.pub hadoop@Node1:~/authorized_keys
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

或者

scp ~/.ssh/id_rsa.pub hadoop@Node1:~/.ssh/authorized_keys

执行结果:

hadoop@VM_160_34_centos:~> scp ~/.ssh/id_rsa.pub hadoop@Node1:~/.ssh/authorized_keys
The authenticity of host 'node1 (10.221.32.234)' can't be established.
RSA key fingerprint is 4a:bc:1e:ca::::af:e4:dd:c4:ce:c1:7b:c7:.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node1,10.221.32.234' (RSA) to the list of known hosts.
hadoop@node1's password:
id_rsa.pub % .4KB/s :

由于还没有免密码登录的,所以要输入一次B机当前用户的密码。

hadoop@VM_160_34_centos:~> ssh Node1
hadoop@node1's password:
Last login: Tue Aug :: from master

3.authorized_keys的权限要是600!!!

chmod  ~/.ssh/authorized_keys

4.A机登录B机。

[hadoop@VM_32_234_centos ~]$ ssh node1
Last login: Tue Aug :: from master

SSH-KeyGen 的用法

假设 A 为客户机器,B为目标机;

要达到的目的:
A机器ssh登录B机器无需输入密码;
加密方式选 rsa|dsa均可以,默认dsa

做法:
1、登录A机器
2、ssh-keygen -t [rsa|dsa],将会生成密钥文件和私钥文件 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub
3、将 .pub 文件复制到B机器的 .ssh 目录, 并 cat id_dsa.pub >> ~/.ssh/authorized_keys
4、大功告成,从A机器登录B机器的目标账户,不再需要密码了;

ssh-keygen做密码验证可以使在向对方机器上ssh ,scp不用使用密码.
具体方法如下:
ssh-keygen -t rsa
然后全部回车,采用默认值.

这样生成了一对密钥,存放在用户目录的~/.ssh下。
将公钥考到对方机器的用户目录下,并拷到~/.ssh/authorized_keys中。

要保证.ssh和authorized_keys都只有用户自己有写权限。否则验证无效。(今天就是遇到这个问题,找了好久问题所在),其实仔细想想,这样做是为了不会出现系统漏洞。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295