如何使用SSH文件传输协议将文件从本地计算机复制到服务器?
我有一台本地使用的 Ubuntu 9.10 台式机。我正在托管提供商上设置服务器。该服务器将运行 Ubuntu 服务器 LTS 的最小版本(仅 LAMP 和电子邮件服务器,没有 GUI)。
我想编写一个脚本(安排为 cron 作业),它允许我将本地文件上传到服务器上。出于安全原因,我想使用 [SFTP][1]。
我是 shell 脚本的新手 - 但我想 shell 脚本是执行此操作的方法(除非我弄错了)。
任何人都可以向我提供有关如何编写此类脚本的初始指示,以便安全地将本地文件上传到服务器吗?
理想情况下,我想在传输之前压缩文件(以节省带宽)
[1]:http://SSH 文件传输协议
I have an Ubuntu 9.10 desktop machine which I use locally. I am setting up a server on a hosting provider. The server will run a very minimal version of Ubuntu server LTS (only LAMP and email server no GUI).
I want to write a script (scheduled as a cron job) that will allow me to upload local files onto the server. I want to use [SFTP][1], for security reasons.
I am new to shell scripting - but I guess shell scripting is the way to do this (unless I am mistaken).
Can anyone provide me with the initial pointers on how to go about writing such a script, in order to SECURELY upload local files to the server?
Ideally, I would like to compress the files before the transfer (to save on bandwidth)
[1]: http://SSH file transfer protocol
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您将其拼写为“SECURE”还是“secure”,我们都无法读懂您的想法并告诉您要防范什么。所以我会给出一个基本的食谱并告诉你它的好处。这可能应该全部转移到超级用户。
现在,您可以创建一个使用 scp 命令执行实际复制的 shell 脚本。从以下位置开始:
这可以防止基本密码间谍活动以及通过 telnet 随机连接到目标主机的情况。如果源系统不安全,它也不安全,我不能保证 ssh 协议的安全性,尽管它确实被广泛使用。
Whether you spell it 'SECURE' or 'secure,' we can't read your mind and tell what you want to secure against. So I'll give a basic recipe and tell you what it's good for. This probably should all move to superuser.
Now, you can make a shell script that uses the scp command to do the actual copies. Start from:
This is secure against basic password spying and against randoms connecting to target-host with telnet. It is not secure if the source system is not secure, and I cannot vouch for the security of ssh protocol, though it certainly is widely used.
使用
scp
。在 cron 作业中使用
scp
的主要问题是:从哪里获取凭据?将密码明文保存并不是一个好主意。更好的想法是在您的计算机上运行一个
ssh-agent
进程。要找到合适的ssh-agent
,您可以运行此脚本:如果脚本成功,您将获得一个可以放入
SSH_AUTH_SOCK
环境中的值运行scp
之前的变量。当您启动客户端时,您应该通过启动 ssh-agent 并运行 ssh-add 来提供您的凭据。
Use
scp
.The main issue with using
scp
in a cron job is this: where do you get your credentials?Putting your password in the clear is not a good idea. A better idea is to have an
ssh-agent
process running on your machine. To find an appropriatessh-agent
you can run this script:If the script succeeds, you get a value you can put into the
SSH_AUTH_SOCK
environment variable before runningscp
.When you bring up the client, you should present your credentials by launching
ssh-agent
and runningssh-add
.