在/ var / www中工作时如何避免使用sudo?

每次我在/var/www工作时,我都想停止使用sudo 。 我怎样才能做到这一点? 我只是想将我的所有网站都放在这个目录中,并且不需要太多痛苦就可以使用它们。

这里的大多数答案都没有考虑安全性。 感觉每次运行sudo都不是很明智。 如果您输入错字(例如( 不执行sudo rm -rf / var/www/dir ),您可能会丢弃您的系统。

注意:从Apache 2.4.7 / Ubuntu 14.04开始, /var/www已移至/var/www/html调整此答案中的命令。

看到:

  • 从2.4.7版本的apache2开始,我的本地网站放在哪里?

  • 为什么将apache2 www dir移动到/ var / www / html?

  • 更改HTTP服务器的默认文档根目录

坏主意:

  • chmod 777 (sagarchalise) – 这允许任何有权访问您系统的人写入目录和文件,从而允许入侵者执行www-data用户下的任何代码
  • chgrp -R www-data $HOME (cob) – 这允许www-data读取或写入主目录中的任何文件。 这并未考虑最低权限规则
  • chown -R $USER:$USER /var/www (kv1dr) – 除非世界对/var/www具有读取权限,否则在www-data下运行的网络服务器将无法读取(提供)文件。 如果文件是公共可访问的纯HTML文档,那么如果世界可以读取该文件,则可能不是问题。 但是如果文件是包含密码的PHP文件,那就是。

注意 :在下面的解决方案中,我已经授予了www-data写权限。 但是,/ /usr/share/doc/base-passwd/users-and-groups.txt.gz指出:

www数据

一些Web服务器作为www-data运行。 Web内容不应归该用户所有,否则受感染的Web服务器将能够重写Web站点。 由Web服务器写出的数据将由www-data拥有。

在可能的情况下,请勿向www-data组授予写入权限。 www-data只需要能够读取文件,以便网络服务器可以为其提供服务。 www-data需要写权限的唯一情况是存储上传和需要写入的其他位置的目录。

解决方案1

将自己添加到www-data组并在/var/www目录中设置setgid位,以便所有新创建的文件也inheritance该组。

 sudo gpasswd -a "$USER" www-data 

更正以前创建的文件(假设您是/var/www的唯一用户):

 sudo chown -R "$USER":www-data /var/www find /var/www -type f -exec chmod 0660 {} \; sudo find /var/www -type d -exec chmod 2770 {} \; 

(甚至更安全:使用6402750并手动chmod g+w file-or-dir ,需要由网络服务器写入)

解决方案2

为您的主目录的每个项目创建一个符号链接。 假设你的项目位于~/projects/foo ,你想让它位于/var/www/foo ,运行:

 sudo ln -sT ~/projects/foo /var/www/foo 

如果您的主目录没有为other目录设置执行位(下降)(出于安全原因),请将其组更改为www-data ,但仅设置执行位 (无读/写)。 对~/projects文件夹执行相同操作,因为它可能包含除www之外的其他项目。 (如果您之前已将用户添加到www-data组,则不需要sudo 。)

 sudo chgrp www-data ~ ~/projects chmod 710 ~ ~/projects 

将组设置为~/projects/foo上的www-data ,并允许Web服务器读取和写入文件和文件+目录并下载到目录中:

 sudo chgrp www-data ~/projects/foo find ~/projects/foo -type f -exec chmod 660 {} \; find ~/projects/foo -type d -exec chmod 2770 {} \; 

更安全:默认情况下使用640和2750并手动chmod文件和目录,这些文件和目录需要由webserver用户写入。 只有当您希望组中可以访问~/projects/foo每个新创建的文件时,才应添加setgid位。

从现在开始,您可以访问http://localhost/foo站点并在~/projects/foo编辑项目文件。

也可以看看

  • 权限问题:Apache如何访问我的主目录中的文件?
  • / var / www不应该有chmod 777的原因

而不是将我的网站存储在/ var / www中,而是将链接放在我的主文件夹中的网站上。 我可以自由编辑或添加页面到我的网站。 当我对更改感到满意时,我会将FTP发送到我的域名链接的托管公司。

如果你将/ var / www写入其组并将自己添加到组中,则在保持相当安全的同时不必使用sudo。 试试这个:

 sudo adduser  www-data sudo chown -R www-data:www-data /var/www sudo chmod -R g+rw /var/www 

然后,您应该能够轻松编辑/var/www/ files。

第一行将您添加到www-data组,第二行将清除所有具有混乱所有权的文件,第三行使得所有作为www-data组成员的用户都可以读取和写入/var/www所有文件/var/www

注意事项

  • 不要将文件权限设置为777 (世界可写)

    这是一个重要的安全漏洞,特别是如果您启用PHP等服务器端脚本。 非特权进程不应该写入会影响网站的文件,或者在使用服务器端脚本的情况下,执行任意代码。

  • 不要将自己添加为www-data组的成员并赋予其写入权限

    该组的目的是它是一个非特权组, 服务器进程运行为。 出于与上述相同的原因,他们应该尽可能具有对网站文件的读取权限。

  • 不要更改Apache进程的权限

    默认情况下,Apache子进程作为www-data用户和组运行,不应更改。 这只是一种给予文件系统无权限写入权限的方法。

    在某些情况下,您希望服务器端脚本能够写入文件,在这种情况下, 只有那些文件应该由www-data写入,并且需要注意确保安全性。

DOS

  • 将文件设置为您自己拥有

    如果您是唯一一个或通常修改网站上某些文件的人,那么只需拥有这些文件即可。 将其所有者设置为

    您无需为此修改服务器权限,因为即使文件归您所有,服务器仍将继续获得只读访问权限。

  • 选择一个合理的位置来存放文件(使用DocumentRoot

    如果/var/www没有意义,欢迎您将它们放在别处。 如果它们特定于您自己的开发或测试,您可以将它们放在您的主目录中。 或者您可以在/srv设置一些目录。

  • 如果要授予写访问权限,请为此创建

    不要重复使用系统组,因为这些系统组通常设计为具有当前访问权限,并且出于安全原因而不再拥有。

这很简单。 你既不需要启用apache’UserDir’(不推荐)也不需要搞乱’www-data’组(在Fedora上的情况下是apache组)

只需在/var/www/html创建项目目录即可

 cd /var/www/html sudo mkdir my_project 

然后只需将项目目录chown给您的用户。

 sudo chown your_username my_project 

现在,您可以使用您选择的任何编辑器IDE作为常规用户开始处理项目文件夹。 没有更多的sudos 🙂

在/ www上的chmod允许所有者访问,并且chown以确保您拥有它。 可能是一个愚蠢的想法,但它肯定会奏效。

您可以在终端中启动www会话

 sudo su www-data 

结合不同颜色的提示*,使其更明显的是它是不同用户的shell,并且策略总是将相应的xterm(和编辑器等)放在 – 例如 – 虚拟桌面4上,以便你习惯了它,以避免混淆。

*)对于具有不同字符的不同颜色的提示,请创建一个文件/ etc / prompt,如下所示:

 # PROMPTING # When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the sec- # ondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be customized # by inserting a number of backslash-escaped special characters that are decoded as follows: # \a an ASCII bell character (07) # \d the date in "Weekday Month Date" format (eg, "Tue May 26") # \D{format} # the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format # results in a locale-specific time representation. The braces are required # \e an ASCII escape character (033) # \h the hostname up to the first `.' # \H the hostname # \j the number of jobs currently managed by the shell # \l the basename of the shell's terminal device name # \n newline # \r carriage return # \s the name of the shell, the basename of $0 (the portion following the final slash) # \t the current time in 24-hour HH:MM:SS format # \T the current time in 12-hour HH:MM:SS format # \@ the current time in 12-hour am/pm format # \A the current time in 24-hour HH:MM format # \u the username of the current user # \v the version of bash (eg, 2.00) # \V the release of bash, version + patchelvel (eg, 2.00.0) # \w the current working directory # \W the basename of the current working directory # \! the history number of this command # \# the command number of this command # \$ if the effective UID is 0, a #, otherwise a $ # \nnn the character corresponding to the octal number nnn # \\ a backslash # \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence # into the prompt # \] end a sequence of non-printing characters # # The command number and the history number are usually different: the history number of a command is its position in # the history list, which may include commands restored from the history file (see HISTORY below), while the command # number is the position in the sequence of commands executed during the current shell session. After the string is # # colors: # \[...\] wird benötigt, damit die shell weiß, daß hier kein printable output ist, und die Umbrüche richtig plaziert. # # ANSI COLORS CRE="\[ [K\]" NORMAL="\[[0;39m\]" # RED: Failure or error message RED="\[[1;31m\]" # GREEN: Success message GREEN="\[[1;32m\]" # YELLOW: Descriptions YELLOW="\[[1;33m\]" # BLUE: System messages BLUE="\[[1;34m\]" # MAGENTA: Found devices or drivers MAGENTA="\[[1;35m\]" # CYAN: Questions CYAN="\[[1;36m\]" # BOLD WHITE: Hint WHITE="\[[1;37m\]" # # default: # postgres, oracle, www-data # # PS1=$BLUE"machine]->"$NORMAL\\w"$BLUE ø $NORMAL" PS1=$BLUE"machine]:"$NORMAL\\w"$BLUE > $NORMAL" # # root, stefan: # case "$UID" in '0') PS1=$RED"machine:"$NORMAL\\w"$RED # $NORMAL" ;; '1000') PS1=$GREEN"machine:"$BLUE\\w$YELLOW" > "$NORMAL ;; # default) # ;; esac 

例如,从/etc/bash.bashrc获取它。

作为帮助区分的附加工具,您可以随时使用别名“编辑”或符号链接编辑文件,这些文件根据您的身份(泰勒/ www-数据)指向gedit或mousepad,vim或pico。 或者您可以使用不同的编辑器配置文件,至少在gedit中,您可以将您的首选项设置为白色地面上的黑色文本或黑色地面上的白色文本。

我只有这样一个以root身份工作的策略,所以我不确定它对于使用www-data有多好。 结合ssh-sessions到不同的主机,它们有自己的提示,它并没有阻止我有时出错,但如果它发生了,我意识到快速,错误,并且它很少发生。

注意:提示脚本部分是bash联机帮助页的副本。

在我的网站的这个页面上,我介绍了在apache和pi用户之间更改/var/www权限的命令,但它必不可少

 sudo chown -R pi /var/www 

然后apache重启

 sudo service apache2 restart