Skip to main content

Command Palette

Search for a command to run...

The Linux Filesystem, Users and Groups - Day 2

Updated
12 min read
The Linux Filesystem, Users and Groups - Day 2

Why again?

I know, that on day 0 of this series I wrote a section in my article which was regarding the Linux File system. Then why again? Well, that was for basic understanding of the Linux filesystem. This will be kind of a deep-dive to the Linux filesystem where you’ll get to know where which thing is present. We will dive into the folders of linux where your configurations are present and have a look at them. So, let’s start:

Let’s start

When you open your terminal, to see where your terminal is open type pwd

Normally you’ll be in the /home/<yourusername> directory. The cd command will help you to change directory right? If you type cd .. it changes the directory to the previous directory. Let’s keep doing it till we reach the parent directory

cd .. # changes to /home/
cd .. # now you are in /
cd .. # doesn't have any effect as you are in the topmost root directory

Now that we are in the root directory, if you do an ls you’ll be able to see a bunch of files

We all know that in Linux based systems, everything is treated like a file. The commands that you type, the devices, network…. everything is treated like a file. So, where does these file lie?

When you do ls inside the / directory, you’ll be able to see a few directories. Some of them go by the names bin, sbin, tmp, dev, usr. These contain the important files that are needed to run your Linux kernel.

bin and sbin

If you do ls bin you’ll be able to see all the list of commands that you use including the one you used rn i.e ls.

Also, there is a dir named sbin. It is a shorthand notation for super bin. It contains commands that administrators can use. One of them is adduser. Here’s a small assignment: Read the manual of the adduser command it will help you to flex your knowledge in front of your friends

Now, let’s jump to the /usr/ dir

usr

Let’s cd to the user dir and see what’s present there by executing ls.

Hmm… There is a bin and sbin there also. If you cd into them and do an ls, you’ll be able to see some new files along with some same files like ls in bin and adduser in sbin. So which one is the real one?

Well, tbh both are real but why are the duplicates present?

Basically /bin holds essential binaries needed for booting and system recovery, while /usr/bin contains regular user applications. Originally, /usr could be on a separate partition, so /bin had to remain available even if /usr wasn't mounted yet

Quick review of other directories in the / dir

  • boot contains the files essential for booting

  • var contains the log files and essential web application files

  • dev contains files about devices

  • lib contains the essential shared utility files

Users in Linux

In Linux, when you login, you login as a user right? A user that you created while installing your OS. You can add new users and also check the name of the current user.

Who are you?

The terminal that you are using has an associated user account with it. To know about the user of the current login session, you can type whoami. It will show you the username of the current terminal session

Adding new user

To add new user, use the adduser command. But remember, it is not a normal command so make sure to use sudo

But everything is treated like a file right? So where are the users? To answer your question let’s jump to the home dir

The home dir

Navigate to the / dir first. Now, type ls home. There you’ll be able to see the name of users that you added earlier. It is the home where every user of your Linux system lives

Where is root?

Now, the administrators will not be normal users right? So, where do they live?

Well, they have a different home and the place where they reside is the /root directory. So, when you login into your Linux system normally through your gui, you login as a user. For getting root access to the terminal type sudo su root. And you’ll be able to see that the $ change to #. Which means now you are the root user i.e the administrator or you can call yourself the god of your Linux OS

Better way to check users

One way to look at users you created is by navigating to the home dir and listing the directories. But there is a better way to look at how many users are being created and are present in your machine. That’s what this command does

cat /etc/passwd

This file consists the list of all the users those are present in your system. You’ll be able to recognize the last one as it is the user that you just created.

If you check clearly, the last user will be like this:

test:x:1001:1001:Test,,,:/home/test:/bin/bash

Can I access passwords

The passwords of the user are present in a different file which is in /etc/shadow. But to access that you’ll need sudo permissions so make sure that you use the sudo command

sudo cat /etc/shadow

On typing this you will get the passwords of all the users in your system, the last one for me is this:

