How to Create and remove users on ubuntu server 20.10

Louis SanchezAugust 14th 2021, 3:59

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

sudo useradd -d /home/username -m username

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

ls -l /home

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

sudo passwd username

adduser

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

sudo adduser username1

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.

sudo userdel username1

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

ls -l /home

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

sudo userdel -r username

Understanding the /etc/passwd file

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

cat /etc/passwd

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

sudo cat /etc/shadow

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

sudo cat /etc/shadow | grep root

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:

sudo su -

Step 11 : Switching to another user account

sudo su - <username>