如何在 Linux 中授予对文件的写权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6732430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I give write permission to file in Linux?
提问by boom
How can I (programmatically) give write permission on a file to a particular user in Linux? Like, for example, its owner? Everyone has read access to this file.
如何(以编程方式)向 Linux 中的特定用户授予对文件的写权限?例如,它的所有者?每个人都可以读取此文件。
采纳答案by DarkDust
In a shell or shell script simply use:
在 shell 或 shell 脚本中,只需使用:
chmod u+w <filename>
This only modifies the write bit for the user, all other flags remain untouched.
这只会修改用户的写位,所有其他标志保持不变。
If you want to do it in a C program, you need to use:
如果要在 C 程序中进行,则需要使用:
int chmod(const char *path, mode_t mode);
First query the existing mode via
首先通过查询现有模式
int stat(const char *path, struct stat *buf);
... and just set the write bit by doing newMode = oldMode | S_IWUSR
. See man 2 chmod
and man 2 stat
for details.
...并通过执行设置写入位newMode = oldMode | S_IWUSR
。查看man 2 chmod
和man 2 stat
了解详情。
回答by Gazler
chmod 644 FILENAME
6 is read and write, 4 is read only. Assuming the owner of the file is the user you wish to grant write access to.
6 是读写,4 是只读。假设文件的所有者是您希望授予写访问权限的用户。
回答by Delan Azabani
The octal mode 644
will give the owner read and write permissions, and just read permissions for the rest of the group, as well as other users.
八进制模式644
将授予所有者读写权限,而仅为组的其余成员以及其他用户授予读取权限。
read = 4
write = 2
execute = 1
owner = read | write = 6
group = read = 4
other = read = 4
The basic syntax of the command to set the mode is
设置模式的命令的基本语法是
chmod 644 [file name]
In C, that would be
在 C 中,那将是
#include <sys/stat.h>
chmod("[file name]", 0644);
回答by Saurabh Gandhi
You can do that using chmod, on ubuntu you can try out $sudo chmod 666 This would give read/write permissions to all...check this out for more details: http://catcode.com/teachmod/
您可以使用 chmod 来做到这一点,在 ubuntu 上您可以尝试 $sudo chmod 666 这将为所有人提供读/写权限...查看更多详细信息:http: //catcode.com/teachmod/
回答by Kumar
You can use chmod 777 <filename>
您可以使用 chmod 777 <filename>