bash 以 root 身份运行 rsync:不允许操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30671292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:08:29  来源:igfitidea点击:

Running rsync as root: Operations Not Permitted

macosbashscriptingpermissionsbackup

提问by Tan

World!

世界!

I have a backup script that would run rsyncfor every user and will archive their /User/userfolder onto our shared drive. Running into an interesting problem with rsync: when executing the script as "sudo" from the shell as the current user I'm unable to preserve the permissions of other users. It will error out and say: rsync: chown <path> - operations not permitted(1)

我有一个备份脚本,可以rsync为每个用户运行,并将他们的/User/user文件夹存档到我们的共享驱动器上。遇到一个有趣的问题rsync:当作为当前用户从 shell 以“sudo”执行脚本时,我无法保留其他用户的权限。它会出错并说:rsync: chown <path> - operations not permitted(1)

Do I have to chownevery user's folder as root before executing the command? The command in being used and in question:

chown在执行命令之前,我是否必须以root 身份访问每个用户的文件夹?正在使用和有问题的命令:

/usr/bin/rsync -av --human-readable --progress /Users/$name/ --exclude=".*" --exclude="Public" /Backup/$name\ -\ $(date +%m-%d-%y)  --exclude=".*/"

Thank you!

谢谢!

回答by Gordon Davisson

It's because the target is on a mounted network volume. You're running rsyncas root, but that only gives you permissions to change ownership on the local (client) computer -- as far as the file server is concerned, you're whatever user authenticated to it, i.e. not root (unless you're using NFS, in which case it's more complicated). Since you're authenticated to the server as a normal user, all files you create on the server will be owned by that normal user, and you won't have permissions to change that.

这是因为目标位于已安装的网络卷上。您rsync以 root 身份运行,但这仅授予您更改本地(客户端)计算机上的所有权的权限——就文件服务器而言,您是对其进行身份验证的任何用户,即不是 root(除非您重新使用 NFS,在这种情况下它更复杂)。由于您以普通用户身份在服务器上进行了身份验证,因此您在服务器上创建的所有文件都归该普通用户所有,您无权更改该文件。

I see two possible solutions:

我看到两种可能的解决方案:

  • Don't try to set ownership on the server. Use rsync -rltgoDv ...instead of rsync -av .... -ais equivalent to -rlptgoD; leaving off the -pmeans it won't try to set ownership on the target files, it'll just store them all as the current (authenticated to server) user.

  • If you really need to preserve ownership (and your local accounts are the same as those on the server), run rsync over SSH instead of file sharing, and SSH into the server as root. Something like rsync -av ... root@server:/Backup/$name\ -\ $(date +%m-%d-%y) ...

    Note that allowing root SSH into a server is generally a bad idea. If you want to do it this way, I'd create an SSH public/private key pair (with an encrypted private key), use that for authentication, and configure the server to reject password-based SSH authentication (at least for root).

  • 不要尝试在服务器上设置所有权。使用rsync -rltgoDv ...代替rsync -av ...-a相当于-rlptgoD;离开-p意味着它不会尝试设置目标文件的所有权,它只会将它们全部存储为当前(已向服务器验证)用户。

  • 如果您确实需要保留所有权(并且您的本地帐户与服务器上的帐户相同),请通过 SSH 运行 rsync 而不是文件共享,并以 root 身份通过 SSH 进入服务器。就像是rsync -av ... root@server:/Backup/$name\ -\ $(date +%m-%d-%y) ...

    请注意,允许 root SSH 进入服务器通常是一个坏主意。如果你想这样做,我会创建一个 SSH 公钥/私钥对(带有加密的私钥),使用它进行身份验证,并将服务器配置为拒绝基于密码的 SSH 身份验证(至少对于 root) .