Complete Guide to Switching Users in Ubuntu | Methods and Management via GUI & Command Line

目次

1. Introduction

What is User Switching in Ubuntu?

Ubuntu is a multi-user Linux distribution that allows multiple users to use a single PC or server. Therefore, the user account switching feature is widely utilized in various environments, from personal use to corporate, educational, and development settings.

By switching users, you can maintain individual work environments while ensuring that other users’ data and settings remain unaffected.

Situations Where User Switching in Ubuntu is Needed

There are several scenarios where switching users in Ubuntu becomes necessary. Below are some common use cases:

1-1. When Sharing a PC at Home

If multiple family members share an Ubuntu PC, user switching is necessary to keep each person’s desktop environment and settings separate. For example, separating a parent’s and child’s accounts helps maintain distinct environments for learning and work.

1-2. Use in Companies and Educational Institutions

In businesses and schools, a single PC may be used by multiple employees or students. In such cases, switching accounts is essential to ensure that each user retains their individual data and settings.
Additionally, system administrators often need to use privileged accounts (root access), making user switching necessary for administrative tasks.

1-3. User Switching in Server Management

On Ubuntu servers, it is common to use specific user accounts to run applications and services. For example:

  • Logging in as a regular user → Switching to administrator privileges only when needed
  • Switching to a specific user account to manage a particular system service

In these scenarios, switching users via the command line (CLI) is frequently required.

1-4. Using Different Users in a Development Environment

Developers may switch users to conduct testing. For instance:

  • Performing regular development work as a standard user
  • Testing the behavior of an application in a different user environment
  • Executing specific administrative tasks as the root user

Especially when verifying operations under different permission levels, user switching becomes crucial.

What This Article Covers

This article explains how to switch users in Ubuntu using both GUI (Graphical User Interface) and Command Line (CLI). It also covers the differences between sudo and su, switching users in an SSH environment, and troubleshooting common errors.

2. Basics of User Management and Switching in Ubuntu

Ubuntu as a Multi-User System

Ubuntu, as a Linux-based OS, allows multiple users to log in simultaneously. Each user is assigned a dedicated account with independent settings and data.

Separation of User Environments

In Ubuntu, each user has their own individual environment, including:

  • Home Directory (e.g., /home/username/)
  • Configuration Files (individual settings for applications)
  • Permissions and Access Control (file and command execution rights)
  • Running Processes (active sessions and background tasks)

This structure ensures that different users can share the same Ubuntu system without interfering with each other’s environment.

Types of Users in Ubuntu

Ubuntu has several different types of users. Understanding their roles helps in managing and switching users effectively.

Standard Users

Standard users have limited permissions and can perform general tasks.

  • Restricted from installing software or modifying system settings
  • Cannot access other users’ data without the appropriate permissions
  • Manages personal data and applications

Administrator Users (sudo Group)

Administrator users can temporarily gain superuser (root) privileges using the sudo command.

  • Can install software and modify system settings with sudo
  • The default account created during Ubuntu’s initial setup usually has sudo privileges

To check administrator users:

getent group sudo

This command retrieves a list of users in the sudo group.

Root User

The root user has full control over the entire system.
By default, Ubuntu disables direct root login for security reasons.

  • Using sudo is the recommended way to gain temporary root access
  • To enter a root shell only when necessary, use sudo su or sudo -i

Enabling the root user (not recommended):

sudo passwd root

Setting a root password allows direct login using su, but this increases security risks.

Key Considerations When Switching Users

There are multiple ways to switch users in Ubuntu. Choosing the right method depends on the environment and purpose.

Switching via GUI

For users operating in a desktop environment, GUI-based switching is the simplest method.

  • Switching users from the lock screen
  • Logging out and logging in as a different user
  • Using the system settings menu

Details on GUI-based switching are explained in the next section, “3. Switching Users via GUI”.

Switching via CLI (Command Line)

To switch users in the terminal, use the su or sudo command.

Using su Command

To switch to another user:

su [username]

To switch to the root user:

