Learning about Unix File Permissions
Tags: unix
Published: 2019-04-01 22:13:38

---
title: Learning about Unix File Permissions
published: true
description: A run through my debugging process when trying to understand Bash terminal errors. 
tags: unix
---

I've recently been tasked with testing out a small program which my employer wrote up. To be really brief about it, it is a piece of code that allows me to type in my terminal `t 'sample text'` and it'll save it to an intake file which I can find and manipulate in my Text Editor. It's basically a way of taking notes without having to leave Terminal, keeping me focused on whatever the task at hand happens to be. 

This was going well for a short while until somehow I deleted the bin. file from my dropbox, where it's currently storing all the information. After restoring the file, as soon as I attempted to use the program again, Terminal would spit out the following:

`-bash: /Users/ackerman/Dropbox/Thoughts/bin/t: Permission denied`

Having never encountered this before, I began looking in all the wrong places.. I thought something was wrong with my directory, then that maybe something was wrong with the Ruby code it's written in or else I had somehow broken my dropbox.. All of my theories were soon proven wrong, but unfortunately, I didn't really know how to figure the solution out. So after trying for some time, I decided to swallow my pride and ask my employer. Thankfully he was more than willing to help me out, so I'm hoping I can pass on his knowledge to help others who may encounter this!

Every file in Unix has three different permission options:

   __1. Owner Permissions:__ This determines the actions that the Owner can perform on the file.

   __2. Group Permissions:__ This determines the actions that a member of the group who owns the file, can perform.

   __3. Other (world) Permissions:__ This shows the actions that any other user can perform on the file.


So the best way to get a handle on what we need to change is to see the what the file we're working with's permissions are set to.

Once in the directory with the correct file. run the `ls -la` command if the file you need to modify is hidden, or simply the or `ls -l` command, if it's not. This will output something similar to this:

`drwxr-xr-x    4 ackerman  staff    128 Feb 19 11:32 mydir
-rw-r--r--    1 ackerman  staff    157 Feb 19 15:22 myfile`

First thing to notice is that the first character is either a `d` or an `-` which denotes whether the file is `d` a Directory or `-` a file.The next 3 characters designate __owner__ permissions, the three after that show the __group__ permissions and the final three in that segment show the permission for the __world/other.__

Reading them is quite simple.

>    __r:__ Indicates the user can __read__ the file.
>
 >   __w:__ Indicates the user can __write__ to the file.
>
 >   __x:__ Indicates whether the user can __execute__ the file.

The rest of the information in the line isn't super pertinent to what I'm covering here. But for a greater understanding there are plenty of resources available if you need.

#But how do I change the permissions?

In order to change permissions, you'll have use the `chmod` (change mode) command, followed by whichever category you wish to change access for. 

__a:__ All Users
__u:__ The Owner user
__g:__ The Owner Group
__o:__ Others (Neither U, or G)

This is coupled with one or more of three different operators.

__+__ Adds the designated permission to a file or directory.
__-__ Removes the permission.
__=__ Sets the permissions.

And followed by the permission action we covered before. (__r, w or x__). Here are some examples of this.


__chmod a+r *myfile*__ files are readable by all
__chmod a-r *myfile*__ files cancels the ability for all to read the file
__chmod a-rwx *myfile*__ cancels all access for all
__chmod g+rw *myfile*__ files give the group read and write permission
__chmod u+rwx *myfile*__ files give the owner all permissions
__chmod og+rw *myfile*__ files give the world and the group read and write permission 

This is all well and good for changing __file_ permissions, but what about __directories__? These need to be changed with a numbering system, and using it this way is called the absolute form. Each number is indicative of whom is getting permission to do what.

>400	Read by owner
>200	Write by owner
>100	Execute by owner
>040	Read by group
>020	Write by group
>010	Execute by group
>004	Read by others
>002	Write by others
>001	Execute by others

In order to use them, simply add them and place them after your `chmod` command and the file/directory name. This isn't just for directories, and can simply be used for editing individual files. You also don't have to worry about learning all these numbers, as you can use a [Permissions Calculator](http://permissions-calculator.org)

In example, here's what it took to fix my problem. All I wanted to do was to make it so that the Owner could Read (400), Write (200) and Execute (100) the file, So I ran `chmod 700 ./t`. When I used the `ls -la` command after this. You can see the file has been changed to allow me as the Owner full permission to do all I needed to the file. 

`-rwx------@ 1 ackerman  staff    63 Mar 29 09:46 t`

I hope this has been of some help to anyone looking for a basic understanding of Unix Permissions. Feel free to let me know if you have any questions!