How to Create and remove users on ubuntu server 21.04

Creating users

Creating users in Ubuntu can be done with one of either of two commands: adduser and useradd

useradd

Step 1 : First, Creating a User with specific home directory

Step 2 : If you list the storage of /home, you should see a folder listed there for our new user:

Step 3 : To create a password for the user, we can use the passwd command.

adduser

Step 4 : Execute adduser along with a username for a user you wish to create.

I was asked for the password (twice), Full Name, Room Number, Work Phone, Home Phone and Other

Removing users

Step 5 : To remove a user account, we'll use the userdel command.

The userdel command does not remove the contents of the user's home directory.

Step 6 : We can see that the files for the username1 user still exist

Step 7 : If you do actually want to remove a user's home directory at the same time you remove an account, just add the -r option

Understanding the /etc/passwd file

Step 8 : First, let's go over the /etc/passwd file.

Each line within this file corresponds to a user account on the system.

1 : Username

2 : Password (encrypted)

3 : UID (User ID)

4 : GID (Group ID)

5 : User ID Info

6 : Home directory

7 : User's shell

Understanding the /etc/shadow file

Step 9 : Let's take a look at the /etc/shadow file. We can use cat to display the contents

Step 10: Execute the following command to see the root user account entry in /etc/shadow:

Whenever you create a user account on a modern Linux system, the user's password is encrypted (an x is placed in the second column of /etc/passwd for the user), and the actual password hash is stored in the second column of /etc/shadow

Switching users

Step 10 : The following command will allow you to switch to root from a user account that has sudo access:

Step 11 : Switching to another user account