su -
Using sudo Command

To execute a command with administrator privileges:

sudo [command]

To switch to the root user:

sudo su

or

sudo -i

Session Retention and Impact of User Switching

  • When switching users via GUI, the previous user’s session remains active, keeping applications open
  • When switching via CLI, a new session starts, leaving the previous session unaffected
  • Using su without a hyphen retains the previous user’s environment variables, while su - resets them
年収訴求

3. How to Switch Users via GUI

In Ubuntu’s desktop environment, you can easily switch users using the Graphical User Interface (GUI). This method is intuitive and ideal for beginners who are not comfortable with command-line operations.

In this section, we will explain two main methods: switching users from the lock screen and logging out to switch users.

3-1. Switching Users via the Lock Screen

Ubuntu allows switching to another user while keeping the current session active. This is useful when multiple family members share a PC or when an administrator temporarily needs to use another account.

Steps to Switch Users Using the Lock Screen

  1. Click the system menu (power button icon) in the top-right corner of the screen
  2. Click the “Lock” button
  3. The screen will lock, and the current user’s session will be preserved
  4. Select “Switch User” from the login screen
  5. Choose a different user, enter the password, and log in

“Lock” Button

“Switch User” Button

Click “Switch User” on the bottom right of the screen

Select New User

Select the new user

Advantages of Using the Lock Screen

The previous user’s session remains active
Applications and work progress are preserved
Ideal for temporary user switching

However, if multiple users remain logged in simultaneously, memory consumption increases. This can slow down performance, especially on systems with limited resources.

3-2. Logging Out to Switch Users

Unlike switching via the lock screen, logging out before switching users completely ends the previous user’s session. This method is useful when you want to free up system resources.

Steps to Log Out and Switch Users

  1. Open the system menu in the top-right corner
  2. Click “Log Out”
  3. A confirmation dialog will appear—select “Log Out”
  4. The login screen will be displayed
  5. Select a different user, enter the password, and log in

Pros and Cons of Logging Out

Closes all applications and processes, freeing up memory
Reduces system resource usage
Unsaved work may be lost
Requires restarting applications after switching users

Click 'Log Out'
Confirmation Dialog
Login Screen

3-3. Important Considerations When Switching Users

Performance Impact of User Switching

  • Using the lock screen keeps applications running in the background, increasing memory consumption
  • On low-end PCs, multiple logged-in users can slow down the system
  • Heavy applications such as video editing or virtual machines require careful resource management

Saving Data Before Switching Users

  • Save any unsaved documents or files before switching users
  • Even if an app has an auto-save feature, manually saving is recommended
  • Pay special attention to browser tabs and text editors with unsaved content

3-4. Summary

  • Ubuntu allows easy user switching via GUI
  • Using the lock screen keeps the current session active, while logging out fully closes it
  • The settings menu can be used to manage user switching, especially for administrators
  • Be mindful of memory consumption and save work before switching users

4. How to Switch Users via Command Line (CLI)

In Ubuntu, you can switch users easily using the Command Line Interface (CLI). This method is especially useful in server environments and remote access via SSH, where GUI is unavailable.

In this section, we will explain in detail how to switch users using the su command, sudo command, and switching users in an SSH environment.

4-1. Switching Users with the su Command

The su (Switch User) command allows switching from the current user to another user. It requires the target user’s password to authenticate.

Basic Usage of the su Command

To switch to another user:

su [username]

Example:

su john

After executing the command, enter the target user’s password to complete the switch.

Switching to the Root User

To switch to the root user:

su -

or

su root

Since the root user has full control over the system, use it with caution to avoid unintended system modifications.

Difference Between su and su – (With Hyphen)

The su command has two variations: su and su -. The difference is how environment variables are handled.

CommandFunction
su [username]Switches to the specified user while keeping the current environment variables
su - [username]Starts a completely new login session, resetting environment variables

The recommended option is su - as it properly applies the new user’s environment settings.

