4

I am trying to create a file in a directory but I am getting:

`touch`: cannot touch ‘test’: Permission denied

Here are my commands:

[user@xxx api]$ ls -l
total 184
...
drwxrwxr-x  2 root root   4096 2016-04-12 14:38 public 
..
[user@xxx api]$ cd ./public
[user@xxx public]$ touch test
touch: cannot touch ‘test’: Permission denied
10

You can't edit the contents of the public directory if you don't have write and execute access.

  • You indicate you are attempting to create a new file. If the test file doesn't already exist in public, touch will attempt to create a new file. It cannot do this without the write and execute permissions over the parent directory. Execute is required to traverse the directory; write is required to add the inode entry for the new file. Apparently, you don't have one or both of these permissions.

  • If the test file does already exist in public, touch will, by default, update the modification time of the file. Only write access to the file is required for this, as the modification date/time is stored in the file's inode. If the file already exists, you will need to inspect the file's permissions using a command like ls -l public/test to determine if you have write access.


The permissions bitmask on the directory, rwxrwxr-x, means:

  • the root user, i.e. the owner of the directory, has write privileges to the directory as indicated by the first rwx block. This user can also read the directory (the r bit) and traverse it to access its contents (the x bit).
  • members of the root group, i.e. the group on the directory, who are not themselves the root user, also have similar privileges to read, write and traverse the directory as indicated by the second rwx block

All other users only have read and execute rights, as indicated by the last r-x block. As noted, for directories, execute permissions allow you to traverse that directory and access its contents. See this question for more clarity on this.

How do I get permissions?

You will need to talk to your system administrator (which might be you!) to do one of the following:

  • Make you the owner of the public/ directory using a command like chown user public/. This will be suitable if you are the only user who will need to have edit rights.
  • Create a new group with a suitable name, perhaps publiceditors, and set this as the group on the public/ directory using a command like chgrp publiceditors public/. Ensure you and any other users who require the ability to modify the directory are listed as members of the group. This approach works where multiple users need edit capability.
  • Make your user account a member of the root group (not something I would recommend).
  • Provide you with access to log in or masquerade as root, such as with sudo or su with the root password
  • Change the rights on the directory to grant all users write permissions, using a command like chmod o+w public. Be aware that this gives everyone on the box the ability to edit and delete files in the public directory.* You may not want this!

*In the absence of other access control enforcement, such as mandatory access control in the kernel.

What do read, write and execute permissions mean in the context of a directory?

Assuming you're on a Linux box, on a directory, a read permission bit allows you to read the directory listing. The write permission bit allows you to update the directory listed, which is required for creating a file*, editing the name of a file, unlinking (deleting) a file. The execute bit allows you to traverse the directory, access its files etc. More information on Linux directory permissions.

* Actually, you're linking a file into the directory. Most times you will do this at the point of file creation, but there are more complex examples. For example, making a hard link to a file which originally existed elsewhere in the file system will require write access to the target directory of the link, despite the fact you're not creating a new file.


Why write access to the directory?

You need to be able to write to the directory to add a reference to the relevant inode for the file you are adding.

  • 1
    Just to be clear, you need both write and execute permissions on a directory if you want to create a new file there. While the write allows you to edit the directory listing, you still need the execute bit to permit accessing said directory listing. – user.friendly Jun 30 '17 at 20:24
  • i.stack.imgur.com/mZ6qv.png shows a nice chart for this. – user.friendly Jun 30 '17 at 20:31
0

Regarding the public directory rights, only user root or a member of the group root can write into it .

What is configured (drwxrwxr-x) means :

  • user root has read-write-exec rights
  • group root has read-write-exec rights
  • other has read-exec rights

Verify that your user is in the root group or give write access to "other" using the command :

chmod o+w public
  • I would advise against changing the permissions if not needed. So first decide if the dir pulic is supposed to provide write access to everyone. Better use sudo for once-in-a-while resp. exceptional tasks. – Murphy Apr 13 '16 at 11:09
  • I agree. First analyse the permission needs before giving access. – nex84 Apr 13 '16 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.