建议的目录权限是什么?


145

我正准备部署Drupal 7网站,但找不到任何有关建议将安全意识强的文件和目录权限设置为什么的文档。

具体来说default/files/(也子目录?) settings.php.htaccess以及其他任何我应该知道的。



有一个博客对此进行了详尽的解释,并以抽象的方式涵盖了它的各个方面。technologymythbuster.blogspot.com/2018/06/...
Kalpesh Popat

Answers:


69

您的Web服务器应该能够读取所有文件,但不能写入文件。如果您的站点涉及上载文件,则授予服务器权限仅写该文件夹的权限。

Drupal文档中提供了有关如何进行设置的更多信息,以及如果不进行设置可能会发生的某些事情。


13
对于sites / default / files目录,情况并非如此,该目录必须可由Web服务器写入,否则您会遇到很多麻烦。
rooby

3
@rooby第二句话澄清了这一点。
kenorb 2015年

14
尽管它说“如果您的站点涉及上传文件”,但这并不是您可能需要对files目录进行写访问的唯一原因。js / css聚合是一个非常常见的示例,这是无关的用户上传文件。其他模块也可能期望该目录是可写的。
rooby

91

Drupal的像这么多的页面很长,而且容易混淆。但其中包含杰森(Jason)的这篇帖子,他的想法是:

由Jason Sale发表于2010年11月1日下午12:40

感谢您编写本文以及所有内容,但是我和99%的阅读此页面的人真正想要的只是文件夹列表旁边的数字列表。

  • /default 在755
  • /default/files 包括744(或755)上的所有子文件夹和文件
  • /default/themes 包括755上的所有子文件夹和文件
  • /default/modules 包括755上的所有子文件夹和文件
  • /default/settings.php/default/default.settings.php在444上

7
这个话题冗长而令人困惑。该页面适合实际情况。
greggles 2013年

17
... Drupal文档!这就是为什么需要立即将其更改为简单易懂的原因!特别是在安全性方面。只是因为它与安全有关!因为它属于我们所有人。受感染的服务器不仅是没有经验的Drupal用户的问题。那是我们所有人的问题。因此,保持受开发人员的启发启发而编写的复杂且写得不好的文档来炫耀复杂性和复杂性以及他们自己如何能够很好地处理它并不是很有帮助。我看不出省略简单的文件权限树图形的意义。webchick对此表示赞同。
nilsun 2014年

3
为什么在/ default / files上有755?万一有人通过入侵前端(或通过较差的配置)设法上传恶意内容,这是否应该总是744?有什么理由要755吗?/ tmp呢?
Webdrips

1
settings.php文件应该是440。“其他”应该看不到settings.php。740,如果您希望能够不使用sudo命令对其进行写操作。
布赖登2015年

只是好奇为什么文件和tmp文件夹中的.htaccess文件需要由网络服务器编写?将444标记为settings.php并不安全。我要问的原因是黑客是否可以通过Web服务器将junkfile.php上传到files文件夹,然后可以替换.htaccess文件。我对吗?
kiranking

82

我在服务器上创建新Drupal站点的做法是让用户属于Web服务器(通常为Apache)组,并让该用户拥有所有Drupal文件。在Ubuntu上,以下是用于进行设置的命令:

# Create a new example user, setting up /var/www/example as their home dir.
useradd -s /bin/bash -d /var/www/example -m example

# Now add that user to the Apache group. On Ubuntu/Debian this group is usually
# called www-data, on CentOS it's usually apache.
usermod -a -G www-data example

# Set up a password for this user.
passwd example

设置完成后,我将以该用户身份登录并在/ var / www / example / docroot或类似位置安装Drupal,然后手动创建文件目录并复制settings.php文件。由于我们在复制Drupal之前以示例用户身份登录,因此我们应在所有核心Drupal文件和脚本(包括.htaccess文件)上自动正确配置我们的文件所有权和权限。

su - example
cd docroot
cp sites/default/default.settings.php sites/default/settings.php

# Temporarily give the web server write permissions to settings.php
chgrp www-data sites/default/settings.php
chmod g+w sites/default/settings.php

现在,我们设置文件目录。

# Create the directory.
mkdir sites/default/files

# Now set the group to the Apache group. -R means recursive, and -v means 
# verbose mode.
chgrp -Rv www-data sites/default/files

接下来,我们将设置权限,以便Web服务器始终可以写入此目录中的任何文件。我们通过在chmod命令中使用2775来执行此操作。2表示将为在此​​目录中创建的任何新文件保留组ID。这意味着www.data始终是任何文件上的组,从而确保Web服务器和用户始终对放置在此目录中的任何新文件都具有写权限。前7个表示所有者(示例)可以R(读)W(写)和X(执行)此处的任何文件。第二个7表示组(www-data)也可以读写该目录中的任何文件。最后,5表示其他用户可以R和X文件,但不能写入。

 chmod 2775 sites/default/files

