如何在 Web 服务器文档根目录上设置只读文件权限 | Linux 中国

2018 年 3 月 26 日 Linux中国 译者:yizhuyan
如何对我存放在 /var/www/html/ 目录中的所有文件设置只读权限?
-- Vivek Gite

致谢
编译自 | https://www.cyberciti.biz/faq/howto-set-readonly-file-permission-in-linux-unix/ 
 作者 | Vivek Gite
 译者 | yizhuyan (yizhuoyan) 🌟 🌟 共计翻译:4 篇 贡献时间:54 天

Q:如何对我存放在 /var/www/html/ 目录中的所有文件设置只读权限?

你可以使用 chmod 命令对 Linux/Unix/macOS/OS X/*BSD 操作系统上的所有文件来设置只读权限。这篇文章介绍如何在 Linux/Unix 的 web 服务器(如 Nginx、 Lighttpd、 Apache 等)上来设置只读文件权限。

如何设置文件为只读模式

语法为:

   
     
     
     
  1. ### 仅针对文件 ###

  2. chmod 0444 /var/www/html/*

  3. chmod 0444 /var/www/html/*.php

如何设置目录为只读模式

语法为:

   
     
     
     
  1. ### 仅针对目录 ###

  2. chmod 0444 /var/www/html/

  3. chmod 0444 /path/to/your/dir/

  4. # ***************************************************************************

  5. # 假如 web 服务器的用户/用户组是 www-data,文件拥有者是 ftp-data 用户/用户组

  6. # ***************************************************************************

  7. # 设置目录所有文件为只读

  8. chmod -R 0444 /var/www/html/

  9. # 设置文件/目录拥有者为 ftp-data

  10. chown -R ftp-data:ftp-data /var/www/html/

  11. # 所有目录和子目录的权限为 0445 (这样 web 服务器的用户或用户组就可以读取我们的文件)

  12. find /var/www/html/ -type d -print0 | xargs -0 -I {} chmod 0445 "{}"

找到所有 /var/www/html 下的所有文件(包括子目录),键入:

   
     
     
     
  1. ### 仅对文件有效 ###

  2. find /var/www/html -type f -iname "*" -print0 | xargs -I {} -0 chmod 0444 {}

然而,你需要在 /var/www/html 目录及其子目录上设置只读和执行权限,如此才能让 web 服务器能够访问根目录,键入:

   
     
     
     
  1. ### 仅对目录有效 ###

  2. find /var/www/html -type d -iname "*" -print0 | xargs -I {} -0 chmod 0544 {}

警惕写权限

请注意在 /var/www/html/ 目录上的写权限会允许任何人删除文件或添加新文件。也就是说,你可能需要设置一个只读权限给 /var/www/html/ 目录本身。

   
     
     
     
  1. ### web根目录只读 ###

  2. chmod 0555 /var/www/html

在某些情况下,根据你的设置要求,你可以改变文件的属主和属组来设置严格的权限。

   
     
     
     
  1. ### 如果 /var/www/html 目录的拥有人是普通用户,你可以设置拥有人为:root:root 或 httpd:httpd (推荐) ###

  2. chown -R root:root /var/www/html/

  3. ### 确保 apache 拥有 /var/www/html/ ###

  4. chown -R apache:apache /var/www/html/

关于 NFS 导出目录

你可以在 /etc/exports 文件中指定哪个目录应该拥有只读或者读写权限 [1]。这个文件定义各种各样的共享在 NFS 服务器和他们的权限。如:

   
     
     
     
  1. # 对任何人只读权限

  2. /var/www/html *(ro,sync)

  3. # 192.168.1.10(upload.example.com)客户端读写权限访问

  4. /var/www/html 192.168.1.10(rw,sync)

关于用于 MS-Windows客户端的 Samba(CIFS)只读共享

要以只读共享 sales,更新 smb.conf,如下:

   
     
     
     
  1. [sales]

  2. comment = Sales Data

  3. path = /export/cifs/sales

  4. read only = Yes

  5. guest ok = Yes

关于文件系统表(fstab)

你可以在 Unix/Linux 上的 /etc/fstab 文件中配置挂载某些文件为只读模式。

你需要有专用分区,不要设置其他系统分区为只读模式。

如下在 /etc/fstab 文件中设置 /srv/html 为只读模式。

   
     
     
     
  1. /dev/sda6 /srv/html ext4 ro 1 1

你可以使用 mount 命令重新挂载分区为只读模式[2](使用 root 用户)

   
     
     
     
  1. # mount -o remount,ro /dev/sda6 /srv/html

或者

   
     
     
     
  1. # mount -o remount,ro /srv/html

上面的命令会尝试重新挂载已挂载的文件系统到 /srv/html上。这是改变文件系统挂载标志的常用方法,特别是让只读文件改为可写的。这种方式不会改变设备或者挂载点。让文件变得再次可写,键入:

   
     
     
     
  1. # mount -o remount,rw /dev/sda6 /srv/html

   
     
     
     
  1. # mount -o remount,rw /srv/html

Linux:chattr 命令

你可以在 Linux 文件系统上使用 chattr 命令改变文件属性为只读[3],如:

   
     
     
     
  1. chattr +i /path/to/file.php

  2. chattr +i /var/www/html/

  3. # 查找任何在/var/www/html下的文件并设置为只读#

  4. find /var/www/html -iname "*" -print0 | xargs -I {} -0 chattr +i {}

通过提供 -i 选项可删除只读属性:

   
     
     
     
  1. chattr -i /path/to/file.php

FreeBSD、Mac OS X 和其他 BSD Unix 用户可使用chflags命令[4]

   
     
     
     
  1. ### 设置只读 ##

  2. chflags schg /path/to/file.php

  3. ### 删除只读 ##

  4. chflags noschg /path/to/file.php


via: https://www.cyberciti.biz/faq/howto-set-readonly-file-permission-in-linux-unix/

作者:Vivek Gite[6] 译者:yizhuoyan 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出


登录查看更多
0

相关内容

【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
【2020新书】从Excel中学习数据挖掘,223页pdf
专知会员服务
85+阅读 · 2020年6月28日
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
160+阅读 · 2020年5月14日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【ICMR2020】持续健康状态接口事件检索
专知会员服务
17+阅读 · 2020年4月18日
【SIGMOD2020-腾讯】Web规模本体可扩展构建
专知会员服务
29+阅读 · 2020年4月12日
100+篇《自监督学习(Self-Supervised Learning)》论文最新合集
专知会员服务
161+阅读 · 2020年3月18日
【资源】100+本免费数据科学书
专知会员服务
105+阅读 · 2020年3月17日
【电子书】Flutter实战305页PDF免费下载
专知会员服务
20+阅读 · 2019年11月7日
msf实现linux shell反弹
黑白之道
49+阅读 · 2019年8月16日
用Now轻松部署无服务器Node应用程序
前端之巅
16+阅读 · 2019年6月19日
Kali Linux 渗透测试:密码攻击
计算机与网络安全
15+阅读 · 2019年5月13日
PHP使用Redis实现订阅发布与批量发送短信
安全优佳
7+阅读 · 2019年5月5日
Pupy – 全平台远程控制工具
黑白之道
43+阅读 · 2019年4月26日
I2P - 适用于黑客的Android应用程序
黑白之道
28+阅读 · 2019年3月6日
去哪儿网开源DNS管理系统OpenDnsdb
运维帮
21+阅读 · 2019年1月22日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
Python | 爬爬爬:爬百度云,爬百度贴吧,爬爱奇艺
计算机与网络安全
3+阅读 · 2018年3月30日
A survey on deep hashing for image retrieval
Arxiv
14+阅读 · 2020年6月10日
Arxiv
108+阅读 · 2020年2月5日
Neural Module Networks for Reasoning over Text
Arxiv
9+阅读 · 2019年12月10日
Arxiv
13+阅读 · 2018年4月18日
Arxiv
8+阅读 · 2018年2月23日
Arxiv
7+阅读 · 2017年12月28日
VIP会员
相关VIP内容
【实用书】学习用Python编写代码进行数据分析,103页pdf
专知会员服务
190+阅读 · 2020年6月29日
【2020新书】从Excel中学习数据挖掘,223页pdf
专知会员服务
85+阅读 · 2020年6月28日
干净的数据:数据清洗入门与实践,204页pdf
专知会员服务
160+阅读 · 2020年5月14日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
【ICMR2020】持续健康状态接口事件检索
专知会员服务
17+阅读 · 2020年4月18日
【SIGMOD2020-腾讯】Web规模本体可扩展构建
专知会员服务
29+阅读 · 2020年4月12日
100+篇《自监督学习(Self-Supervised Learning)》论文最新合集
专知会员服务
161+阅读 · 2020年3月18日
【资源】100+本免费数据科学书
专知会员服务
105+阅读 · 2020年3月17日
【电子书】Flutter实战305页PDF免费下载
专知会员服务
20+阅读 · 2019年11月7日
相关资讯
msf实现linux shell反弹
黑白之道
49+阅读 · 2019年8月16日
用Now轻松部署无服务器Node应用程序
前端之巅
16+阅读 · 2019年6月19日
Kali Linux 渗透测试:密码攻击
计算机与网络安全
15+阅读 · 2019年5月13日
PHP使用Redis实现订阅发布与批量发送短信
安全优佳
7+阅读 · 2019年5月5日
Pupy – 全平台远程控制工具
黑白之道
43+阅读 · 2019年4月26日
I2P - 适用于黑客的Android应用程序
黑白之道
28+阅读 · 2019年3月6日
去哪儿网开源DNS管理系统OpenDnsdb
运维帮
21+阅读 · 2019年1月22日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
Python | 爬爬爬:爬百度云,爬百度贴吧,爬爱奇艺
计算机与网络安全
3+阅读 · 2018年3月30日
相关论文
A survey on deep hashing for image retrieval
Arxiv
14+阅读 · 2020年6月10日
Arxiv
108+阅读 · 2020年2月5日
Neural Module Networks for Reasoning over Text
Arxiv
9+阅读 · 2019年12月10日
Arxiv
13+阅读 · 2018年4月18日
Arxiv
8+阅读 · 2018年2月23日
Arxiv
7+阅读 · 2017年12月28日
Top
微信扫码咨询专知VIP会员