Today I learned - Permission denied? chown to the rescue
Tags: linux, docker, todayilearned
Published: 2020-02-01 12:36:52

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](https://dev-to-uploads.s3.amazonaws.com/i/us5tj9v3qur58g9zm19k.png)

**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](https://dev-to-uploads.s3.amazonaws.com/i/8vli252lv31d1jwurmh0.png)

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

This is how to read it:
![how to read permissions](https://dev-to-uploads.s3.amazonaws.com/i/bfbxi77f5nq303y7jd3g.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/tok5pxfcm098fslp35a7.png)

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](https://dev-to-uploads.s3.amazonaws.com/i/hy72a55i0i3gpfkwu7sa.png)


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:

- [Chown Command in Linux (File Ownership)](https://linuxize.com/post/linux-chown-command/)
- [Linux chown command
](https://www.computerhope.com/unix/uchown.htm)
- [How To Use chmod and chown Command](https://www.cyberciti.biz/faq/how-to-use-chmod-and-chown-command/)
- [Linux chmod command](https://www.computerhope.com/unix/uchmod.htm)
- [Chmod Command in Linux (File Permissions)](https://linuxize.com/post/chmod-command-in-linux/)