linux可以改变文件夹权限吗

在Linux中更改文件夹权限主要依靠chmod命令。
此命令允许您控制其他人如何访问您的文件夹、他们是否可以修改它以及是否可以查看它。

首先我们来说一下谁有权限更改权限。
只有两个人,一个是这个文件夹的老大,另一个是root。
老大,您就是创建这个文件夹的用户。
Root是一个超级老板。
普通人,即普通用户,只能管理他们创建的文件夹。
Root可以控制一切。

chmod 可以通过两种方式进行。

第一种,符号模式。
就像加、加、减一样。
格式为 chmod [谁] [正负等于] [权限] 文件夹名称。

谁?老板,你来了; g、老板圈子里的人; o, outsider; a, all.一般情况下,大家都是默认的。

加法和减法呢? + 添加权限;
减少权限; = 直接授予权限,不用管其他事情。

权限呢? r 读取; w writes; x is executed.对于文件夹,x 表示您是否可以进入其中查看内容。

举个例子吧。
chmod g+w,o-x mydir。
这意味着老板圈子里的人有写权。
外人,执行权没了。
就是这么简单。

二、数字模式。
如果您使用数字,这会更容易。
是一个三位数,大约是7 5 5 顺序是老大、老大的圈子、外人。

这些数字代表什么意思? 0 没有权限; 1 execute; 2 写; 4 read. Add it up.例如7 是4 +2 +1 ,同时具有读和写执行。

7 5 5 是什么意思?老板拥有所有权限。
老大圈子有读/写执行,圈外只有读/写执行。
就是这样。

还有一个递归修改。
甚至子文件夹也发生了变化。
使用 -R 选项。
例如 chmod -R 7 5 0 /path/to/dir。
这意味着这个目录及其下的所有子目录都拥有所有boss权限。
老板的圈子可以读取并执行它们,外人没有权限。

实际应用怎么样?如果您希望其他人看到您的文件夹,请添加读取权限。
chmod o+r mydir。
如果您想防止其他人更改它,请减少写入权限。
chmod o-w mydir。
如果你想让人进入,你必须有x权限。

linux 文件夹赋权给用户

说白了,Linux中更改文件夹权限主要有两个步骤:先用chown改老大,然后用chmod开门。
然而,这件事很复杂。
在各种场景下使用错误的参数可能会导致系统崩溃。

我们先来说说chown最重要的用法。
当我们去年运行该项目时,它几乎被毁掉了,因为我们没有添加 -R。
例如,如果您想将 /var/log/project 的完全控制权授予用户 devops,只需 chown devops /var/log/project 即可。
但是,如果下面的子文件夹也发生更改,则需要添加 -R。
使用这个参数很有趣,但也有风险。
上次有人使用 -R 更改 /etc 时,整个服务器立即变得混乱。
另外需要注意的是chown和chmod的执行顺序不能颠倒。
如果你先更改权限,然后更改老板,你可能会发现新老板也没有权限进行操作。
还有一个更重要的细节。
例如,如果您想将文件夹和所有文件递归发送给组管理员,您可以使用 chown :admin /path/folder -R。
注意前面有一个冒号。

一开始我以为chmod只是加数字,但后来我意识到有些不对劲。
例如,如果你给所有者完全权限7 00,则用户组和外部人员甚至看不到它。
去年测试环境发生了一些事情,因为没有看文档,没有设置/tmp/output为6 00,结果运维所有日志Couldn't even capture it.很多人没有注意到这一点。
Execution permission for folders is very important. The default folder is rwxr-xr-x, which means the owner can enter, view, and change it, but group users and outsiders can only view but cannot log in. If you remove the execute permission x, the group users can't even knock on the folder door.
Wait, one more thing to remind you, when setting permissions with numbers, do not use 7 7 7 - honestly, it's quite a trick. Last year, a newcomer set /public to 7 7 7 , and the script downloaded all sensitive files directly. Finally, let's talk about a scenario. If you want the user to be able to write to the reports folder /data/reports at will without giving reporter root permissions, simply add the line reporter ALL=(ALL) NOPASSWD:/data/reports to /etc/sudoers so he can use the sudo write /data/reports/report.csv command to write files without having to enter the password every time. It is recommended to check the configuration with visudo first. Just don't change the command and don't forget to verify the syntax.
If you play it well this permission game can keep you out of trouble, but if you play it poorly it will get you drinking in a pot.