Precautions When Using su

  • To use su, you need to know the target user’s password
  • For administrative tasks, it is generally recommended to use sudo instead of su
  • Remaining as root for a long time can be risky (exit the root session with exit after finishing tasks)
exit

4-2. Switching Users with the sudo Command

The sudo (Superuser Do) command temporarily grants administrative privileges. Unlike su, you do not need to know the target user’s password as long as your user has sudo permissions.

Running Administrative Commands with sudo

To execute a command with administrative privileges:

sudo [command]

Example:

sudo apt update

Enter your current user’s password to execute the command with root privileges.

Switching to the Root User Using sudo

To temporarily become the root user:

sudo su

or

sudo -i

This will switch the session to the root user with full administrative rights.

Running a Command as Another User with sudo

To execute a command as a specific user:

sudo -u [username] [command]

Example:

sudo -u john whoami

This command runs whoami as the user “john” and returns the effective username.

Difference Between sudo and su

CommandFunctionPassword Required
su [username]Fully switch to another userTarget user’s password
sudo [command]Run a command with temporary administrative privilegesCurrent user’s password
sudo suSwitch to the root userCurrent user’s password

4-3. Switching Users in an SSH Environment

When connected to a remote server, you must use CLI commands to switch users, as GUI is unavailable.

Switching Users After SSH Login Using su

First, connect to the remote server:

ssh [username]@[server IP address]

After connecting, switch to another user:

su [username]

Or switch to the root user:

su -

Switching Users Using sudo in an SSH Session

If your user has administrative privileges, you can use sudo to execute commands as another user.

sudo -u [username] -s

This functions as an alternative to su.

Logging In Directly as a Specific User via SSH

By default, SSH connections use a user with sudo privileges, but you can log in directly as another user:

ssh [another user]@[server IP address]

Example:

ssh john@192.168.1.100

4-4. Summary

  • su fully switches to another user, but requires the target user’s password
  • sudo allows temporary execution of administrative commands without needing another user’s password
  • sudo su and sudo -i can be used to switch to the root user
  • In an SSH environment, su and sudo -u are useful for managing multiple users

5. Managing Users in Ubuntu (Adding, Deleting, and Modifying Users)

In Ubuntu, managing multiple users effectively is essential, especially when handling user addition, deletion, and modification. This is particularly important in server management and multi-user environments.

This section focuses on managing users using the Command Line Interface (CLI).

5-1. Adding a New User

In Ubuntu, users with administrative privileges (users in the sudo group) can create new user accounts.

Adding a User with the adduser Command

The most common method for adding a user is using the adduser command.

Executing the Command
sudo adduser [new_username]

Example:

sudo adduser john
Process Overview

When executing this command, you will be prompted to enter:

  1. A password for the new user
  2. Optional user information (Full Name, Phone Number, etc.)
  3. A home directory (e.g., /home/[username]) will be automatically created
  4. Basic configuration files will be set up

Once completed, the new user account is ready for use.

Adding a User with the useradd Command

The useradd command can also be used to create a user. However, unlike adduser, it does not automatically create a home directory or set a password.

Executing the Command
sudo useradd -m -s /bin/bash [new_username]

Example:

sudo useradd -m -s /bin/bash alex

Options:

  • -m: Creates a home directory
  • -s /bin/bash: Sets Bash as the default shell

To set a password for the new user:

sudo passwd alex

Granting sudo Privileges to a New User

To add a user to the sudo group and grant administrative privileges:

sudo usermod -aG sudo [username]

Example:

sudo usermod -aG sudo john

After executing this command, john will have administrator privileges.

5-2. Deleting a User

Removing unused user accounts improves security and optimizes system resources.

Deleting a User with the deluser Command

To delete a user, use the deluser command.

Executing the Command
sudo deluser [username]

Example:

sudo deluser john

This command removes the user john, but their home directory remains intact.

Deleting a User with the userdel Command

The userdel command can also remove users, but it provides lower-level control than deluser.

Deleting a User Along with Their Home Directory
sudo userdel -r [username]

Example:

sudo userdel -r alex

