Linux Basics Every Developer Should Know - Part 2

Gabriel Bonfim
4 min readDec 20, 2022

--

What the hell is Sudo? -About Permissions

This is the continuation of an Article Series about Linux, If you didn't check the first article, please go take a look!

1. Permissions and Entities.

Source: Linux Handbook

Now that we’ve seen the basic commands, it’s time to another important topic: Permissions. In Linux, all files and folders have three types of permissions they can possess:

  • Read
  • Write
  • Execute

The Read permission means the user can read the file, check its contents, but cannot execute it or alter in any way.
With the Write permission, the user can alter the file, but not execute it. It often comes along with the Read permission, since in most cases there’s no sense in writing to a file without being able to read it.
Last but not least, the Execute permission permitis the user to execute the file, without reading Its contents, or altering them in any way.

Those permissions can be assign to three separate entities:

  • Owner
  • Owner Group
  • Other

The Owner entity is the file/folder owner, a file/folder can only be owned by ONE user.
The Owner Group entity is a User Group assigned to file/folder, like the Owner there can be only ONE assigned, and here you will decide what permissions the Owner Group will have.
The Other entity means everyone else. Here you will decide what permissions someone who is not the owner, or is not in the Owner's user group, will have.

2. Checking and Changing Permissions.

But how can we check what permissions are assigned to a file? Well, we can use a command we already learned on our previous post, ls, with a simple option -l.

gabrielbonfim1@ubuntu-linux-22-04-desktop:~$ ls -l HomeInsides.txt
-rw-rw-r-- 1 gabrielbonfim1 gabrielbonfim1 83 Dec 13 08:17 HomeInsides.txt
Linux permissions.

The first dash will tell us if we’re checking a File or a Folder. A File will have a dash sign, and a Folder will have a letter ‘d’. The permission's settings are displayed in a form of string (‘-’,’r’,’w’,’x’), and are separated in three groups:

  • File Permissions for the Owner
  • File Permissions for the Owner Group
  • File Permissions for the Other Users

They represent the three forms of permissions:

  • r : Read
  • w : Write
  • x : Execute

In our example:

The Owner of the file can read and write to it, but not execute. The users on the Owner User Group can read and write to it, but not execute. Other users can only read the file.

3. Executing something as the Root User.

In the next sessions we’ll see how to change a file’s permissions, but in order to do that we need to have the permission to edit the permissions. (Makes sense, right?)

We can do that by temporary using the commands with the sudo prefix, which will tell our linux to execute the command as the Root User, The highest user in linux hierarchy.

4. Changing Permissions

We can change a File’s Permissions with the command sudo chmod filename . If we want to add an execute permission to our file we can simply do sudo chmod +x filename , this will add to the owner’s permissions the possibility to execute the file.

Like you may have thought, if we want to take some permissions off, we can do sudo chmod -w filename , after that our File's Owner won't be able to change the file in any way.

If we want to add or remove permissions for the other entities we can specify with a character before the + or - sign.

  • o for the Owner
  • g for the Owner Group
  • u for the rest of the Users
  • a for all of them

Again, in our example, if we want to change the permissions for the rest of the Users, we can do sudo chmod u+rwx filename , which will make it possible for everyone to do anything they want with the file, Read, Write or Execute.

5. Changing Owner

This is rather simple, if we want to change the Owner of a File, we can use chown . Just do sudo chown user filename and the owner of the file will change. Do it with cautions, as it may prevent other users to access the file.

And that was It for our second part! Thank you for following up to here! If you guys liked my content, consider following me! I'm a new writer to Medium and it helps me alot! See you on our next part!

--

--

Gabriel Bonfim
Gabriel Bonfim

Written by Gabriel Bonfim

Just an Enthusiast DEV trying to learn everything from everything. I write about MacOS, Python and Javascript.

No responses yet