CentOS7做ssh免密登录
(1)实验环境
两台CentOS7:
youxi1 192.168.1.6
youxi2 192.168.1.7
这里我将防火墙关闭进行实验,如果防火墙开启,请将端口加入到防火墙规则中。
(2).目标
在ssh端口不为22的情况下,进行单向免密登录或双向免密登录(端口不一致)
(3).实验
首先修改两台服务器的端口,vim /etc/ssh/sshd_config,找到如下部分
#Port 22
将#去除,22改为想要的端口号。这里我将youxi1的ssh端口号改为2890,youxi2的ssh端口号改为2891。
接着使用命令systemctl restart sshd重启服务。再使用netstat -tlunp | grep sshd查看端口号(如果没有netstat请安装net-tools)
[root@youxi1 Packages]# netstat -tlunp | grep sshd //youxi1
tcp 0 0 0.0.0.0:2890 0.0.0.0:* LISTEN 9953/sshd
tcp6 0 0 :::2890 :::* LISTEN 9953/sshd
[root@youxi2 ~]# netstat -tlunp | grep sshd //youxi2
tcp 0 0 0.0.0.0:2891 0.0.0.0:* LISTEN 17526/sshd
tcp6 0 0 :::2891 :::* LISTEN 17526/sshd
1)单向免密登录
youxi1使用ssh远程youxi2不需要密码,但youxi2使用ssh远程youxi1需要密码
在yousi1上使用ssh-keygen生成公钥和私钥(这里使用默认的rsa),一路默认即可
[root@youxi1 ~]# ssh-keygen -t rsa //默认指定的是rsa,所以可以没有-t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //选项没有指定生成地址时,此处也可以指定
Created directory '/root/.ssh'.
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:ia+le9ZX3cAxztmIINJbWnEGrK9lq4lY4pYNevgqecM root@youxi1
The key's randomart image is:
+---[RSA 2048]----+
| . .ooo |
| . o =o o |
| . B . = * |
| .+. . B .|
| . S. o.|
| . . + . o|
| o o.+. o= . . |
|o E.++.=+.o . |
| o.*+ =+o. . |
+----[SHA256]-----+
在没有指定生成地址时,会默认生成到家目录下的.ssh/目录下。使用rsa就会生成id_rsa和id_rsa.pub两个文件,如果使用的是dsa则生成的是id_dsa和id_dsa.pub两个文件。
[root@youxi1 ~]# ls /root/.ssh/
id_rsa id_rsa.pub
接着使用命令ssh-copy-id命令将公钥发到youxi2服务器上
[root@youxi1 ~]# ssh-copy-id -i .ssh/id_rsa.pub -p2891 root@192.168.1.7 //-p选项指定被远程的服务器的端口号
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host '[192.168.1.7]:2891 ([192.168.1.7]:2891)' can't be established.
ECDSA key fingerprint is SHA256:j3ee8eoTo2XEv0QxCYmxphMipcNRxC+IONPmt1HwRLg.
ECDSA key fingerprint is MD5:25:e2:b4:08:f2:79:7d:6e:42:84:b5:78:3d:6a:81:20.
Are you sure you want to continue connecting (yes/no)? yes //yes继续
/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
root@192.168.1.7's password: //输入192.168.1.7服务器上的root用户的密码 Number of key(s) added: 1 Now try logging into the machine, with: "ssh -p '2891' 'root@192.168.1.7'"
and check to make sure that only the key(s) you wanted were added.
公钥传完后虽然会在本地生成.ssh/known_hosts文件,但并不生效。而在youxi2服务器的root用户的家目录下生成.ssh目录,并含有authorized_keys文件。
[root@youxi1 ~]# ls .ssh/
authorized_keys
此时youxi1上的id_rsa.pub文件与youxi2是上的authorized_keys文件相同。
最后测试:在youxi1上ssh远程youxi2,会发现并不需要输入密码
[root@youxi1 ~]# ssh -p 2891 root@192.168.1.7
Last login: Sun May 12 17:46:49 2019 from youxi1.cn
[root@youxi2 ~]# ls .ssh/
authorized_keys
注意:是本机生成的公钥发给被远程的服务器,在发送公钥和远程服务器时,都需要指定被远程的服务器的端口号。
2)双向免密登录
双向免密就是互换公钥即可,这里接着上面把youxi2的公钥发送到youxi1上,并进行测试。
[root@youxi2 ~]# ssh-keygen
Generating 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:9+woxNPvkE99zGUEZNcI+DJaUUIZXXMKb7k/Y6kPiJU root@youxi2
The key's randomart image is:
+---[RSA 2048]----+
| .+*++*.+|
| +..+.B.|
| o = .|
| + o. o |
| .S+.E . o|
| =.++.. =o|
| . ooo+..==|
| . *. +.o|
| ...+... |
+----[SHA256]-----+
[root@youxi2 ~]# ssh-copy-id -i .ssh/id_rsa.pub -p2890 root@192.168.1.6
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
The authenticity of host '[192.168.1.6]:2890 ([192.168.1.6]:2890)' can't be established.
ECDSA key fingerprint is SHA256:j3ee8eoTo2XEv0QxCYmxphMipcNRxC+IONPmt1HwRLg.
ECDSA key fingerprint is MD5:25:e2:b4:08:f2:79:7d:6e:42:84:b5:78:3d:6a:81:20.
Are you sure you want to continue connecting (yes/no)? yes
/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
root@192.168.1.6's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh -p '2890' 'root@192.168.1.6'"
and check to make sure that only the key(s) you wanted were added. [root@youxi2 ~]# ssh -p 2890 root@192.168.1.6
Last login: Sun May 12 17:24:54 2019 from youxi2.cn
[root@youxi1 ~]#
CentOS7做ssh免密登录的更多相关文章
- Centos7配置ssh免密登录群发
ssh免密登录是客户端发送自己的公钥到服务器.用公钥进行解密,自己生成的私钥进行加密. 首先在客户端查看sshd服务是否启动 [zhiwei@zhiwei1 ~]$ ps -Af|grep sshd; ...
- [CentOS7] ssh免密登录 scp免密传输
我们采用RSA非对称加密算法,原理: 如果,A要和B通讯,则: (1). A通过RSA算法生成公钥(.pub)和私钥(公钥用于加密,私钥用于解密) (2). B将A的公钥文件(.pub)内容加入到au ...
- centos7:ssh免密登陆设置及常见错误
目录 一.免密登录设置 二.常见错误 三.CentOS7再ssh-copy-id时的错误 一.免密登录设置 1.使用root用户登录,进入到目录/root/.ssh 2.执行命令:ssh-keygen ...
- Ubuntu如何配置SSH免密登录
前言 在搭建hadoop集群时,需要主机和副机之间实现SSH免密登录 一.环境准备 1.ubuntu两台 二.安装SSH 1.首先检测一下本机有没有安装SSH服务,如果没有任何打印说明未安装 sudo ...
- linux上ssh免密登录原理及实现
因为我的服务器集群需要回收日志到中央进行统一处理,所以需要建立ssh互信关系实现免密登录.关于ssh的使用大家可能都很熟悉了,我们今天主要来讲下ssh连接和免密登录的原理. scp 传输文件 scp( ...
- Linux的SSH免密登录认证过程研究
一.先看下SSH免密登录使用到的工具和生成的文件 工具:ssh-keygen用于生成秘钥文件,其中秘钥分为公钥和私钥.ssh-copy-id用于复制公钥文件到被控制机. 文件:ssh-keygen生成 ...
- SSH(安全协议外壳)介绍及Linux SSH免密登录
SSH(安全外壳协议) SSH 为 Secure Shell 的缩写,是一种网络安全协议,专为远程登录会话和其他网络服务提供安全性的协议.通过使用 SSH,可以把传输的数据进行加密,有效防止远程管理过 ...
- Shell脚本实现SSH免密登录及批量配置管理
本节索引 场景分析 ssh免密登录 pssh工具批量管理 SHELL自动化脚本 本篇总结 场景分析 作为一个运维工程师,不是每个人工作的环境都想阿里.腾讯那样,动不动就上亿的PV量,上万台服务器.我们 ...
- Ubuntu ssh免密登录
ssh免密登录工作原理 server A免登录到server B: 1.在A上生成公钥私钥. 2.将公钥拷贝给server B,要重命名成authorized_keys(从英文名就知道含义了) 3.S ...
随机推荐
- Windows与Linux之间文件传输
(1).使用WinSCP工具,实现将Windows的文件上传到Linux指定目录下 (1).输入主机名.用户名.密码,选择登录,成功连接至Linux系统 (2).在左侧列表,选择要上传文件,单击右键选 ...
- YOLO---YOLOv3 with OpenCV 再使用
YOLO---YOLOv3 with OpenCV 再使用YOLOv3 with OpenCV官网 @ https://github.com/JackKoLing/opencv_deeplearnin ...
- 1118 DOM
目录 BOM与DOM window对象 navigator对象(了解即可) screen对象(了解即可) history对象(了解即可) location对象 弹出框 计时相关 DOM对象 DOM结构 ...
- css选择器学习(二)属性选择器
属性选择器 /*******************************************css2中的属性选择器*************************************** ...
- Maven+Docker 部署
Maven+Docker 部署 安装jdk8镜像 docker pull openjdk:8-jdk-alpine maven插件推送方式 修改/etc/docker/daemon.json文件,加入 ...
- Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】
题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...
- keydown([[data],fn]) 当键盘或按钮被按下时,发生 keydown 事件。
keydown([[data],fn]) 概述 当键盘或按钮被按下时,发生 keydown 事件. 注释:如果在文档元素上进行设置,则无论元素是否获得焦点,该事件都会发生.直线电机滑台 参数 fnFu ...
- ActiveMQ C#实例
重点参考:NMS Documentation 一.ActiveMQ Queue 在ActiveMQ中Queue是一种点对点的消息分发方式,生产者在队列中添加一条消息,然后消费者消费一条消息,这条消息保 ...
- nodejs基础(管道、流)实现:复制、压缩、加密、解压,解密,写入文件
stream流 都是events.EventEmitter的一个实例,都可以来创建自定义事件(也就是说,流是一个事件的实例) 在nodejs中 对http的请求与响应都是用流来实现的,请求就是一个输入 ...
- C语言学习笔记1-数据类型和标识符
http://blog.csdn.net/jadeshu/article/details/50751901 1.数据类型 ---1.1基本类型 --------------数值型(short(2) i ...