如何在Linux中给文件写权限?

时间:2022-04-16 06:37:11

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中的特定用户授予文件上的写权限?比如它的所有者?每个人都可以读这个文件。

4 个解决方案

#1


19  

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。

#2


8  

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);

#3


1  

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是只读。假设文件的所有者是您希望授予写访问权的用户。

#4


0  

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/

#1


19  

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。

#2


8  

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);

#3


1  

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是只读。假设文件的所有者是您希望授予写访问权的用户。

#4


0  

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/