赞
踩
连接自己必须先在本地生成自己的公钥和私钥
要连接谁就要把自己的公钥放到谁的服务器上
ssh-keygen
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:ZoNMD/ueg9Tp+gYoQNY7EClt6z1ZX7BuZxm4vXcAXJE root@c7-1-54 The key's randomart image is: +---[RSA 2048]----+ | o+ .o | |.=o. . E | |+.... o = . | |. .o o.=o = | | o ..+++S= + | | o = o+B.= . | | . o =.o . . | | ..+.. . . | | .++. . . | +----[SHA256]-----+
按提示默认生成公钥私钥对在/root/.ssh/目录下
ll -a .ssh/
总用量 24
drwx------. 2 root root 57 8月 11 2020 .
dr-xr-x---. 15 root root 8192 4月 28 22:15 ..
-rw-------. 1 root root 2602 8月 11 2020 id_rsa ## 私钥
-rw-r--r--. 1 root root 570 8月 11 2020 id_rsa.pub ## 公钥
-rw-r--r--. 1 root root 527 3月 29 02:14 known_hosts ## 可识别的主机
ssh-copy-id命令可以把本地主机的公钥复制到远程主机的authorized_keys文件上
ssh-copy-id -i /root/.ssh/id_rsa.pub 服务器对端IP
私钥一定是自己留好,公钥可以发布出去!
ssh-copy-id -i /root/.ssh/id_rsa.pub 10.0.0.56
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.0.0.56 (10.0.0.56)' can't be established.
RSA key fingerprint is SHA256:xiviIpop6NgLGgUH/KasDBNHiO9cKXSZ80wOYrv1PZo.
RSA key fingerprint is MD5:88:b9:65:83:8f:d2:8a:6b:ca:21:07:81:a0:03:04:49.
Are you sure you want to continue connecting (yes/no)? y
Please type 'yes' or '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@10.0.0.56's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '10.0.0.56'"
and check to make sure that only the key(s) you wanted were added.
ssh 10.0.0.56
Last login: Sun Apr 25 23:54:46 2021 from 10.0.0.1
sshpass用于非交互SSH的密码验证,一般用在sh脚本中,无须再次输入密码(本机known_hosts文件中有的主机才能生效)。
sshpass -p 密码 ssh -o StrictHostKeyChecking=no root@10.0.0.1 hostname -I
sshpass -f可以读取文件内的密码
sshpass -f <密码文件> ssh -o StrictHostKeyChecking=no root@10.0.0.1 hostname -I
-p password #后跟密码它允许你用 -p 参数指定明文密码,然后直接登录远程服务器
-f filename #后跟保存密码的文件名,密码是文件内容的第一行
-e #将环境变量SSHPASS作为密码
rsync -av /root/.ssh IP:/root
ssh-keygen -p
ssh-agent bash
ssh-add
输入私钥密码,临时性的密码
这时候私钥文件被偷走了,也没关系
禁用原来的用户名密码策略,仅支持私钥验证
基于key验证与密码无关,修改密码后不影响key验证
sshpass -e #将环境变量SSHPASS作为密码
IPLIST="
10.0.0.8
10.0.0.18
10.0.0.7
10.0.0.6
10.0.0.200"
rpm -q sshpass &> /dev/null || yum -y install sshpass
[ -f /root/.ssh/id_rsa ] || ssh-keygen -f /root/.ssh/id_rsa -P ''
export SSHPASS=centos ##设置系统变量,也就是密码
for IP in $IPLIST;do
sshpass -e ssh-copy-id -o StrictHostKeyChecking=no $IP
done
想访问那台机器,就把自己的公钥拷贝到对方的/root/.ssh/中就行了
部署的100台服务器都想互相访问
只需要每台服务器的/root/.ssh/目录中有一套同样的公私钥对就可以了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。