Linux User Switching

LinuxBeginner
Practice Now

Introduction

Linux is a multi-user operating system where multiple users can work simultaneously. Understanding how to navigate between different user accounts is a fundamental skill for system administrators and Linux users. This lab introduces you to user switching in Linux using the su (substitute user) command.

In this lab, you will learn how to create a new user, switch between users, perform operations as different users, and return to your original user account. These skills are essential for system administration tasks, security practices, and understanding user permissions in Linux environments.

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

Switching Users with the su Command

Now that we have created the apprentice user, let's learn how to switch to this user using the su command.

Understanding the su Command

The su (substitute user) command allows you to switch to another user account during a login session. There are two common ways to use the su command:

  1. su username - Switches to the specified user but keeps the current environment variables
  2. su - username - Switches to the specified user and also loads that user's environment

Using the second option with the hyphen (-) is generally preferred as it provides a complete environment switch, making it feel like you're actually logging in as that user.

Switching to the Apprentice User

Let's switch to the apprentice user with a complete environment switch:

su - apprentice

You'll be prompted for the apprentice user's password. Enter the password you set during user creation (password123):

Password: password123

After successful authentication, your command prompt will change to indicate that you are now logged in as the apprentice user. The prompt might look something like this:

apprentice@ubuntu:~$

Verifying the User Switch

To confirm that you are now operating as the apprentice user, run the whoami command:

whoami

The output should be:

apprentice

You can also check the current working directory with the pwd command:

pwd

The output should show that you are in the home directory of the apprentice user:

/home/apprentice

Creating Files as the Apprentice User

Now that we're logged in as the apprentice user, let's create some files to demonstrate that actions performed in this session are executed with the apprentice user's permissions.

Creating a Directory

First, let's create a project directory in the apprentice user's home directory:

mkdir -p ~/project

The -p flag ensures that the command doesn't produce an error if the directory already exists. There won't be any output if the command executes successfully.

Creating a Text File

Now, let's create a text file in the project directory:

echo "This is my first file as the apprentice user." > ~/project/apprentice-journal.txt

There won't be any output when executing this command, but it will create a file with the specified content.

Viewing the File Contents

Let's check the contents of the file we just created:

cat ~/project/apprentice-journal.txt

The output should be:

This is my first file as the apprentice user.

Checking File Ownership

We can also verify that the file is owned by the apprentice user:

ls -l ~/project/apprentice-journal.txt

The output should look something like this:

-rw-rw-r-- 1 apprentice apprentice 44 Oct 15 13:45 /home/apprentice/project/apprentice-journal.txt

Notice that both the user and group owner of the file are set to apprentice, confirming that the file was created with the apprentice user's permissions.

Switching Back to Your Original User

After completing tasks as the apprentice user, it's time to switch back to your original user account. This is a common practice when you temporarily need to perform actions as another user and then return to your primary account.

Exiting the Apprentice User Session

To return to your original user session, simply type exit or press Ctrl+D:

exit

You should see the command prompt change back to your original user prompt, which might look something like:

labex@ubuntu:~/project$

Verifying the User Switch

To confirm that you've successfully switched back to your original user account, run the whoami command:

whoami

The output should be:

labex

Accessing Files Created by Other Users

Even though you've switched back to your original user, you can still access files created by the apprentice user if you have the appropriate permissions:

sudo cat /home/apprentice/project/apprentice-journal.txt

The output should show the file contents:

This is my first file as the apprentice user.

Comparing User Context

Now that you've experienced switching between users, let's demonstrate the difference in user context by checking the home directory paths:

echo "My home directory is: $HOME"

The output should show your original user's home directory:

My home directory is: /home/labex

If you were to switch back to the apprentice user and run the same command, the output would be different:

su - apprentice                    ## (Don't execute this now, just for illustration)
echo "My home directory is: $HOME" ## Would show: My home directory is: /home/apprentice

This demonstrates how the su command with the - option completely switches the user environment, including environment variables like $HOME.

Summary

In this lab, you learned essential Linux user management skills:

  1. Creating a new user account with the adduser command
  2. Granting sudo privileges to a user with the usermod command
  3. Switching between users using the su command
  4. Creating files as a different user
  5. Verifying user context with commands like whoami and checking environment variables
  6. Switching back to your original user with the exit command

These skills are fundamental for Linux system administration and daily operations in multi-user environments. User switching allows administrators to perform tasks with different permission levels and maintain proper security practices by avoiding constant use of the root account.

Understanding user management in Linux helps you:

  • Maintain system security by using appropriate user permissions
  • Troubleshoot permission-related issues
  • Test applications and configurations from different user perspectives
  • Manage multi-user systems effectively

As you continue your Linux journey, these skills will serve as a foundation for more advanced system administration tasks.