I used chmod 777 -R / and why you should be careful or avoid doing it
Tags: aws, ec2, linux, beginners
Published: 2023-03-17 15:03:00

As an application developer, I never cared much about Linux commands, but since I am working at a very small company now I get to do all sorts of things that I really enjoy, if you are like me then this article is for you.

I recently ran **chmod 777 -R /** command on one of our admin AWS EC2 instances, and this resulted in a lot of unwanted and some not-so-good things. I will detail the things that happened and some details about chmod and permissions in general in this article and by the end of this article, you will have a good understanding of these and will execute these commands with some confidence.

As an application developer, I do not have 100% knowledge of Linux systems and how they work internally, but after I did this mistake I did some research and wrote this up so that I might not do the same in the future if you are reading this I might save you as well.

### File System, user and user groups in Linux:

Before going into the details let us, in short, discuss file systems, user and user groups in Linux, basically, on Unix based system there are permissions attached to each of the files created, you might have heard like read, write and execute. Based on these permissions a user will have access to these files and there is something called a super user who can grant or revoke access to any files on the file system. This superuser will be particularly useful when a user owns a very important directory and leaves the organization then in this case a superuser can come and change the owner of that directory(if a normal user is permitted to run some commands at the root level then this can be done using sudo.)

### About Access Permissions on files

Now let's discuss something related to access that can be set on files, these are either represented with alphabets or numbers

```bash
ls -l your_file_name -- Command 
-rw-------@ 1 SRA 4238108 Apr 21  2022 IMG_0237.jpg --- output
# here my file name is IMG_0237.jpg
ls -ld your_folder_name -- Command
```

So here, the line below **ls - l** gave us the permissions that we have on the file, which are read and write(rw). if you want to know about a directory we can use **ls -ld**. you can find more about what **ls** command can do using **man ls** command.

### About how you can change Permissions, user or user group

Now let's discuss how we can modify permissions are assign a new user or group to a certain folder or a file. This can be done using chown and chmod commands. Firstly, let's discuss chmod and the access codes that come with chmod

* r: used to give read permission
    
* w: used to give write permission
    
* x: used to give execute permission
    
* \-r: used to remove read permission
    
* \-w: used to remove write permission
    
* \-x: and you guess it right to remove execute permission
    

<details data-node-type="hn-details-summary"><summary>Alternate Code</summary><div data-type="detailsContent">Alternatively, we can also you numbers to represent these permission.</div></details>

let says you used ls -l on file and you were not satisfied with the permission on the file then you use chmod.

```bash
chmod permission_you_want your_filename
chmod rwx your_filename
```

Now let's discuss **chown,** this can be used to change user and or group on a file or directory. let's say as i said earlier a file or folder is owned by someuser that has left the company and we own want to change it to a different user and group.

```bash
chown user_name:user_group_name file_or_folder_name
```

Once you run this command and now want to check the user and group, you can just use ls -l or ls -ld.

### Now what is the fuss about -R

Up to this point, we are just either dealt with a folder or a single file, but what will you do if you want to change permission on every single file in a folder and any files in a subfolder in a folder, this is where -R(Recursive) option will come in hand.

```bash
chmod -R your_access_modes folder_name
chmod -R 777 your_folder_name/
```

### Why you should be careful

Now let's go into the details of why we should be extra careful while running chmod 777 in general, 777 means you are giving read, write and execute permission to the user which might be ok to give to a single file, but giving to a complete folder is too risky but can be accepted to an extent but the command I ran

```bash
chmod -R 777 /
```

This is too risky and I think should be avoided and should be never run especially if you are an application developer, what this does is it changes permissions on every single file under root, which is every single file on your Linux machine, this gives read, write and execute permission to all the files to all the user who have access to that machine, this is a very serious security issue(these issues can be an article in itself so I am not discussing them here.). In my case how it effected us is, the machine I ran this command on, we use to run our jenkins and it impacted your CI/CD pipeline for almost two days, it messed up with the sudo permissions that other users have on this machine. In simple words it got the attention of multiple people and multiple people had to work on fixing it, so that was the impact of it in simple words.

### In Conclusion

But yeah you don't have to worry about it if you already ran it, there is nothing you can do about it other than fixing it. This is what my manager said to me when I confessed that I am the reason for the pipeline being done and I quote his exact words.

> I've done worse over my career -- if someone hasn't messed something up in Linux, they probably are not doing enough development.

I felt very good after reading these words but at the same time felt that I should be extra careful before doing something like this again. So, that is it for this article hope i help someone out there by writing this up, and if you want to know more about these commands I discussed above there are some great articles in fact, I read those before writing this article.

[why-is-chmod-r-777-destructive](https://www.baeldung.com/linux/why-is-chmod-r-777-destructive)  
[change-folder-and-content-permissions](https://www.baeldung.com/linux/change-folder-and-content-permissions)  
[advanced-file-permissions](https://www.baeldung.com/linux/advanced-file-permissions)