开发者

How do I give write permission to file in Linux?

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 fi开发者_StackOverflow社区le.


In a shell or shell script simply use:

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:

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.


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.

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

#include <sys/stat.h>

chmod("[file name]", 0644);


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.


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/


You can use chmod 777 <filename>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