Hey everyone, welcome to our channel
In this video, we are going to see how to add users in Linux via the command line interface
also known as the CLI, as well as the graphical user interface also known as the GUI
Now to demonstrate this, we are going to use a Debian based system as well as a Red Hat system
Now for the Debian based system, we have Ubuntu 22.04 and for the Red Hat system, we are going
to demonstrate using Rocky Linux 9. Now in Debian or Ubuntu systems, the add user command is the command that is used to add
a user to your current Linux system. Now the add user command is an interactive tool that lets you specify the user details
when adding the user. The syntax for adding a user using the add user command is as follows
So on your command line, run the command sudo add user followed by the name of the user
So in this case, we are going to add a user called Bob. And by the way, if you're running this command as root user, simply skip sudo and just specify
add user Bob. Alright so we are using sudo here because we are running the command as a sudo user
Alright so hit enter. Now as you can see, a couple of things happen
So this command creates or adds a new user called Bob. Now the new user is given a UID or a user ID, which in this case is 1001
And the command also creates a group that is named after the user
So this is the primary group that takes after the user. So as you can see, the user is created as well as a group that is called Bob
Okay, now later, the home directory for the user is created. So this is the path to the home directory
And the files from the etc scale directory are copied to the user directory
So here we are prompted for a password. So I'm going to specify the user's password
And this is the password they're going to use to log in. Alright
Now I'm going to confirm the password. Okay. Then I'm going to hit enter
And once you hit enter, you're going to be prompted to provide new values for the user
So here I'm going to type the full name. So in this case, I'm simply going to provide Bob and hit enter
So next you're going to be prompted to provide other details. Okay
So this is at your discretion. So I'm just going to hit enter to skip providing these details
All right. But feel free to provide the details if you have
Okay, so once you're done, you'll be prompted to check or verify whether the information
is correct. So in my case, I'm satisfied and I'm going to press Y on my keyboard and press enter
to confirm. All right. So at this point, the user Bob has already been created
And to confirm this, I'm going to run the command ID followed by the user Bob like that
and hit enter. So as you can see here, we have the user, the UID, which is 1001
We also have the GID, which is just the same as the UID, 1001
And the user belongs to a group called Bob. Okay. So this is the primary group that a user belongs to once they are added
All right. Similarly, we can check the etc password file. So this is a file that contains user details
And I'm going to pipe the output and filter a name called Bob like that
So here the hyphen I flag simply ignores case sensitivity. Okay. So I'm going to hit enter
And you can see we have a user called Bob. And this is the UID
This is the GID or the group ID. This is the path to the user's home directory
And this is their default shell. All right. Now I'm going to clear the screen
Now additionally, you can use the add user command alongside the shell argument to specify
a different shell that the user should use. So for example, let me assume I'm adding a user called Patrick
So I'm going to run the command sudo add user. Now Patrick. Okay
Now I can specify the shell using the shell option like that
Then I can specify the shell, for example, the bond shell
Like that. Okay. So here I'm specifying the bond shell. Now the bond shell is typically the original Unix shell and it's a minimal shell designed
for scripting and executing simple commands. Okay. Now I'm going to hit enter
So as you can see, we go back to the same drill. Okay. So let me provide the user's password
Then I'm going to confirm it. Okay. So here I'm going to say Patrick
And I'm going just to press enter to accept the defaults and finally hit Y
All right. So now let's check the details for Patrick. So once again, I'm going to run the command id patrick
So as you can see, we have a different UID and GID. So this is 1002. Okay
And the user Patrick belongs to a group called Patrick. Now let's check the etc password file
So once again, I'm going to filter this information using grep
And once again, you can see we have Patrick's details. Okay. Now in this case, you can see now Patrick is using the bond shell. Okay
Now by default, the add user command places the user in the bond again shell
All right. So once again, shell is simply an improvement of the bond shell
All right. So with the shell option, you can specify the shell that a user belongs to
All right. Now let's take another example. Now you can, apart from specifying the shell, you can also specify the path to the home
directory using the home option. So for example, I'm going to add a user called Alice
So I'm going to run the command sudo add user Alice. Okay
Now by default, the home directory will be forward slash home forward slash Alice, but
you can also provide a different path to the home directory. For example, here I can say home, then forward slash home
Then I can provide, let's say, forward slash Janice instead of Alice
All right. And as we said earlier, if we opt out or we leave out the home option, the add user command
will simply default to forward slash home forward slash Alice. So in this case, we have specified forward slash home forward slash Janice
All right. So let me hit enter. So again, I'll be prompted for the user's password
And I'll confirm it. Okay. Then here I'm just going to say Alice
And I'm going to hit enter to accept the defaults and finally type yes
All right. Now when I use the cat command to check more details in the etc password file, I'm going
to find that the user has a different path to the expected home directory
So let's do so. So here I'm going to use grep once again, and I'm going to check for Alice
All right. So as you can see, in this case, we have forward slash home forward slash Janice. Okay
And this is a path that we specified here. All right. So those are just a few examples of the add user command
And this will work on across all Debian based distributions, including Ubuntu. Okay
Now let's switch over to the graphical interface and see how you can add a user
Adding a user using the graphical interface is a pretty straightforward procedure
And in front of you is a graphical user interface. So I'm using, I'm running Ubuntu 22.04 as you can see with the iconic Jamie Jollifish logo
Now to get moving, simply navigate to the left, the bottom left corner here, where you're
going to see the show applications icon. Now click here and then you navigate to settings
All right. So I'm going to select settings. So this is going to open the settings window as you can see
Now on the settings window, just navigate to this search icon here located at the top
left corner and here search for users. Okay. So this will populate an option for users
So you're going to click on this. So currently on my system, I have a user called James as you can see
So this is the only user, the only login user, sorry, that is configured in this system
Now to add more users, I need to unlock. So I'm going to click unlock
So this will prompt me for authentication. So here I'm going to provide my user password
And once I authenticate, I'll now be able to add new users to the system
So now you can see the add user button is now active
So I'm going to create this add user button. And here I'm going to specify the user's full name
So here I'm going to add a user called Linux Techie. Like that. Okay
So as you can see the full name and the username, the username gets out of field
All right. So now I'm going also to set the user's password
And by the way, you can also select to allow the user to set a password when they next login
All right. But for now, I'm going to set a password. All right
So here I'm going to specify the user's password. Okay. So be sure you provide a strong password and also confirm it
And now with the password confirmed, just hit or click add. Now when you click add you can see now we have a new user called Linux Techie
Additionally, you can also add the user to the administrator group. All right
Which in this case is the sudo group. Now you can just toggle this like that
All right. So now the user belongs to the administrator group. All right. Okay
So now that's about it for adding a user using the graphical user interface
So here we can close this window. All right. So that's it in how to add a user using the CLI and the graphical user interface on Ubuntu
which is a Debian based system. So now let's switch over to Rocky Linux 9 and see how you can achieve the same procedure
On Red Hat systems such as Rocky Linux, the command for adding users is the useradd command
So the useradd command takes the following syntax. So here I'm running as a sudo user
So I'm going to run sudo. Then I'm going to invoke useradd command followed by the username that you want to add
So here I'm going to add a user called Bob. Then I'm going to hit enter. Okay
So as you have seen, the useradd command, unlike the add user, does not provide an interactive
way of adding a user. So immediately after adding the user, you need to configure a password for the user
To do so, simply run the command sudo password followed by the username Bob
Then I'm going to hit enter. And once you do so, you're going to be prompted for the user's password
So here I'm going to provide Bob's password. Then I'm going to confirm it. Okay
So as you can see on your screen, the user has successfully been added
And you can confirm this by running the id command followed by the user Bob. Okay
So as you can see, the user Bob has a UID of 1001, which is the same as the GID and
also belongs to a primary group called Bob. All right. Now, you can also use the useradd command to provide a custom directory for a user
So for example, let's assume you're adding a user called Alice. So the command will be sudo useradd
So use the hyphen D to provide a custom directory then followed by the home directory that you
want to provide. So in this case, I'm going to say slash home slash Janice. Okay
Then I'm going to provide the username. So in this case, this is the user called Alice
Then I'm going to hit enter. All right. Now without adding a password, let's check the attribute of the user Alice
And to do so, I'm going to check the Etsy password file, which contains information
about users. Then I'm going to filter the name Alice, as you can see
So as you can see here, the user Alice has a custom home directory at slash home slash Janice
All right. Now remember that without specifying the hyphen D option, the useradd command by default
places the user in the default user home directory. Okay. So for example, let's see the case with Bob
So if we check the details for Bob, you can see we have the default home directory, which
is slash home slash Bob. All right. So the hyphen D option allows you to specify a different or a custom home directory
So in this case, instead of slash home slash Alice, we have slash home slash Janice
All right. Okay. Let me clear the screen. Now similarly, you can specify a custom UID using the hyphen U option
So for example, let's create a user called Alex. So we're going to have sudo useradd
Then the hyphen U option will allow you to specify a custom UID
So here I'm going to specify 1004. All right. So I'm going to specify Alex, which is a user that I'm going to add
All right. Now, let me check using the ID command. So I'm going to run ID Alex
So as you can see, we get the UID as 1004, which is the same as the GID
All right. Okay. So the useradd command also allows you to create a user with a different login shell
As we have seen previously in Ubuntu, the default shell is the bornagain shell, but
you can also specify a different shell such as the born shell. Okay
So similarly here, you can provide a different or you can specify a different shell
So you can have sudo useradd. Now you can use the hyphen S option to provide a different shell
So here, let's say for example, you want the user to acquire the, or to use the born shell
like that. Okay. And in this case, let's say we are creating a user called Tim
All right. So just hit enter. All right. Now when we view the etc password file, you realize that the shell has now, it indicates
the born shell for the user Tim. Okay. So let's verify this
So grep hyphen i Tim. So as you can see from the output, Tim now has a shell, which is indicated as a born shell. Okay
So also you can do something like creating a user with a comment. So for example, you can have sudo useradd
So you can pass the hyphen C option. Okay. So the hyphen C flag allows you to add a comment
For example, this is a sysadmin. So let's say we are creating a user called Paul and Paul is a sysadmin. Okay
So just hit enter. And now we can view the etc password file
And so here we have grep hyphen i Paul. And you can see here on this fourth or rather this fifth column here, we can see the comment
that you have just specified. So this is a sysadmin and this is what is specified here
All right. Okay, so now we have seen how to add a user on a Red Hat system
And in this case, we have used the Roki Linux 9 for demonstration
Let's now go over to the graphical user interface and see how you can add a user
As you've seen earlier, the graphical user interface or the GUI provides users with the
simplest way of adding users. And this is especially beneficial for those who do not have the experience or the expertise
of running commands on a Linux terminal. So here I am on Roki Linux
And as you can see, this is a GNOME environment. So just like Uniboot 22.04, all you need to do is to head over to the settings section
So here I'm going to click on activities. And here I'm going to click on this applications icon here, then I'm going to select settings
Okay, so regardless of the desktop environment you're using, you're going to navigate to settings
So as you can see, I already have some pre-existing users here
All right. So now what you need to do is to head over and click on the unlock button here
Okay, so this will prompt me for my authentication. So here I'm going to provide my password to authenticate
All right. So now to add a user, just like we saw in Ubuntu, I'm just going to click add user here
All right. So here I'll be prompted for a user. All right
So let's assume now we want to add a user called Collins
So I'm just going to type Collins and the username will be automatically autofilled
So this is a standard user. All right. Now here you can set a password or allow a user to set a password when they next log in
So this is entirely up to you. So I'm just going to set a password
Okay, so here I'm going to type in the user's password and I'm going to confirm it
All right. You can also choose whether the user will be a standard user or an administrator user
So here I'm going to select an administrator user. All right. And I'm going to click add
And as you can see, the user Collins has successfully been created
All right. And you can see this is an admin user. All right
The password has already been set. Okay. So all right. So that's how you create a user using the GUI on Rokka Linux 9
And as you have seen, it's pretty much similar to creating a user using a GUI when you're
using Ubuntu. So now with the creation of users out of the way, now let's see how you can manage users
by modifying their groups and other attributes. So see you shortly on the other end
So now we are back to the CLI and for the remaining part of this lecture, we are going
to be using Ubuntu because the commands here are going to be just the same as the ones
that would work on a Red Hat system. So in this section, we're going to see how you can change user attributes on a Linux system
And one of the most popular tools to use is the user mode command
Now the user mode command is a powerful utility used to modify user accounts or change the
properties of a user. You can use it to change various attributes such as the user's home directory, the login
name and even the password expiry date. All right, now you can add a user to the group by running the following user mode command
So you can run the command sudo user mode with a hyphen A and G flag
So the hyphen A appends the user to the group and the G option specifies the group name
So let's for example, say you want to add the user Bob to the sudo group
So you'd have to run the command sudo user mode hyphen A capital G
Then you provide the group that the user is going to be added on
So in this case, this is sudo and then you provide the username Bob
Now hit enter. So here I'm going to provide my password to enable this operation and I'm going to hit enter
All right, now to confirm this, you can run the command groups then followed by the user
in this case Bob and hit enter. So as you can see from the output, the user Bob now belongs to two groups
We have the primary group called Bob and the secondary group which is called sudo
All right. So you can add, let's now add another group. So here I'm going to add Bob to the group Patrick
So here I'm going to change this from sudo to Patrick and I'm going to hit enter
All right now once again, let me run the groups Bob command and you can see now Bob belongs
to two groups. We have sudo and Patrick. All right. Okay now you can remove the user from a group by running the gpassword command
So here you can run sudo then gpassword like that with a hyphen d option
Then you specify the username. So here it's Bob. So I'm going to remove Bob from the Patrick group
So I'm going to specify the group name Patrick here and I'm going to hit enter
All right so you can see from the output removing user Bob from group Patrick
Let's confirm this. So I'm going to run the command groups Bob and you can see now Bob belongs to the two groups
We have the primary group Bob and the secondary group sudo. All right you can also run the command id Bob to confirm this
So we can see Bob belongs to groups the initial primary group and the sudo group
All right let me now clear the screen. Also you can change the login username
All right so for example let's change the login username for Alice to something else
So here I'm going to run the command sudo user mode. So to change the login username use the hyphen l option like that followed by the new username
So for example we want to change the login user from Alice to Sandra like that
Now specify the old username which is Alice then hit enter. All right now to confirm this you can try logging in as user Alice and when you do so
you're going to find an error. So let's try now to log in as Alice
So you can see from the terminal that user Alice does not exist or the user entry does
not contain all the required fields. Okay and the reason for this is because we have changed the login name from Alice to Sandra
Okay now let's switch back to Alice. So here I'm going to run the command sudo user mode hyphen l then Alice
So we're just going to do the opposite of our previous command
Okay so now we are renaming Sandra to Alice. All right so in this case the login user will be Alice instead of Sandra
I'm going to hit enter. Now let me try to log in as Alice and this time around the login will be successful
So you'll be prompted for the password. Okay so once I provide my password I should be able to log in as Alice and I can confirm
this by running whoami Alice. All right okay now let me exit
All right so also you can change the home directory of a user
So let me clear the screen. So earlier on we had changed the login directory for Alice from Alice to Janice
Let's confirm this. So I'm going to check the cat etc. I'm going to confirm the etc password entry for Alice
So here I'm going to specify Alice. So you can see we at some point we had changed the home directory for Alice to home Janice
as you can see here. So with the user mode command you can also change the home directory and to do so just
run the command sudo user mode then use the hyphen d flag
All right so this allows you to change to a new home directory and we are going to specify
this as home Alice like that. Okay then I'm going to specify the username so in this case it's Alice
Then I'm going to hit enter. Let's confirm this. So I'm going to run the cat command as shown and as you can see this time around we have
slash home slash Alice. So previously we had forward slash home forward slash Janice which has changed to forward
slash home forward slash Alice. All right also we can change the user's login shell using the hyphen s option
All right so let's check the let's check the login shell for Patrick
So I'm going to run the command cat xe password grep hyphen i Patrick
So here we have seen we can see the login shell is the born shell so we are going to
change this to born again shell and to do so I'm going to run the user mode command
with the hyphen s option. All right then I'm going to specify the login shell so in this case we are changing it from
born shell to born again shell like that. Then I'm going to specify the username in this case Patrick
And I'm going to hit enter. All right now let me confirm the entry
So I'm going to run the cat command again and as you can see we have switched from the
born shell to the born again shell. All right so those are a few commands that you can use with user mode to change the user attributes
All right so in the next section we're going to see how to delete a user from a Linux system
In Linux the userdel command is used to delete a user account and its related files
So the command takes the following syntax. So run the command sudo then followed by userdel followed by the username
So in this case let me specify a user called Bob. All right and I'm going to hit enter
So the user Bob has been deleted from the system. And to confirm this I can just run the command id bob
So as you can see there's no such user called Bob. However the home directory for the user Bob still exists
And to do so you can run the ls command like this
So as you can see we still have the home directory for the user called Bob
And to get rid of this directory you'd have now to run the command sudo rm-r with the
hyphen r option. Then you specify the home directory for the user Bob like this
All right so let me hit enter. So once again let me confirm that the home directory does not exist
So you can see we have removed the user Bob as well as its home directory
Now there's an easier way of deleting both the user and the home directory
And to do so you can run the command sudo userdel with the hyphen r option
So now let me delete the user called Patrick. So I'm going to specify Patrick like that
And I'm going to hit enter. Okay so as you can see we get a notification here
Patrick not found. All right so now let's check if the user Patrick exists
So I'm going to run the command id Patrick. So you can see there's no user called Patrick
Let's check if Patrick's home directory exists. So I'm going to run the ls command
And as you can see we do not have the home directory for Patrick
So if you want to remove the home directory of a user, pass the hyphen r option
Now let's see. Let's check out another example. So now how about forcefully deleting a user in Linux
So to forcefully delete a user you use the hyphen, the hyphen f option
And this is mostly used when you want to delete a user even though they're currently logged in
So now I'm going to check the currently logged in users by running the command w
So from the command you can see the user Mary is currently logged in and these are the details
So what I'm going to do I'm going to try and remove the user in the normal way
So let's see if we can remove the user using the normal user del command without any option
So if I try to remove the user Mary like that, you're going to get the notification
that the user Mary is currently logged in by this PID. Now to remove the user simply pass the hyphen f option
So I'm going to say user del with a hyphen f option then followed by the user in this
case Mary and I'm going to hit enter. So you get this notification here
Alright but don't worry about this. The truth is the user has been removed and you can check by running the command id Mary
So as you can see there's no user called Mary on the system
OK and we can also confirm this by running the cat etsy password
Then you're going to pipe this to grep hyphen i Mary. As you can see there is no entry for the user Mary
Alright so those are just a few examples of how you can delete a user in Linux
So you can see we use the user del command and you can use the hyphen r option to delete
the user in the home directory and the hyphen f flag to forcefully delete a user even though
they're logged into the system. And that's it for this lecture. Thank you for coming this far and in this lecture we have seen how to add a user in Linux
We have also seen how to change the user attributes using the user mode command and also how to
delete the user using the user del command and its various options
So thank you for coming this far. Subscribe to our channel and like this video
See you in the next lecture