Understanding Linux Users and Creating a New User
In Linux, each user has their own account with specific permissions and home directory. Before we can switch between users, we need to create a second user account.
Creating a New User
Let's create a new user named apprentice using the adduser command. This command requires administrative privileges, so we'll use sudo to execute it with elevated permissions:
sudo adduser apprentice
When you run this command, you'll be prompted to set a password for the new user and provide some optional information. For this lab, let's set the password to password123 and you can press Enter to skip the optional information fields:
Adding user `apprentice' ...
Adding new group `apprentice' (1001) ...
Adding new user `apprentice' (1001) with group `apprentice' ...
Creating home directory `/home/apprentice' ...
Copying files from `/etc/skel' ...
New password: password123
Retype new password: password123
passwd: password updated successfully
Changing the user information for apprentice
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] Y
Granting Sudo Privileges
Now, let's give the apprentice user sudo privileges so they can perform administrative tasks:
sudo usermod -aG sudo apprentice
This command adds the user apprentice to the sudo group. There won't be any output if the command executes successfully.
Checking User Existence
To verify that our new user was created successfully, we can list the contents of the /home directory:
ls -l /home
You should see a directory for the apprentice user:
total 8
drwxr-xr-x 5 apprentice apprentice 4096 Oct 15 12:34 apprentice
drwxr-xr-x 5 labex labex 4096 Oct 15 12:00 labex
We can also check the list of users in the system by viewing the /etc/passwd file:
grep apprentice /etc/passwd
This should show you information about the apprentice user:
apprentice:x:1001:1001:,,,:/home/apprentice:/bin/bash