密钥对及sftp文件接口
Posted 航松先生
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了密钥对及sftp文件接口相关的知识,希望对你有一定的参考价值。
写作背景:工作中,跨主机的文件传输,如果需要定时任务执行,需要解决后台执行中取消人机交互过程的动作,只能借助于密钥对。
示例:从132.**.**.36 访问 132.**.**.37
密钥对是两台主机之间设置信任,能够免密码登录,避免人机交互过程。
1、进入客户端(132.**.**.36)的路径,在系统路径下的.ssh下存放秘钥配置
webusr@iomtimer1:/home/webgrp >cd $HOME
webusr@iomtimer1:/home/webgrp >ls -al
-rw------- 1 webusr webusr 9448 May 22 11:11 .sh_history
drwx------ 2 webusr webusr 256 Nov 15 2017 .ssh
-rw------- 1 webusr webusr 1364 May 18 11:10 .vi_history
2、在客户端(132.**.**.36)的.ssh下运行ssh-keygen -t rsa,生成公钥id_rsa.pub(如果文件已经存在的就别执行了直接copy,执行会导致文件内容重新生成,之前设置的密钥全部失效)
webusr@iomtimer1:/home/webgrp/.ssh >cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDS1N/PlyKiSY0fRwyvcNc9lEGOCHNebyqOYk0tA2VU4tHLe1qzhmqAmGm++TWRSxQVu68LYyCCnFphIwKPf+lfpOMuNQ3ydJxzLPUI9nPqoRQB6vCY+l4GPXfTM0/KAJroI3eyD+YPjKaTRfUjGtFX4s5bAnJ5qSvod5bz/PvHZ0rVjQLsdu3bWohdGd0EsMK+jmnKIdHfmAi3Bf5f57dYBVD0ilBUWse9ie7rd7DdZEajRWTY16Hw4fYZxZORNi9DXNqOUl7cEE7FCiZOSea9kBxQ4F+WSbpaPHWtDqxwVzHAYFQEyCKXakLHmnjLDrIZp6hY2VxQQqxaPDN6DqR3 webusr@iomtimer1
3、【从客户端(132.**.**.36)获取id_rsa.pub】放到服务端(132.**.**.37)cat $HOME/.ssh/authorized_keys
4、在客户端(132.**.**.36)验证sftp webusr@132.**.**.37 date ,第一次执行需要输入密码
webusr@iomtimer1:/home/webgrp/.ssh >sftp webusr@132.**.**.37 date
Connected to 132.77.255.37.
sftp>
5、在客户端(132.**.**.36)sftp登录
webusr@iomtimer1:/home/webgrp/.ssh >sftp webusr@132.**.**.37
Connected to 132.**.**.37.
sftp>
6、第一次从客户端登录并输入密码,客户端默认会自动生成一个私钥,存放在cd $HOME/.ssh下的known_hosts文件中
efile92@iomitfdb2:/interface/electronic_files/data/.ssh >cat known_hosts
#132.77.74.109 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA1ox85RlwxHugD6gGRjEO4ztW0iDpNoaSnaO/dGD6zpWQ+H8ifhILrMI/qKvyqjGvM27nTXDzpr29tcZezvWCSe2FxxQ30kI+2TQtE9xtur4jv9KEThFMjHkfIdQ/Laqu3EVnTwSyttvKORaALTY5ROBJv2gczlsvdiyNCy8+I763yEV8jf81nk5dT1P/6kxMEyipk7iEzzvcSrTQqljk/J12it0bS5B7D+z3xxbiMlzHQ6bMFml8JxP/ehjvUSLuf30L/50QVOZ9nB1ybv+oRfR2gHIPIeVoC3cx/bGvHkBrSJK/pNPslB/cI5A8mcFJ1hYAb0jYXzgosZ5JdLc0cQ==
132.77.138.210 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBMiWbsK16NfqppENSJ9Sk/Z8S7vPgZgPxvD7fvB35xlTfnt1rImemEUzbGIyDdNAnF8x/Y4vzf+wZ/TSqt/lvEs=
efile92@iomitfdb2:/interface/electronic_files/data/.ssh >
如果这里面有对应的主机信息,且私钥或公钥不对,都无法用sftp登录,会报错:Host key verification failed.
7、注意事项
1)在shell脚本中使用sftp的免密码访问时,shell脚本的权限必须不能为777,否则不能登录,建议750或700
2)两台主机网络要联通
3)客户端要具有访问服务端的sftp权限
4)每台主机的密钥信息可以重复生成,但是每次生成都不一样,重新生成后,之前所有设置全部失效,需要重新配置处理。
5)传输文件样例: scp test.txt ftpcrm@132.77.134.14:/crmftp。
8、客户端通过sftp访问服务端连接处理慢
1)步骤一:连接慢的主要原因是DNS解析导致
解决方法:
最为常见的原因是因为server的sshd会去DNS查找访问client IP的hostname,如果DNS不可用或者没有相关记录,就会耗费大量时间。
1、在server上/etc/hosts文件中把你本机的IP和hostname加入 -----常见原因,这里配置客户端的VIP地址修改为实地址即可解决。
2、在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:
UseDNS no
# GSSAPI options
GSSAPIAuthentication no
GSSAPIAuthentication参数是用于Kerberos验证的,而对于绝大多数人来说,不可能使用这种验证机制的,所以要注意把他们停掉。
然后,执行/etc/init.d/sshd restart重启sshd进程使上述配置生效,在连接一般就不慢了。
2)步骤二:
如果还慢的话,检查ssh服务端上/etc/hosts文件中,127.0.0.1对应的主机名是否和
uname -n的结果一样,或者把本机ip和hostname(uname -n结果)加入到/etc/hosts里。
3)步骤三:
利用ssh-v的调试功能查找慢的原因
其实可以用下面的命令调试为什么慢的细节(学习这个思路很重要)。
[root@localhost ~]# ssh -v root@192.168.2.15
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.2.15 [192.168.2.15] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH_4*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
The authenticity of host \'192.168.2.15 (192.168.2.15)\' can\'t be established.
RSA key fingerprint is ca:18:42:76:0e:5a:1c:7d:ef:fc:24:75:80:11:ad:f9.
Are you sure you want to continue connecting (yes/no)? yes
=======>这里就是提示保存密钥的交互提示。
Warning: Permanently added \'192.168.2.15\' (RSA) to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/identity
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Next authentication method: password
root@192.168.2.15\'s password:
=======>这里就是提示输入密码的交互提示。
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Last login: Tue Sep 24 10:30:02 2013 from 192.168.2.13
在远程连接时如果慢就可以确定卡在哪了。
[root@localhost ~]# ssh -v oldboy@192.168.2.18
OpenSSH_5.3p1, OpenSSL 1.0.0-fips 29 Mar 2010
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.2.18 [192.168.2.18] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_dsa type 2
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host \'192.168.2.18\' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
上述配置没配就发现卡到gssapi这。就大概知道是gssapi的问题。实际上在Linux系统优化部分就应该优化SSH服务的此处。
以上是关于密钥对及sftp文件接口的主要内容,如果未能解决你的问题,请参考以下文章
[dotnet] 封装一个同时支持密码/安全密钥认证的SFTP下载器,简单易用。
cmd窗口使用sftp命令非密钥和密钥登录SFTP服务器的两种方式
Apache sshd-sftp 0.11.0:新主机密钥文件导致 ssh_dispatch_run_fatal:签名不正确