test:$y$j9T$A/LaMJOSt9gwnFARUecio0$/YjAeHOxHXmkUvbK5cBRHA0OZvJ0KxPtSq0uUUt4ve3:20205:0:99999:7:::

This is not the password I entered, this is the hashed version of it. So yes, you can definitely access the passwords but you can only access the hashed passwords

Let’s get back to the user

I am typing out how the user looked when I ran cat /etc/passwd again:

test:x:1001:1001:Test,,,:/home/test:/bin/bash

Let’s decode the components present here

  • The first word test is its username

  • x represents the password

  • Then comes the user id(UID) i.e 1001

  • Then is the group id i.e 1001

  • Then the name

  • Then the commas which were meant to be the extra set of metadata which we didn’t provide for which it doesn’t hold any value

  • Then is the home directory where our user lives i.e /home/test

  • Then the path of the shell that our user will use i.e /bin/bash

So the format is

username:x:UID:groupId:Name,data1,data2,data3,data4:homeDir:shellDir

But why user Id and group Id? You’ll get there soon but as of now let’s just keep this in mind that when you add a user using the adduser command, a group is being created and then a user inside that group is being created. In our case, the group test is being created and then the user test is being created.

useradd

There is a difference between the adduser and useradd command. The adduser command does a lot of work by asking you information about the user like the password and other metadata. It also creates a group

But the useradd is just like you and me. It’s lazy. So let’s see what it does:

sudo adduser test1

As you can see, the useradd command didn’t ask for any password or anything it just created it automatically. But wait, is it added to the users list? Let’s see the file where user info is present. So, type in cat /etc/passwd

Well, I definitely see test1 but there are a few things which are different:

  • The name and additional data is not present

  • The shell is /bin/sh and not bash

That’s why it is called lazy. It comes in handy when you want to add multiple users so that you don’t have to spend time configuring them

We didn’t give it a password right? Let’s check the /etc/shadow file and see if it is present using the command sudo cat /etc/shadow

Well, we definitely see an entry for our user but we don’t see a password. Okay let’s fix it by giving test1 a password

passwd

To set the password for our user test1, we’ll use the passwd command and obv we’ll have to use sudo because this is a sensitive command which requires administrator previllages

sudo passwd test1

Now, let’s check the /etc/shadow file:

sudo cat /etc/shadow

There we go, we see something. But the shell assigned to test1 is sh and not the bash shell. Let’s fix that

usermod

To modify a created user, we use the usermod command. To know about the usermod command, type out usermod -h. There you’ll be able to see the option -s to modify the shell.

To modify the shell, just type out

sudo usermod test1 -s /bin/bash

Now, if we check the /etc/passwd file to see the configs, you’ll be able to see that the shell has been updated

su command

Now to change the user in your running terminal session, you will have to use the su command. It basically means switch user.

sudo su test1

If you don’t type test1 then you’ll be logged in as the root user which is not a good idea anyways cuz if you mess something up as the root user, it will be bad for your system

Well, now we are logged in as the user test1. Let’s check the passwords file once again:

Well, we get an error. It says that test1 is not in the sudoers file. What is that? Let’s explore

The sudoers file

The sudoers file is a very very important file in your Linux filesystem that you don’t want to mess up by any means because if you do so, your whole linux system can get messed up. So, there are multiple text editos available to view and edit the sudoers file including vim, nano. But the safest way is to use visudo. So let’s exit as test1 and get back as the normal user and check the file

sudo visudo

You might want to scroll down a little till you see this:

Under the user previllage specification, you can see which users have the sudo access and then you can see the syntax for which group has the sudo access. Let’e quickly get through the syntax for a single user for now and experiment with it for a bit:

# to give test1 all sudo command previllage
test1 ALL=ALL

# to give access to only one command previllage you can use the path of that command in the sbin dir
test1 ALL = /sbin/adduser # only gives access to the adduser command

For now, I’ll give test1 all previllage. So once I write test1 ALL=ALL, I’ll press Ctrl+S to save and Ctrl+X to exit.

