Getting to Know Linux File Permissions & Ownership

Every file & directory in Linux stores specific permissions. These permissions are represented by 9 bits

You might notice the screenshot from my last post where I discussed the top 5 most used Linux commands. The first character ‘d’ on the first line represents the file type, in which this case is a directory. Next, we have ‘rwx’ these 3 bits are for the user permissions – r = read, w=write x=execute. This means that the user has full permissions over the file. The next 3 bits are for the group permissions – r-x – which in this case, the group is able to read & execute. Lastly we have all other permissions which can read & execute.

So what if we wanted to change the permissions of the file? First we need to understand that each bit or entity is represented numerically.

Read4
Write2
Execute1

From the table you can see how the permissions are represented. If we take the example from above, our group permissions are totaled to 5 – read = 4, write = 0, execute = 1


Changing Permissions

To change the permissions of the file or directory we use the chmod command. To be able to use the chmod command you either need to own the file or be logged in as root. Chmod has a few variations for managing permissions, so I will show those here and you can pick the one that you think would be most beneficial to you.

chmod u=rwx, g=rx, o=rwx <file name or directory>

chmod ug=rwx, o=rw <file name or directory> this puts the user & group together as noted by chmod <ug> and then other

chmod 777 <file name or directory> – this would put full permissions on user/group/other. This one is my favorite since you’re not having to type as much.


File Ownership

When you create a file you’re considered the owner of the FILE and have the default permissions – 666

If you’re creating a directory you have default permissions of 777

As you can see from the screenshot the section highlighted in red is the who owns the file and the section column in blue is the group. Be default the file will be created under your primary group.

To change the ownership of the file or directory, we use the chown command

chown <user>:<group> <file name or directory>

As you can see from the screenshot above I was able to change the user from ‘xetekk’ to ‘root’

Additionally, we can also use the chgroup command to change the group of files or directories

chgroup <group name> <file name or directory>


Conclusion

In this post we discussed how to use the chmod, chown, & chgroup commands to manage file permissions and ownership. These commands help secure your files from unwanted changes by other users or groups.

Tagged:

Related Posts