ssh 无秘钥登录
Posted 春天的风情
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssh 无秘钥登录相关的知识,希望对你有一定的参考价值。
三台centos 一台服务器192.168.2.152 一台客户端192.168.2.142 一台客户端192.168.2.151
(1)产生无密钥
[[email protected] ~]# ssh-keygen -t dsa
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:
fa:65:a6:b8:6f:d1:f1:e0:40:2e:56:50:85:24:99:b5 [email protected]
The key‘s randomart image is:
+--[ DSA 1024]----+
| oB+o. |
| o.+. |
| +E |
| o o o |
| . .S+ + |
| .. o . |
| . .+ |
| o.= |
| o++ |
+-----------------+
注意这里要默认敲三下回车
(2)查看产生的密钥 会产生两个文件一个id_dsa是密钥 一个id_dsa.pub是公钥
[[email protected] ~]# ls -l .ssh/
总用量 12
-rw-------. 1 root root 616 3月 28 23:12 authorized_keys
-rw-------. 1 root root 672 3月 29 02:35 id_dsa
-rw-r--r--. 1 root root 616 3月 29 02:35 id_dsa.pub
(3)把公钥发给其他服务器 要输入yes 和对向服务器的密码 这里特别注意如果对方ssh端口不是22,是其他端口用""括起来加-p
例如ssh-copy-id -i .ssh/id_dsa.pub "-p21 [email protected]"
[[email protected] ~]# ssh-copy-id -i .ssh/id_dsa.pub [email protected]
The authenticity of host ‘192.168.2.142 (192.168.2.142)‘ can‘t be established.
RSA key fingerprint is 1c:f0:dd:01:d6:58:5e:e9:07:37:b4:cc:c4:91:59:de.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.2.142‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
(4)无密钥登录
[[email protected] ~]# ssh -p22 [email protected]
Last login: Sun May 6 20:15:39 2018 from 192.168.2.149
[[email protected] ~]# ip a | grep 192.168
inet 192.168.2.142/24 brd 192.168.2.255 scope global dynamic ens33
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
(5)传文件给客户端服务器 142 scp -p22端口 -rp r是目录 p是不改变文件属性 bao目录名字 root用户@ip : /root 存放的路径
[[email protected] ~]# scp -p22 -rp bao/ [email protected]:/root
apache-tomcat-9.0.5.tar.gz 100% 9254KB 9.0MB/s 00:00
grafana-3.1.1-1470047149.x86_64.rpm 100% 39MB 38.7MB/s 00:01
sunlogin_remoteclient_2.2.0.39537Beta.tar.gz 100% 2849KB 2.8MB/s 00:00
zabbix-3.4.7.tar.gz 100% 16MB 16.4MB/s 00:00
nginx-1.13.9.tar.gz 100% 971KB 971.5KB/s 00:00
jdk-8u162-linux-x64.tar.gz 100% 181MB 45.3MB/s 00:04
客户端查看
[[email protected] ~]# cd bao/
[[email protected] bao]# ls
apache-tomcat-9.0.5.tar.gz nginx-1.13.9.tar.gz
grafana-3.1.1-1470047149.x86_64.rpm sunlogin_remoteclient_2.2.0.39537Beta.tar.gz
jdk-8u162-linux-x64.tar.gz zabbix-3.4.7.tar.gz
(6)写到脚本里
给192.168.2.151服务传入公钥
vi ssh.sh
第一种方法
scp -p22 -rp bao/ [email protected]:/root
scp -p22 -rp bao/ [email protected]:/root
第二种方法
for n in 142 151
do
scp -p22 -rp bao/ [email protected]$n:/root
done
sh ssh.sh
[[email protected] ~]# sh ssh.sh
apache-tomcat-9.0.5.tar.gz 100% 9254KB 9.0MB/s 00:00
grafana-3.1.1-1470047149.x86_64.rpm 100% 39MB 38.7MB/s 00:00
sunlogin_remoteclient_2.2.0.39537Beta.tar.gz 100% 2849KB 2.8MB/s 00:00
zabbix-3.4.7.tar.gz 100% 16MB 16.4MB/s 00:00
nginx-1.13.9.tar.gz 100% 971KB 971.5KB/s 00:00
jdk-8u162-linux-x64.tar.gz 100% 181MB 90.5MB/s 00:02
apache-tomcat-9.0.5.tar.gz 100% 9254KB 9.0MB/s 00:00
grafana-3.1.1-1470047149.x86_64.rpm 100% 39MB 38.7MB/s 00:01
sunlogin_remoteclient_2.2.0.39537Beta.tar.gz 100% 2849KB 2.8MB/s 00:00
zabbix-3.4.7.tar.gz 100% 16MB 16.4MB/s 00:00
nginx-1.13.9.tar.gz 100% 971KB 971.5KB/s 00:00
jdk-8u162-linux-x64.tar.gz 100% 181MB 60.3MB/s 00:03
以上是关于ssh 无秘钥登录的主要内容,如果未能解决你的问题,请参考以下文章