大网数据广告活动
服务器运维问答

Execute rsync command from script without prompt for passphrase

· 52CE 我爱测

I use rsync to deploy code to a remote server and currently I have passphrase for the security key. How can I add the passphrase in the rsync command without a prompt to use it in a script? I don't use rsync-daemon. My command: r…

问题原文

原帖链接 · Otto · CC BY-SA 4.0

I use rsync to deploy code to a remote server and currently I have passphrase for the security key. How can I add the passphrase in the rsync command without a prompt to use it in a script?

I don't use rsync-daemon.

My command:

rsync -vaz -e "ssh -i /root/.ssh/key.ppk" --chmod=ugo=rwX --omit-dir-times --no-perms . [email omitted]:/var/www/html

已采纳解答

原帖链接 · Jenny D · CC BY-SA 4.0

Make your security independent of the password of the private key, by instead restricting the usage of the key on the server side:

  1. Create a separate key pair used only for rsync. Make that private key passwordless.
  2. On the server, make sure that you have the script rrsync available. (It will usually be installed with the rsync package, probably in /usr/share/doc/rsync-VERSION/support.
  3. Add the public part of the key to the target user's authorized_keys on the server. At the start of the line, add a load of restriction to enforce that the key can only be used from the correct server and only to rsync things to a specific directory, like this:
command="/path/to/rrsync /var/www/html/",no-pty,no-port-forwarding,no-user-rc,no-X11-forwarding,no-agent-forwarding,from="IPADRESS" ssh-rsa AAA[...]

Now you have a key that can be used only for one thing - to rsync things from one single server to the directory /var/www/html. The restrictions on the serverside mean that even if someone else gets hold of the key, the destruction they can wreak is very limited, and certainly far less than if they get hold of the password to a key that allows full access.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。