如果此目录中有任何现有文件,请确保Web服务器在其上具有写权限。

 chmod g+w -R sites/default/files

现在,Drupal已准备好安装。完成后,非常重要的是返回settings.php并确保所有用户仅具有读取权限。

 chmod 444 sites/default/settings.php

而已!此设置可确保您避免拥有目录的用户或Web服务器无法在files目录中写入/更改/删除文件的任何情况。


将.htacess设置为444 jsut作为settings.php是不安全的吗?无论如何,该文件很少在drupal核心中更新。
kiranking

您可以将.htaccess设置为444,但是在升级Drupal核心时会增加额外的麻烦,并且不会使您的站点更加安全。
q0rban

30

Drupal文件文件夹应可由Web服务器写入。最安全的方法是更改​​组并使其可写,如下所示:

chgrp www-data sites/default/files
chmod g+w sites/default/files

除了文件上传文件夹,最安全的是所有文件的chmod 644,目录的755。

可以这样完成(在Drupal-site文件夹中运行时,.是用于当前路径):

find . -type f | xargs chmod 644
find . -type d | xargs chmod 755

请记住,您需要chmod g+w在运行上述命令后再次进行设置,因为这些操作将重置所有文件和文件夹上的chmod。


2
该答案假定:您正在使用ubuntu。您的站点是在该服务器上运行的唯一站点。它也只能解决一次该问题,而不是使其自动发生(例如,通过更改umask)。
greggles 2013年

不一定是Ubuntu,即使在同一台服务器上运行多个站点,它仍然是一种有用的措施。对于那些不熟悉Linux的人,我不建议更改umask(希望应该已经具有这些设置)。我知道这不是完美的安全措施,但我们不要让完美成为这里的敌人。
mikl 2015年

在多站点上,我运行chgrp -R www-data sites/*/fileschmod -R g+w sites/*/files摆脱了状态页错误。
leymannx

20

在不知道以下情况下,对“ chmod blah”或“ chown X”的任何建议都是毫无意义的:文件上的默认user:group是什么以及您的Web服务器以什么用户和组运行。

其他人链接到的Drupal Docs在该主题上非常出色,但另一个资源是Security Review Module,它有助于确保正确设置所有内容。


9

考虑到使用FTP在服务器上创建文件的情况,我会回信,使用的凭据与运行Web服务器所使用的凭据不同(通常,Apache以没人/没人的身份运行)。这意味着拥有在运行Drupal安装程序之前手动创建的文件(还包括从Drupal存档上传到服务器上的文件)的用户不是用于运行Web服务器的用户(用户名或组均不匹配) 。这种情况也适用于使用SSH创建这些文件的情况。

  • settings.php文件必须可以从Drupal安装程序中写入,但是一旦安装完成,建议将其设置为只读(安装程序建议,并且Drupal将定期检查该文件是否为只读)。在我描述的场景中,此文件的权限至少应为644。
  • .htaccess文件(至少存在于两个位置)应具有权限644。创建该文件的用户仍应能够覆盖该文件,以防Drupal的下一版本带有一个.htaccess文件,已更新(已经发生过一次(在该文件中添加一行以避免安全问题)。也可以将权限设置为444,但是在这种情况下,当需要更新文件时,应将权限更改回644。
  • 包含由模块创建的文件的default/files目录(该目录)必须是(对于分配给Web服务器进程的用户,然后是分配给该Web服务器上运行的PHP脚本的用户):
    • 可读的
    • 可写的
    • 可以遍历(模块必须能够到达default/files/<directory-used-by-the-module>/<sub-directory-used-by-the-module>

在阅读了drupal.org/node/244924(特别是drupal.org/node/244924#comment-8186447)上的线程之后,没有一种不同的方法提供了如何实现将文件上传到只有660 RW-RW的方式。没有执行位。这不是主要的安全问题吗?
2016年

8

建议的文件/目录权限:

  • Drupal Webroot应该是世界可读的(请参阅:updater.inc):0755
  • 对于公共上传目录:0755或0775
  • 私人上传目录:0750或0770
  • 对于公共上传的文件:0644或0664
  • 对于私人上传的文件:0640或0660
  • 用于上传目录中的.htaccess(请参阅:file_create_htaccess()):0444(默认值)或0644
  • 对于settings.php,对于所有(和其他机密文件)都是只读的:0440
  • 对于所有其他网站目录:0755
  • 对于所有其他网络文件:0644

建议的文件/目录所有权:

  • 所有上传目录/文件的所有者应设置为Apache用户,
  • 所有网络/源目录/文件的所有者应设置为非Apache用户,
  • (可选)将所有源的组设置为Apache组,

以下是控制新项目的默认目录/文件权限的变量:

file_chmod_directory: 0775
file_chmod_file: 0664

这是一些用于修复权限的脚本:fix-permissions.sh


阅读更多:


这是我用来修复远程主机上公用/专用目录权限的脚本:

#!/bin/sh -e
# Script to correct public/private directory and files permissions.
[ -z "$1" ] && { echo Usage: $0 @remote.dst; exit 1; }

DST="$1" && shift
GET_HTTP_GROUP='ps axo user,group,comm | egrep "(apache|httpd)" | grep -v ^root | uniq | cut -d\  -f 1'

drush $* $DST ssh 'PUB=$(drush dd %files) && PRIV=$(drush dd %private) && AGROUP=$('"$GET_HTTP_GROUP"') && chgrp -vR $AGROUP $PUB $PRIV && chmod -vR u+rwX,g+rwX,o+rX $PUB $PRIV'

注意:上面的代码将尝试检索Apache组并将其设置为GET_HTTP_GROUP变量。


非常好的备忘单,带有书签。干得好..
2016年

4

该Shell脚本位于此页面的底部:https : //www.drupal.org/node/244924

我偶尔运行它以确保正确设置我的权限。

#!/bin/bash
# Help menu
print_help() {
cat <<-HELP
This script is used to fix permissions of a Drupal installation
you need to provide the following arguments:
1) Path to your Drupal installation.
2) Username of the user that you want to give files/directories ownership.
3) HTTPD group name (defaults to www-data for Apache).
Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER --httpd_group=GROUP
Example: (sudo) bash ${0##*/} --drupal_path=/usr/local/apache2/htdocs --drupal_user=john --httpd_group=www-data
HELP
exit 0
}
if [ $(id -u) != 0 ]; then
  printf "**************************************\n"
  printf "* Error: You must run this with sudo. *\n"
  printf "**************************************\n"
  print_help
  exit 1
fi
drupal_path=${1%/}
drupal_user=${2}
httpd_group="${3:-www-data}"
# Parse Command Line Arguments
while [ $# -gt 0 ]; do
  case "$1" in
    --drupal_path=*)
      drupal_path="${1#*=}"
      ;;
    --drupal_user=*)
      drupal_user="${1#*=}"
      ;;
    --httpd_group=*)
      httpd_group="${1#*=}"
      ;;
    --help) print_help;;
    *)
      printf "***********************************************************\n"
      printf "* Error: Invalid argument, run --help for valid arguments. *\n"
      printf "***********************************************************\n"
      exit 1
  esac
  shift
