DEV Community

Cover image for Today I learned - Permission denied? chown to the rescue

Posted on

Today I learned - Permission denied? chown to the rescue

Today I learned is a series where I share a short summary of one thing I learned during the day. The idea is to show what resources I used and give my own short explanation to things.

I was working with a CMS system in Docker Node Alpine environment. At some point, when installing the CMS, I got an error stating that my user doesn't have permissions to run mkdir in /extensions

No permissions

Why? I had changed my default user in Docker container from root to myotherusername for security reasons.

By doing that, I also lost many of the permissions to manipulate the filesystem.

101 of permissions in Linux

You should know that in Linux, files/directories can have three types of permissions:

  • READ(r)
  • WRITE(w)
  • EXECUTE(x).

Also, there are three parties you can set the file permissions for:

  • owner(creator by default)
  • user groups
  • others

You can run ls -la to see that info.
It will display the following data about your current directory:

  • a list of files and directories
  • their permissions
  • owners
  • owner groups

In my case, I was interested in /extensions folder.
extensions folder

drwxr--r-- refers to the permissions that were applied to the directory.

This is how to read it:
how to read permissions

We can see that:

  • it's was directory
  • owner(root) could read, write, execute
  • group(users who were in the root group) could read
  • others could read

As my current user was myotherusername, not root, I didn't have permissions to create directory inside /extensions folder.

Solution? chown

With chown, you can modify the owner of a file or directory.

While being root user, I had to run this command:
chown command

And boom - myotherusername became the owner of the folder and got the permissions to read, write and execute in /extensions folder.

Btw, the -R here means recursively.

As I used Dockerfile to build my image, I actually added the command to be run inside the Dockerfile:
dockerfile code

And that's was it!

If you're more interested in the topic, I also recommend you to read about chmod command which is used to manipulate specific permissions for owner/groups/others.

Also, if you have any feedback / suggestions, please write in the comments.

The resources I used for learning this topic:

Top comments (0)