Now let’s switch the user to test1 and chek if we can execute the sudo command or not

Well, yes we could add our user

userdel

Now let’s delete our newly created user cuz we don’t need hello rn. To do that, let’s use the userdel command. You can again check the passwd file to check if the user has been added or not:

sudo userdel hello

Now, let’s exit out of that login session and login as the default user by typing exit. Now let’s explore groups.

Groups

As the name suggests, groups are simply sets of users who are combined into a single group. Let’s do some basic operations on groups

groupadd

To create a new group, we use the groupadd command. Let’s create a hello group

sudo groupadd hello

You can see all the groups in the /etc/group file. Just use this command and you’ll be able to see all the groups in your system

cat /etc/group

Here are my last 3 lines of that command:

Now let’s add a few users. For now, I’ll just add world1 and world2 and also give them a password. Now, let’s add them in the group

usermod -aG

To add users into a grop, we use the usermod -aG <groupname> <username> command. In our case, to add user world1 to our group, we’ll have to use this command

sudo usermod -aG hello world1

Similarly, add world2 also. Then, when you see the file /etc/group you’ll see this:

Thus world1 and world2 has been successfully added to this group

Since we didn’t update the sudoers file for world1 and world2, so it will naturally not have sudo access. You can check it

But instead of that, we are now going to give the group hello sudo access. So that world1 and world2 both have sudo access. Let’s openup the sudoers file using sudo visudo command and add this line on the groups section

Basically when you type %<name> it specified the group name. Now you can easily execute sudo commands in that group. Check it yourself by switching the user.

Now, let’s try to remove the users from the group

gpasswd

To remove the users from a group, we’ll have to use the gpasswd command. Yes, I know the name says something different, but we have no choice. Use this command to remove user world1 from group hello.

sudo gpasswd -d world1 hello

Now, check if world1 still has sudo access or not, also try to remove world2 from the group

groupdel

For deleting a group, we can use the groupdel command. In our case to delete the group named hello, we’ll use:

sudo groupdel hello

Summary

Let me sum up everything so that you can only use this section to revise the commands quickly:

Linux Filesystem Structure

  • The root directory / is the top-level directory containing essential system folders

  • /bin and /sbin contain essential commands (binary executables)

    • /bin has regular user commands like ls

    • /sbin (super bin) contains administrator commands like adduser

  • /usr/bin vs /bin: Both contain commands, but /bin has essentials needed for booting, while /usr/bin has regular applications

  • Other important directories:

    • /boot: Files essential for booting

    • /var: Log files and web application files

    • /dev: Device files

    • /lib: Essential shared utility files

    • /home: Where user directories are stored

    • /root: Home directory for the root user (administrator)

User Management

  • whoami: Shows current user of terminal session

  • adduser: Creates a new user with full configuration prompts

  • useradd: Creates a new user with minimal configuration (the "lazy" version)

  • User information is stored in:

    • /etc/passwd: Contains user details (username, UID, GID, home directory, shell)

    • /etc/shadow: Contains hashed passwords (requires sudo access)

  • passwd: Command to set/change user passwords

  • usermod: Modifies user settings (like changing shell with -s option)

  • su: Switches between users in the terminal

  • userdel: Deletes users

Group Management

  • Groups allow organizing sets of users with similar permissions

  • groupadd: Creates a new group

  • Group information is stored in /etc/group

  • usermod -aG: Adds users to a group

  • gpasswd -d: Removes users from a group

  • groupdel: Deletes a group

Sudo Access Management

  • The sudoers file controls which users/groups have admin privileges

  • Best edited with visudo for safety

  • Individual users can be given sudo access with: username ALL=ALL

  • Groups can be given sudo access with: %groupname ALL=ALL

  • Access can be limited to specific commands

Conclusion

Now you know how to poke around the file system, add users when your computer gets lonely, and play permission games with sudo. Not too shabby for a day's work!

Just remember - when in doubt, there's always Google... or you could just try random commands until something works. That's the Linux way! (Kidding, please don't do that.)

Thanks and see ya