Basic User Management in Linux (Users and Groups)


linux terminal commands to manage users and groups

Hey there, folks! Today, I'm gonna chat with you about some basic user management in Linux. I'm using Ubuntu for this, so let's dive right in!

Add a User

First things first, to add a user, you gotta have those super user permissions. So, here's what you do: just type in the following command:

sudo adduser new_user_name

For example, I'm adding the user "piyumi" to my system:

sudo adduser piyumi

When you run that command, it'll ask you to enter a password and some other details like the full name. Once you've done that, your new user will be good to go!

By the way, when you create a new user, it automatically creates a group for that user.

Let's Verify if the User Got Created

The details about users are tucked away in the /etc/passwd file. Usually, user details are found at the end of this file. So, we use the tail command to display the last 10 lines of this file. You can also use the cat command with grep to search for a specific user:

tail /etc/passwd

Now, you should be able to spot your shiny new user in there!

Logging into a User

To log into the new user, you need to use the su command. This command will switch the user. Here's how you run it:

su - user_name

We use the - command to land right in the user's home directory. Otherwise, we'd be stuck in the same directory as the current user.

Once you're logged in, you can do all sorts of things with that user. For instance, if you want to change the password, just run the `passwd` command. It'll ask for the old password and the new one to update it.

Logging Out

To exit from the current user and go back to the previous user, just run the exit command. That'll log you out of the current user.

Creating Groups

Now, when it comes to managing permissions in Linux, you'll need groups. Only super users can create groups. So, let's hop over to our main user or use a super user and run this command:

sudo addgroup new_group_name

For instance:

sudo addgroup tyrolead

This creates a new group, and you can spot it in the /etc/group file. Let's take a peek there to confirm:

tail /etc/group

Add user to a group

Now, you should see the new group listed with a group ID (GID). At this point, it won't have any users. To add users to this group, use this command:

sudo usermod -G group_name/GID user_name

For example:

sudo usermod -G tyrolead piyumi

And if you head back to check out /etc/group, you'll see that "piyumi" has been added to the group.

Removing a User

Removing a user is a piece of cake with the deluser command. To do it, you'll need super user permissions, of course:

sudo deluser user_name

Seeing the Current User

To find out who the current user is, you can use the `whoami` command. It'll display the current user's name right there in the terminal. Handy when you're feeling a bit lost!

Conclusion

Alright, that's a wrap for today's article, my friends! I'd love to dive deeper into managing permissions in a Linux system, but we'll save that for another day. Understanding users and groups is a crucial foundation, so keep that knowledge handy. Catch you in the next article!

Post a Comment

0 Comments