强制 SFTP/SCP 使用远程目录的权限复制文件

强制 SFTP/SCP 使用远程目录的权限复制文件

我在使用 SFTP 和 SCP 时遇到问题,复制的文件不会继承远程父目录的权限。我在 serverfault 上看到过类似的问题,其中umask修改了 SFTP/SCP 会话的权限,但是,这不一定能解决我的问题,因为某些目录需要具有与其他目录不同的权限。因此,我没有默认设置umask

因此,我想力量复制的文件具有远程系统上父目录设置的权限。基本上,我希望 SCP/SFTP 的工作方式cp没有选项-p。目前 SFTP/SCP 正在模仿cp -p该行为。

以下是我希望发生的事情:

1.)用户想要复制foo.txt具有权限的文件:

-rw-------. 1 用户 user 0 2月29日 09:08 foo.txt

2.)用户使用SCP复制foo.txt到服务器目录下/bar/bar具有权限(设置了setgid):

drwxrws---+ 3 root 用户组 4096 2月28日 12:19 bar

3.)/bar具有以下 facl 设置:

用户::rwx
组::rwx
组:用户组:rwx
默认值:用户::rwx
默认:组::rwx
默认值:组:用户组:rw-

4.)foo.txt 应该具有以下权限(和 facl):

-rw-rw----+ 1 用户用户组 0 2 月 29 日 09:33 foo.txt
用户::rw-
组::rwx #有效: rw-
组:用户组:rw-

5.)相反,foo.txt具有权限:

-rw-------+ 1 用户 用户组 0 2 月 29 日 09:36 foo.txt
用户::rw-
组::rwx #有效:---
组:用户组:rw-#有效:---

有没有简单的方法可以让文件获得上述预期的权限?

另外,我的 facl 是否有意义,或者它们是多余的?

编辑:已修复帖子以使其正确显示。(Serverfault 的代码和编号效果不太好。我需要将内容包装在预标签中。)

答案1

手册页中写道:“当 scp 复制文件时,目标文件将具有某些文件属性。默认情况下,文件权限遵循目标主机上的 umask,修改和最后访问时间将是复制的时间。或者,您可以告诉 scp 复制原始文件的权限和时间戳。-p 选项可实现此目的。”

例如:

sftp user@server:backup <<< $'put -rp mysoftware/mysqldump'

根据以上信息,我不确定不使用 cronjob 设置权限是否可以实现您想要的效果。umask 选项仅适用于正在创建的文件。Setgid 仅适用于组。我相信您可以编写一个以递归方式设置权限的作业,但这就是我能想到的所有会导致您所描述的结果,除非我误解了这个问题。

答案2

通过 ssh 管道传输 cat 命令。这将创建文件(因此它将继承权限),然后它将用内容填充它。例如,要将本地文件 foo 放在 remoteHost 上并继承 setfacl 设置的权限:

ssh user@remoteHost "/bin/cat > /pathOnRemoteServer/foo" < foo

答案3

如果您可以选择使用rsync,则可能对权限有一些额外的控制。特别是,请查看 选项下的文本--perms。手册页如下:

      In summary: to give destination files (both  old  and  new)  the
      source permissions, use --perms.  To give new files the destina-
      tion-default   permissions   (while   leaving   existing   files
      unchanged),  make  sure  that  the --perms option is off and use
      --chmod=ugo=rwX (which ensures  that  all  non-masked  bits  get
      enabled).  If you'd care to make this latter behaviour easier to
      type, you could define a popt alias for it, such as putting this
      line  in  the file ~/.popt (the following defines the -Z option,
      and includes --no-g to use the default group of the  destination
      dir):

         rsync alias -Z --no-p --no-g --chmod=ugo=rwX

      You  could  then  use  this new option in a command such as this
      one:

         rsync -avZ src/ dest/

相关内容