This command deletes alex and also removes their home directory (/home/alex/).

Important Considerations When Deleting a User

  • If you need to keep user data, back up the home directory
sudo tar -czf /backup/john_backup.tar.gz /home/john
  • You cannot delete a user while they are logged in
  • If john is currently logged in, running sudo deluser john will result in an error.
  • To forcibly terminate a user session, use:
sudo killall -u [username]

5-3. Changing a Username

To rename an existing user account, use the usermod command.

Changing a Username Using usermod

Executing the Command
sudo usermod -l [new_username] [current_username]

Example:

sudo usermod -l michael john

This renames the user john to michael.

Renaming the Home Directory

By default, renaming a user does not change their home directory name (/home/john remains unchanged).
To rename the home directory as well, follow these steps:

Renaming the Home Directory
sudo mv /home/john /home/michael
Updating the User’s Home Directory Path
sudo usermod -d /home/michael -m michael

Considerations When Renaming a User

  • You cannot rename a currently logged-in user
  • If john is logged in while renaming, the process will fail.
  • Use a different admin account or root to rename users.
  • Check if the renamed user retains sudo privileges
  • After renaming, verify if michael is in the sudo group using:
sudo groupmems -g sudo -l

5-4. Summary

  • adduser is the easiest way to create a new user
  • deluser removes users, while userdel -r also deletes their home directory
  • usermod -l allows renaming users, but the home directory must be renamed separately
  • It is recommended to back up data before deleting users

6. FAQ (Frequently Asked Questions)

Managing user accounts and switching users in Ubuntu can sometimes be confusing, especially for beginners. In this section, we address common questions and solutions related to user switching and management.

6-1. What Is the Difference Between su and sudo? Which One Should I Use?

Q: I don’t understand the difference between su and sudo. Which one should I use?

A: su completely switches to another user, while sudo temporarily grants administrative privileges.

CommandPurposeRequired Password
su [username]Fully switch to another userTarget user’s password
sudo [command]Run a command with temporary administrative privilegesCurrent user’s password
sudo suSwitch to the root userCurrent user’s password

💡 Best Practice: For security reasons, it is recommended to use sudo instead of su whenever possible.

6-2. How Can I Switch Users Without Entering a Password Every Time?

Q: I share my Ubuntu PC with family. Is there a way to switch users without entering a password every time?

A: Enabling automatic login allows you to log in without entering a password.

Steps to Enable Automatic Login

  1. Open the “Settings” application
  2. Go to the “Users” menu
  3. Enable “Automatic Login”

💡 Note: Automatic login poses a security risk, so it is recommended for shared home PCs rather than business or server environments.

6-3. How Can I Switch Users Without Using sudo in an SSH Environment?

Q: When I connect via SSH, is there a way to switch users without using sudo?

A: You can use the runuser command as an alternative to su.

runuser -l [username] -c "command"

Example:

runuser -l john -c "whoami"

💡 Tip: The runuser command is particularly useful when switching users inside scripts.

6-4. How Can I Fix the “Authentication Failure” Error When Using su?

Q: When I run the su command, I get an “Authentication failure” message. How can I fix this?

A: The su command requires the target user’s password.

Check the following:

  • Ensure the password is correct (check if Caps Lock is on)
  • Verify if the user account is locked
sudo passwd -S [username]

→ If it shows L (locked), unlock the account with:

sudo passwd -u [username]
  • Check if /etc/pam.d/su restricts su
sudo nano /etc/pam.d/su

→ If the line auth required pam_wheel.so use_uid is uncommented, only users in the wheel group can use su.

6-5. Can I Recover a Deleted User’s Data?

Q: I accidentally deleted a user. Is there any way to recover their data?

A: If the home directory was not deleted, you can restore the user data.

Checking if the Home Directory Still Exists

ls /home/

Recovering a Deleted Home Directory

If /home/[username] was deleted, full recovery may be difficult without a backup. However, you can try the following:

  1. Install the extundelete tool
sudo apt install extundelete
  1. Scan for deleted files
