linux之间的免密通信原来是这么的简单
何为免密通信,说的大白话就是,我连接你的服务器不需要密码,哈哈,就是所谓的免密通信
今天小编也不讲免密的基本原理了哈,原理的话,百度里面有好多
小编的主要目的呢是,大概的说下怎么实现密码通信
大概怎么实现免密通信,为小编的另一个入侵redis并实现免密通信(还没发布),来打下一个基础。废话不多说,那么我们开始吧
环境
服务器 | 用途 | ip | 系统 |
---|---|---|---|
服务器A | 生成公钥,免密连接服务器B | 192.168.196.128 | centos7 |
服务器B | 接受A服务器的公钥,让A服务器免密连接 | 192.168.196.129 | centos7 |
何为免密通信
密码通信就是A服务器连接B服务器的时候不需要输入密码,简单画个图吧,如下
### 没有免密的效果
[root@bogon ~]# ssh root@192.168.196.129
The authenticity of host '192.168.196.129 (192.168.196.129)' can't be established.
ECDSA key fingerprint is SHA256:o98cQWSKlxj3FYKpIcckFsAsb3+hRJ9w+DQThSbUUks.
ECDSA key fingerprint is MD5:9d:ee:d4:8e:1d:02:be:c9:ba:5f:15:51:99:3a:ed:97.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.196.129' (ECDSA) to the list of known hosts.
root@192.168.196.129's password:
Last login: Sun Nov 26 16:01:19 2023 from 192.168.196.1
[root@bogon ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:e7:5f:14 brd ff:ff:ff:ff:ff:ff
inet 192.168.196.129/24 brd 192.168.196.255 scope global noprefixroute dynamic ens33
valid_lft 1138sec preferred_lft 1138sec
inet6 fe80::fe45:3b37:69fd:9dcd/64 scope link noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::12ce:59d6:ca98:3ebb/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
如何实现免密通信
公钥和私钥的认识
每台服务器上面都有两个钥匙,一个是公钥也就是pub(公共的),一个是私钥,这个私钥可是非常主要的哈,这个私钥可以打开公钥的钥匙,简单这样理解吧。
或者这样说,哪个服务器上面有我的公钥,我就可以拿着自己的私钥就连接上他,还是免密的呦,这个就是免密通信的最终实现方法
那么这些公钥怎么生成的呢,在哪个目录下面呢,接着往下看
生成公钥和私钥
生成自己的公钥
[root@bogon ~]# ssh-keygen -t dsa ##下面一路回车就行a'S
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:/dQIhle46c4ZbbwP/G6Ehobq+sNj7JBo9sTtS63V7uo root@bogon
The key's randomart image is:
+---[DSA 1024]----+
| .. |
| ... |
| . +o |
| +o. o |
| So.+o.. |
| o o ...=+* . |
| + =oo.o+.=+o |
| o o +Bo .+ .o. |
| o*B=Eoo ++ |
+----[SHA256]-----+
您在 /var/spool/mail/root 中有新邮件
[root@bogon ~]#
实现免密
方法1,命令推送公钥
使用ssh-copy-id root@192.168.196.129
来实现推送公钥
[root@bogon ~]# ssh-copy-id root@192.168.196.129
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
The authenticity of host '192.168.196.129 (192.168.196.129)' can't be established.
ECDSA key fingerprint is SHA256:o98cQWSKlxj3FYKpIcckFsAsb3+hRJ9w+DQThSbUUks.
ECDSA key fingerprint is MD5:9d:ee:d4:8e:1d:02:be:c9:ba:5f:15:51:99:3a:ed:97.
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.196.129's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.196.129'"
and check to make sure that only the key(s) you wanted were added.
方法2 直接粘贴复制公钥
说白了就是想办法将自己的公钥放到对面服务器的/root/.ssh/authorized_keys这个里面,这个文件里面存放着所有可以免密登录这台服务器的公钥
A机器上的公钥
B机器上的authorized_keys
大家不知道发现了什么密码了没有
其实很简单,就是把A服务器的公钥追加到B服务器的authorized_keys文件里面就行,什么,什么是追加,好吧,就是在authorized_keys文件内容的下面新起一行,输入A服务器的公钥就行。
小编在管理大批量服务器的时候就是这样复制过去的,小编知道可以,这边就不演示了,还请大家自己测试下吧
结束语
小编没有介绍的那么细致,当然小编也只是为了配合小编将要写的一个入侵redis并实现免密登录被入侵服务的一个文档,当然文章还没有写,不过马上就要写了。
还有就是上面的公钥,如果你复制到了你的服务器里面,那小编就可以用小编自己的私钥来实现免密连接你了哈