如何获取Shell脚本来更改文件权限?

发布于 2025-01-09 06:13:12 字数 2117 浏览 0 评论 0原文

我有一个网页,用户可以在其中上传文件。上传后,网页调用shell脚本将上传的文件移动到另一个位置。我确信“mv”命令由于权限问题而失败,但我不确定如何克服这个问题。

首先要事。我的网页运行在 Ubuntu 16.04 服务器和 Apache2 2.4.41 上。当用户上传文件时,该文件将保存在服务器上的目录 /var/www/html/uploads 中:

me@myServer:/var/www/html/uploads$ ls -l
total 44
-rw-r--r-- 1 www-data www-data   761 Feb 21 15:38 UsersUploadedFile.txt
me@myServer:/var/www/html/uploads$

我注意到该文件归用户 www-data 所有>。

文件上传后,网页会调用此 shell 脚本将文件移动到另一个目录:

#!/bin/bash
echo "Attempting to move the uploaded file..."
{
        mv /var/www/html/uploads/UsersUploadedFile.txt  /home/me/UsersUploadedFile.txt
} || {
   echo "Gah, failed to move the file!"
}

当文件上传时,您在我的网页上看到以下内容:

Attempting to move the uploaded file...
Gah, failed to move the file!

因此“mv”命令失败。

我的第一直觉是这是一个权限问题。正如我之前提到的,要移动的文件由用户 www-data 所有。 shell 脚本也是如此:

me@myServer:/var/www/html$ ls -l
total 36
-rwxr-xr-x 1 www-data www-data  593 Feb 21 15:53 moveTheFile.sh
me@myServer:/var/www/html$

但是我想要移动文件的目录由用户 me 所有,并且由于其他脚本原因我无法更改它。

我想我想做的是让 shell 脚本将 UsersUploadedFile.txt 的所有权更改为用户 me,然后移动文件。但是,如果我在 shell 脚本中插入 chown 命令,该命令也会失败。

看起来用户 www-data 正在运行 apache2 服务,因此也运行 shell 脚本...?我不知道。

me@myServer:/var/www/html$ ps -ef | grep apache
root      14931      1  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14934  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14935  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14936  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14937  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14938  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14942  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
ph9821    15165  14831  0 16:03 pts/2    00:00:00 grep --color=auto apache
me@myServer:/var/www/html$

那么我在这里可能做错了什么?以及如何获取 shell 脚本来更改文件权限?我想这就是我真正需要在这里完成的事情。谢谢。

I have a webpage where users can upload a file. After the upload, the webpage calls a shell script to move the uploaded file to another location. I'm certain the "mv" command is failing because of a permissions issue, but I'm uncertain how to overcome this.

First things first. My webpage runs on an Ubuntu 16.04 server and Apache2 2.4.41. When the user uploads a file, that file is saved on the server in directory /var/www/html/uploads:

me@myServer:/var/www/html/uploads$ ls -l
total 44
-rw-r--r-- 1 www-data www-data   761 Feb 21 15:38 UsersUploadedFile.txt
me@myServer:/var/www/html/uploads$

I notice that the file is owned by user www-data.

Once the file is uploaded, the webpage calls this shell script to move the file to another directory:

#!/bin/bash
echo "Attempting to move the uploaded file..."
{
        mv /var/www/html/uploads/UsersUploadedFile.txt  /home/me/UsersUploadedFile.txt
} || {
   echo "Gah, failed to move the file!"
}

When the file is uploaded, you see this on my webpage:

Attempting to move the uploaded file...
Gah, failed to move the file!

So the "mv" command is failing.

My first instinct was that this was a permissions issue. The file-to-be-moved is owned by user www-data, as I mentioned before. The shell script is too:

me@myServer:/var/www/html$ ls -l
total 36
-rwxr-xr-x 1 www-data www-data  593 Feb 21 15:53 moveTheFile.sh
me@myServer:/var/www/html$

But the directory where I want to file to be moved is owned by user me, and I can't change that for other scripting reasons.

What I guess I'd like to do is to is have the shell script change UsersUploadedFile.txt 's ownership to user me, and then move the file. But if I insert a chown command into the shell script, that command fails, too.

It looks like user www-data is running the apache2 service, therefore also running the shell script...? I'm not sure.

me@myServer:/var/www/html$ ps -ef | grep apache
root      14931      1  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14934  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14935  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14936  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14937  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14938  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
www-data  14942  14931  0 13:00 ?        00:00:00 /usr/sbin/apache2 -k start
ph9821    15165  14831  0 16:03 pts/2    00:00:00 grep --color=auto apache
me@myServer:/var/www/html$

So what might I be doing wrong here? And how can I get the shell script to change file permissions? I guess that's what I really need done here. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧梦荧光笔 2025-01-16 06:13:12

这是一个权限问题。用户 www-data 无法在您的主目录中写入文件。

假设您的主目录由用户“me”和组“me”拥有,您必须在主目录上设置权限,以便您的组可以写入它(它应该已经)。然后在该组中添加用户 www-data。

重要说明

如果您在主目录上设置了权限 777,那么系统上的每个用户都可以写入该目录!您应该输入 775 或 770。只有您和您的小组应该能够写入(也可以读取,具体取决于您的要求)。如果这是您自己的计算机,则并不重要;如果这是具有许多用户的计算机,则至关重要。

记住权限是:用户组其他。 777 是所有 3 个的 rwx。这可能很危险。

This is a permission issue. User www-data cannot write a file in your home directory.

Assuming your home directory is owned by user "me" and group "me", you will have to set permissions on your home directory so your group can write into it (it should already). Then add user www-data in that group.

Important note

If you put permission 777 on your home directory, then every user on the system can write into that directory! You should put 775 or 770. Only you and your group should be able to write (and maybe read also, depending on your requirement). Not important if this is your own machine, critical if this is a machine with many users.

Remember permissions are: user group others. 777 is rwx for all 3. It can be dangerous.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文