done
if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/core/modules/system/system.module" ] && [ ! -f "${drupal_path}/modules/system/system.module" ]; then
  printf "*********************************************\n"
  printf "* Error: Please provide a valid Drupal path. *\n"
  printf "*********************************************\n"
  print_help
  exit 1
fi
if [ -z "${drupal_user}" ] || [[ $(id -un "${drupal_user}" 2> /dev/null) != "${drupal_user}" ]]; then
  printf "*************************************\n"
  printf "* Error: Please provide a valid user. *\n"
  printf "*************************************\n"
  print_help
  exit 1
fi
cd $drupal_path
printf "Changing ownership of all contents of "${drupal_path}":\n user => "${drupal_user}" \t group => "${httpd_group}"\n"
chown -R ${drupal_user}:${httpd_group} .
printf "Changing permissions of all directories inside "${drupal_path}" to "rwxr-x---"...\n"
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
printf "Changing permissions of all files inside "${drupal_path}" to "rw-r-----"...\n"
find . -type f -exec chmod u=rw,g=r,o= '{}' \;
printf "Changing permissions of "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
cd sites
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
printf "Changing permissions of all files inside all "files" directories in "${drupal_path}/sites" to "rw-rw----"...\n"
printf "Changing permissions of all directories inside all "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
for x in ./*/files; do
    find ${x} -type d -exec chmod ug=rwx,o= '{}' \;
    find ${x} -type f -exec chmod ug=rw,o= '{}' \;
done
echo "Done setting proper permissions on files and directories"
Copy the code above to a file, name it "fix-permissions.sh" and run it as follows:
sudo bash fix-permissions.sh --drupal_path=your/drupal/path --drupal_user=your_user_name

Note: The server group name is assumed "www-data", if it differs use the --httpd_group=GROUP argument.

2

另外,如果您正在运行fastcgi,则php将以用户身份运行,并且将有权访问该用户有权访问的所有文件,除非您有意避免这种情况。


1

这帮助我解决了OSX许可问题。我是由原生用户在https://www.drupal.org/node/244924#comment-3741738中找到的。我就像他在迁移后遇到了问题。

[root@localhost]cd /path_to_drupal_installation/sites
[root@localhost]find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
[root@localhost]find . -name files -type d -exec find '{}' -type f \; | while read FILE; do chmod ug=rw,o= "$FILE"; done
[root@localhost]find . -name files -type d -exec find '{}' -type d \; | while read DIR; do chmod ug=rwx,o= "$DIR"; done

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.