免密登录
ssh可以拿来远程linux主机,通常我们远程主机时都须要输入密码
我们也可以对其免登
SSH免密码登入防止首次须要输入yes
ssh -o stricthostkeychecking=no
expect
expect是交互性很强的脚本语言,可以帮助运维人员实现批量管理成千上百台服务器操作,是一款很实用的批量布署工具!
sshpass+expect解决交互式问题–待验证
sshpasslinux到linux
sshpass是一款手动为ssh填充密码的开源插件,并没有对ssh做任何更改,是从外部进行的操作。sshpass实现比较简单,而且十分易用,原理与expect类似
仅用于linux
1、本地主机(操作系统为centos,IP为192.168.0.12)下载sshpass
yum install sshpass
使用方式
$ sshpass -p密码 ssh 要连接的主机名@要连接的IP地址
2、连接到远程主机192.168.0.36:
$ sshpass -p1 ssh oceanstar@192.168.0.36
Last login: Fri Dec 11 15:19:16 2020 from 192.168.0.12
ps:其他示例
实例1:直接远程联接某台主机:
命令:sshpass-p‘密码’sshroot@192.168.11.11【默认为22端口】
实例2:远程联接指定ssh的端口:
命令:sshpass-p‘密码’ssh-p1000root@192.168.11.11(当远程主机不是默认的22端口时侯)
实例3:从密码文件读取文件内容作为密码去远程联接主机
命令:sshpass-fxxx.txtsshroot@192.168.11.11
实例4:从远程主机上拉取文件到本地
命令:sshpass-p‘123456’scproot@host_ip:/home/test/t./tmp/
示例5:ssh也可以自己连自己
命令:sshpass-p123xhysshoceanstar@localhost
ssh-key-genlinux到linux
节点1:192.168.0.12
节点2:192.168.0.36
1、192.168.0.12终端使用ssh链接访问
$ ssh root@192.168.0.36
The authenticity of host '192.168.0.36 (192.168.0.36)' can't be established.
ECDSA key fingerprint is SHA256:6Mv/0RDHCNt0wGjPAWG6Pq5Xfr9XVMJE8ocgD+YLiF4.
ECDSA key fingerprint is MD5:43:03:84:00:01:6a:67:8e:ec:6f:a0:63:a2:fe:48:5e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.36' (ECDSA) to the list of known hosts.
root@192.168.0.36's password:
Last login: Sun Nov 29 14:23:14 2020
从前面我们可以看出默认是须要使用密码登入的嵌入式linux培训,这么假如我们想要免密登录,看下边
2、在192.168.0.12上使用ssh-key-gen命令生成私钥和公钥
$ ssh-keygen
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:
45:38:a0:c2:be:de:aa:0a:a6:8d:1a:d6:09:48:5b:2a root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
| .. .. |
| . . o. |
| .o.. .. |
|o.+. . |
|E+. S |
|. o.. |
|oo.o |
|== . |
|O.+.. |
+-----------------+
3、将里面生成的秘钥复制用ssh-copy-id命令将私钥复制或上传到远程主机,并将身分标示文件追加到节点2的~/.ssh/authorized_keys中:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.0.36
The authenticity of host '192.168.0.36 (192.168.0.36)' can't be established.
ECDSA key fingerprint is 43:03:84:00:01:6a:67:8e:ec:6f:a0:63:a2:fe:48:5e.
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.0.36's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.0.36'"
and check to make sure that only the key(s) you wanted were added.
4、接出来就可以免密登录了
# ssh 192.168.0.36
Last login: Fri Dec 11 12:59:46 2020 from 192.168.0.12
window到linux通常可以在cmd或则powershell上可以从windows登入到linux
ssh ip -l用户名 -p端口
并且默认是须要输入密码的
这么怎样在windows上配置免密授权?
2、打开cmd命令行,ssh-keygen-trsa,生成密钥文件,id_rsa(公钥)、id_rsa.pub(私钥)
3、将生成的私钥使用scp命令传输到Linux服务器的.ssh文件夹
scp ./id_rsa.pub 远程服务器用户名@IP:~/.ssh/windows_ras.pub
我们来看一下有没有传输成功
4、在Linuxshell中执行命令:catid_dsa.pub>>~/.ssh/authorized_keys
5、然后我们就可以免密登录了(假如不想,就执行一下chmod600~/.ssh/authorized_keys)
scp是securecopy的缩写,用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器linux windows 文件传输,并且scp传输是加密的。可能会稍为影响一下速率。当你服务器硬碟变为只读readonlysystem时,用scp可以帮你把文件移下来。另外,scp还特别不占资源,不会提升多少系统负荷,在这一点上linux windows 文件传输,rsync就远远不及它了。其实rsync比scp会快一点,但当小文件诸多的情况下,rsync会造成硬碟I/O特别高红旗linux桌面版,而scp基本不影响系统正常使用。
免密上传文件
有时侯须要把Windows中的文件传给Linux,或则把Linux中的文件传给Windows,用Xftp就可以传输,并且某日发觉Xftp不能用了,或则没有安装Xftp的话,在命令窗口中使用scp命令传输文件也是一个不错的选择
windows—>linux输入密码上传文件
1、尝试先ssh联接到linux,之后使用rz命令上传文件
从里面可以看出,除非使用XShell等工具,否则是难以rz上传命令的
假如我们想要将windows上的文件上传到linux服务器中,可以使用scp命令
在文件所在目录下shift+右键打开powershell窗口,输入命令
scp -p 端口 路径/文件名 远程用户名@远程IP:远程绝对路径
scp -P 端口 路径/文件名 远程用户名@远程IP:远程绝对路径+重命名文件 //如果我们想要重命名上传的文件,使用大写的P
事例:
相对路径
scp .backup-platform.zip root@192.168.1.6:/opt/
绝对路径
scp E:Scp_filebackup-platform.zip root@192.168.1.6:/opt/
scp -P 22 F:/MyCode/QSshT/QSshTest/build-QSshTest-Desktop_Qt_5_11_2_MinGW_32bit-Debug/a.txt root@192.168.0.12:/opt/b.txt
注意:
(1)Windows中的文件须要置于没有英文的目录下,否则会报错./backup-platform:Nosuchfileordirectory
(2)Windows中的传输的是文件夹的话须要打包成压缩包再发送,否则会报错./backup-platform:notaregularfile
(3)确保XShell才能连上,否则会出现ssh:connecttohost192.168.0.106port22:Connectiontimedout错误
免密上传文件
ps:推荐这个“echoy|pscp.exe-pw123xhyC:/Users/admin/Downloads/boguansdk.ziproot@192.168.0.24:/home/oceanstar/workspace/”,下边的不用看了
ssh-key-gen
操作和window免密登录ssh操作一样。
在Windows中下载文件到Linux
在Linux中复制文件到Windows
相对路径
scp sql_bak.sql root@192.168.1.4:e:Scp_file
绝对路径
scp /opt/sql_bak.sql root@192.168.1.4:e:Scp_file
注意:由于windows系统本身不支持ssh合同,所以,要想这个命令成功执行,必须在windows顾客端安装sshforwindows的顾客端软件,例如winsshd,使windows系统支持ssh合同才行。
参考了:
怎样在CentOS/RHEL上设置SSH免密码登入