sudo extundelete /dev/sdX --restore-all

Replace /dev/sdX with the correct partition (e.g., /dev/sda1).

💡 Note: Data recovery is not always successful. Regular backups are strongly recommended.

6-6. Summary

  • Understand the difference between su and sudo and use them appropriately.
  • Automatic login can simplify user switching but may pose security risks.
  • In SSH environments, runuser and sudo -u are useful alternatives for switching users.
  • If su: Authentication failure occurs, check the password and account lock status.
  • Recovering a deleted user’s data is difficult, so regular backups are essential.

7. Summary

In this article, we have explained how to switch and manage users in Ubuntu using both GUI and CLI methods. Since Ubuntu supports multi-user environments, proper user management helps ensure a secure and efficient system.

Below, we summarize the key points covered in each section and provide best practices for user management.

7-1. Basics of User Switching in Ubuntu

  • Ubuntu is a multi-user system, where each user has an independent environment.
  • It is essential to understand the differences between standard users, administrator (sudo) users, and the root user.
  • User switching can be done using GUI or CLI (command-line interface).

7-2. Switching Users via GUI

  • Using the lock screen allows switching while keeping the current session active.
  • Logging out before switching fully closes the previous user’s session, freeing system resources.
  • The “User Accounts” settings menu allows administrators to manage users easily.
  • Key considerations:
  • Keeping multiple users logged in increases memory usage and can slow down the system.
  • Ensure that all unsaved work is saved before switching users.

7-3. Switching Users via CLI (Command Line Interface)

  • The su [username] command allows switching users after entering their password.
  • The su - command (with a hyphen) starts a clean session with the new user’s environment settings.
  • The sudo [command] command lets a user execute administrative commands temporarily.
  • In an SSH environment, the runuser -l [username] -c "[command]" command can switch users without using sudo.

7-4. Managing Users (Adding, Deleting, and Modifying)

  • Adding new users:
  • Use sudo adduser [username] to create a new user with a home directory.
  • Use sudo usermod -aG sudo [username] to grant sudo privileges.
  • Deleting users:
  • sudo deluser [username] removes a user while keeping their home directory.
  • sudo userdel -r [username] removes both the user and their home directory.
  • Renaming users:
  • Use sudo usermod -l [new_username] [old_username] to rename a user.
  • Manually rename the home directory if needed.

Best Practices:
Back up user data before deleting accounts (especially important files).
Properly manage users with administrative privileges (limit sudo access to necessary users).

7-5. FAQ – Common Issues and Solutions

  • Understand the difference between su and sudo, and use sudo for security purposes whenever possible.
  • Automatic login can simplify user switching but comes with security risks.
  • Use runuser or sudo -u to switch users efficiently in an SSH environment.
  • If you encounter su: Authentication failure, check the password and account lock status.
  • Recovering deleted user data is difficult, so regular backups are highly recommended.

7-6. Best Practices for User Management in Ubuntu

🔹 Properly Managing User Permissions

  • Clearly distinguish between standard users and administrator (sudo) users.
  • When using sudo, consider using visudo to restrict users to specific commands.

🔹 Security Considerations

  • Avoid logging in as the root user directly; use sudo when needed.
  • Disable automatic login in business or server environments for better security.
  • Regularly review and remove unnecessary users to maintain system security.

🔹 Data Backup

  • Before deleting a user, back up their home directory using:
sudo tar -czf /backup/username_backup.tar.gz /home/username
  • Use rsync or cron jobs for regular backups.

7-7. Final Summary

  • Understand both GUI and CLI methods for switching users and choose the appropriate method based on your needs.
  • Use su and sudo correctly to manage administrative privileges efficiently.
  • Delete unused users to enhance security, but always back up important data before doing so.
  • Implement a solid backup strategy to prevent data loss in case of accidental deletions.

Ubuntu provides powerful user management features, and using them correctly will help ensure a secure and efficient system. We hope this guide has been helpful for understanding and implementing user switching and management in Ubuntu.

侍エンジニア塾