linux - scp 文件没有设置正确的所有者

标签 linux ssh file-permissions scp umask

是 SCP 设置文件权限有问题还是我错误配置了我的服务器?

用例:

我想编辑服务器上的一个名为“importantFile.txt”的文件。该文件的所有者和组为“master”:

ls -l importantFile.txt:
-rw-rw-r--  1 master master     7 Mar 18 08:11 importantFile.txt

我被称为“奴隶”,但幸运的是,我在“主人”组中,所以我可以根据需要编辑文件。然而,我是一个懒惰的奴隶,懒得在服务器上编辑文件,我更愿意在本地机器上编辑文件并将其 SCP 到服务器:

echo "bored slave info" > importantFile.txt
scp importantFile.txt slave@theServerAddress:/pathToFile/importantFile.txt

如果我这样做,服务器上的文件内容会正常上传,文件的时间戳也会更新,但文件的权限不会改变,文件仍归“master”所有。这是一个问题,因为如果“奴隶”上传了不良内容,没有人会知道是“奴隶”造成了问题,“主人”就会感到内疚。

也许我必须设置一个 umask?如果是的话在哪里?我尝试了 .bash_profile 但没有成功,而且在 Google 上也没有在 /etc/ssh/sshd_config 中找到任何关于 umask 的信息。

最佳答案

这对 scp 来说没什么特别的 - 尝试以从属身份登录到服务器,并使用您最喜欢的文本编辑器编辑文件...您会发现发生相同的行为...写入文件不会让您成为文件的所有者。


例子:

作为根

#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile 
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     0 2009-03-19 10:30 testfile
#echo foo > testfile 
#ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     4 2009-03-19 10:30 testfile

作为用户(在 vboxusers 组中)

>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     4 2009-03-19 10:30 testfile
>echo bar >> testfile 
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:29 ..
-rw-rw-r--  1 root vboxusers     8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x  2 root vboxusers  4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root      12288 2009-03-19 10:31 ..
-rw-rw-r--  1 root vboxusers    12 2009-03-19 10:31 testfile
>cat testfile 
foo
bar
baz

关于linux - scp 文件没有设置正确的所有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/658166/

相关文章:

linux - 如何在 Linux 中启动停止的进程

linux - 如何从后台进程 linux shell 脚本中获取结果?

linux - 在可能的值上加载平均

opengl - VNC 终端中的 vtkXOpenGLRenderWindow 错误

c - C中的文件创建权限

apache - 在 Apache 上保护 Wordpress

linux - 用下划线替换空格并小写 - 文件名

c - 从 c 程序在远程主机上执行命令

git - 如何通过 ssh 获取托管在 Azure DevOps Repo 中的 Terraform 模块

Ubuntu:FSL 目录的权限被拒绝