通过 SSH 将远程文件同步到我的本地机器,在本地使用 sudo 权限和我的个人 SSH 密钥 [英] rsync remote files over SSH to my local machine, using sudo privileges on local side, and my personal SSH key

查看:61
本文介绍了通过 SSH 将远程文件同步到我的本地机器,在本地使用 sudo 权限和我的个人 SSH 密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将远程机器上的目录 /var/sites/example.net/ 同步到本地机器上相同路径的目录.

I want to sync a directory /var/sites/example.net/ from a remote machine to a directory at the same path on my local machine.

远程机器仅使用密钥而不是密码来验证 SSH 连接.

The remote machine only authenticates SSH connections with keys, not passwords.

在我的本地机器上,我在 ~/.ssh/config 中设置了一个别名,以便我可以轻松地运行 ssh myserver 进入.

On my local machine I have an alias set up in ~/.ssh/config so that I can easily run ssh myserver to get in.

我正在尝试 rsync -a myserver:/var/sites/example.net//var/sites/example.net/ 但它失败了,因为我的本地用户没有编辑权限本地目录/var/sites/example.net/.

I'm trying rsync -a myserver:/var/sites/example.net/ /var/sites/example.net/ but it fails because my local user does not have permission to edit the local directory /var/sites/example.net/.

如果我尝试 sudo rsync -a myserver:/var/sites/example.net//var/sites/example.net/(只需添加 sudo),我就可以解决本地权限问题,但后来我遇到了一个不同的问题——我的本地 root 用户没有看到正确的 ssh 密钥或 ssh 别名.

If I try sudo rsync -a myserver:/var/sites/example.net/ /var/sites/example.net/ (just adding sudo), I can fix the local permission issue, but then I encounter a different issue -- my local root user does not see the proper ssh key or ssh alias.

有没有办法通过修改这个 rsync 命令来完成这个文件同步?我想避免更改任何其他内容(例如,不更改文件权限或 ssh 设置)

Is there a way I can accomplish this file sync by modifying this rsync command? I'd like to avoid changing anything else (e.g. no changes to file perms or ssh setup)

推荐答案

试试这个:

sudo rsync -e "sudo -u localuser ssh" -a myserver:/var/sites/example.net/ /var/sites/example.net/

这会以 root 身份运行 rsync,但 -e 标志会导致 rsync 以本地身份运行 ssh用户(使用 sudo -u localuser),所以 ssh 命令可以访问必要的凭据.Rsync 本身仍以 root 身份运行,因此它具有必要的文件系统权限.

This runs rsync as root, but the -e flag causes rsync to run ssh as your local user (using sudo -u localuser), so the ssh command has access to the necessary credentials. Rsync itself is still running as root, so it has the necessary filesystem permissions.

这篇关于通过 SSH 将远程文件同步到我的本地机器,在本地使用 sudo 权限和我的个人 SSH 密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