sftp是基于ssh的,服务器在接收到连接请求时,应该会尝试查找客户端的公钥,如果找不到,或者找到的公钥不匹配,就会要求做用户名密码认证
SFTP 使用时不输入密码实现过程:
主要步骤如下:
1.为运行shell脚本的本地用户生成密钥对
2.将其中的公钥分发到sftp欲登录的远程服务器上
3.编写并以上面的本地用户运行shell脚本
一.生成密钥对
在shell脚本中使用sftp时必须用到密钥对(公钥和私钥).可使用下列方式生成(SSH 2.X版本),这里本地用户记为:local_user:
ssh-keygen -t rsa
屏幕提示:
Generating public/private dsa key pair.
Enter file in which to save the key (/home/local_user/.ssh/id_dsa):
# 按回车保存为: /home/local_user/.ssh/id_dsa,即当前用户local_user的私钥
Enter passphrase (empty for no passphrase):
# 按回车,表示读取密钥时不需要密钥的密码
Enter same passphrase again:
# 确认密钥的密码,必须和上面的输入相同
Your identification has been saved in /home/local_user/.ssh/id_dsa.
# 私钥保存信息
Your public key has been saved in /home/local_user/.ssh/id_dsa.pub.
# 公钥保存信息
The key fingerprint is:
ec:41:e8:08:38:0b:f8:1e:bc:92:98:32:fc:d7:69:7d ...
# 密钥指纹
二.分发公钥
为了使用密钥,必须将公钥分发到欲登录的远程服务器上,这里远程服务器记为remote_host,欲登录的远程用户记为remote_user
1.copy公钥到欲登录的远程服务器的远程用户的家目录下,例如:
copy id_dsa.pub到remote_host:/home/remote_user/.ssh/
若目录/home/remote_user/.ssh/不存在,请先创建之.
2.将copy来的公钥文件改名为authorized_keys
3.修改公钥文件的访问权限
chmod 644 authorized_keys
三.示例
目标:把某路径下的COBOL85.pco拷贝成aaaa
#!/bin/bash
sftp ntt12@172.23.16.55 << EOF
put /home/ntt12/work/WWW/COBOL85.pco /home/ntt12/work/WWW/aaaa
quit
[EOF]
本文介绍如何通过生成密钥对实现SFTP无密码登录。包括密钥对生成、公钥分发及shell脚本应用等步骤。
2932

被折叠的 条评论
为什么被折叠?



