UbuntuUbuntu Based

How to Switch Users on Ubuntu

Switch Users on Ubuntu

In the multi-user environment of Ubuntu, knowing how to efficiently switch between user accounts is a fundamental skill. Whether you’re sharing a computer with family members, managing different administrative tasks, or testing applications with varying permission levels, Ubuntu provides several methods to seamlessly transition between user profiles without completely shutting down your system. This comprehensive guide explores all aspects of user switching in Ubuntu, from basic graphical methods to advanced terminal techniques.

Understanding Multi-User Functionality in Ubuntu

Ubuntu, like other Linux distributions, is built on a robust multi-user architecture that allows multiple users to share a single physical machine while maintaining separate environments. Each user account has its own home directory, permissions, settings, and installed applications.

The multi-user system in Ubuntu operates with a hierarchy of permissions. Regular users have limited access to system files and configurations, while administrative users (those with sudo privileges) can perform system-wide changes. This separation creates a secure environment where users can work independently without interfering with each other’s files or system settings.

When multiple users are set up on an Ubuntu system, each can customize their environment according to their preferences. This separation is particularly valuable in shared computing environments such as:

  • Family computers where parents and children need different access levels
  • Office workstations shared by multiple employees
  • Educational settings where multiple students may use the same computer
  • Development environments where testing different user permissions is necessary

Understanding this foundation helps appreciate why user switching, rather than completely logging out, can be advantageous for workflow efficiency.

Graphical Method: Switching Users via GUI

The most straightforward way to switch users in Ubuntu is through the graphical user interface. This method is intuitive and requires no command line knowledge, making it accessible for all users regardless of technical expertise.

Using the System Menu

To switch users via the GUI in Ubuntu:

  1. Click on the system menu located in the top-right corner of your screen
  2. Select the Power Off/Log Out option to open the dropdown menu
  3. Choose between “Switch User” or “Log Out”

The Switch User option allows you to leave your current session active with all applications running in the background. This is ideal when you need to temporarily use another account but plan to return to your work later. In contrast, the Log Out option closes all your applications and ends your session completely, which is preferable when you’ll be using another account for an extended period.

After selecting either option, you’ll be presented with the login screen showing all available user accounts on the system. Simply click on the desired user account and enter the password to access that user’s environment.

Desktop Environment Variations

The exact appearance and location of the user switching option can vary depending on your Ubuntu desktop environment:

GNOME (Default Ubuntu Desktop)
The steps outlined above apply to the standard GNOME environment used in recent Ubuntu versions. Look for the power/user icon in the top-right corner.

XFCE
In XFCE, click on the “Whisker Menu” (the main application menu), then select “Log Out” to access user switching options.

KDE Plasma
KDE users can click on the application launcher, then the “Leave” option, which provides choices including “Switch User.”

Despite these variations, the concept remains consistent across desktop environments – access the main system menu, look for power or session options, and select the user switching function.

Fast User Switching Feature

Ubuntu’s fast user switching feature allows multiple users to be logged in simultaneously, with their sessions running concurrently. This means one user can be working in the background while another uses the computer. When you switch back, all your applications and work remain exactly as you left them.

This feature is particularly useful in shared environments where multiple users need frequent access to the computer. It eliminates the need to close applications and restart them when switching between users, significantly improving productivity.

Command Line Method: Switching Users via Terminal

For power users and system administrators, switching users via the terminal offers flexibility and efficiency, especially when working remotely or troubleshooting system issues.

Using the ‘su’ Command

The primary command for switching users in the terminal is su, which stands for “substitute user.” The basic syntax is:

su username

Replace “username” with the name of the user you want to switch to. When you execute this command, you’ll be prompted to enter the password for that user account. After successful authentication, your terminal session will change to operate as the specified user.

However, there’s an important distinction between using su username and su - username:

su - username

The hyphen (or dash) is significant. When you include it, you start what’s called a “login shell,” which:

  1. Initializes all environment variables as if the user had logged in directly
  2. Changes to that user’s home directory
  3. Loads their shell configuration files (.bashrc, .profile, etc.)

Without the hyphen, you maintain your current environment variables and remain in the same directory, simply gaining the permissions of the other user.

For a more complete switch that mimics a fresh login, using su - username is recommended, though some prefer to use the full --login option for clarity:

su --login username

Switching to Root User

The root user in Ubuntu has complete administrative access to the system. By default, the root account is disabled for direct login in Ubuntu for security reasons. Instead, administrative tasks are performed using the sudo command.

However, if you need to switch to the root user in the terminal, you can use:

sudo su

This combines the sudo command (which grants temporary administrative privileges) with the su command. You’ll be prompted for your own password (not the root password), assuming your user has sudo privileges.

For a full root environment with proper environment variables, use:

sudo su -

Remember that using the root account for everyday tasks is discouraged as it bypasses the security safeguards built into Ubuntu. Only use root access when absolutely necessary for system administration.

Returning to Original User

To exit a user session in the terminal and return to your original user, simply type:

exit

Alternatively, you can use the keyboard shortcut Ctrl+D. This will terminate the current user shell and return you to the previous user context.

If you’ve switched through multiple users (for example, from user A to user B to user C), you’ll need to use exit multiple times to return to your original session.

Advanced Terminal Techniques

Beyond basic user switching, there are several advanced techniques that provide greater control and flexibility for managing user sessions.

Login Shell vs. Non-Login Shell

Understanding the difference between login and non-login shells is important for effective user switching:

Login Shell (using su - username):

  • Simulates a complete login process
  • Changes to the user’s home directory
  • Loads the user’s environment variables
  • Executes login initialization scripts (.profile, .login)
  • Provides a clean environment for the new user

Non-Login Shell (using su username):

  • Maintains the current directory
  • Inherits many environment variables from the original user
  • Only loads the user’s .bashrc file
  • Useful for quick commands that need different permissions

Choosing between these options depends on your specific needs. For completely taking on another user’s identity and environment, use a login shell. For quick operations that just need different permissions, a non-login shell may be more efficient.

Using sudo Command for Temporary Privileges

In many cases, you don’t need to fully switch users – you just need elevated privileges for a specific command. This is where sudo comes in:

sudo command_to_run

The sudo command allows authorized users to execute specific commands with the security privileges of another user (by default, the root user). After entering your password, the command runs with elevated privileges, but you remain as your original user.

For executing a command as a specific user other than root:

sudo -u username command_to_run

This approach is more secure than constantly switching to the root user, as it limits elevated privileges to only the specific commands that need them.

SSH Method for Remote User Switching

When managing Ubuntu systems remotely, SSH (Secure Shell) provides methods for user switching:

ssh username@hostname

To directly SSH into a different user account on a remote system. Or, if already connected:

sudo su - another_username

To switch users within an existing SSH session.

Remote user switching requires careful attention to security. Ensure proper SSH key management and consider using key-based authentication rather than passwords for enhanced security.

Keyboard Shortcuts for Fast User Switching

Keyboard shortcuts offer the fastest way to switch between users, particularly useful for power users who prefer to keep their hands on the keyboard.

Creating Custom Keyboard Shortcuts

Ubuntu allows you to create custom keyboard shortcuts for user switching:

  1. Go to Settings > Keyboard > Shortcuts
  2. Click the “+” button to add a new shortcut
  3. Name it “Switch User”
  4. Enter the command gdmflexiserver for GNOME or dm-tool switch-to-greeter for LightDM
  5. Assign your preferred key combination (e.g., Ctrl+Alt+S)

This creates a quick keyboard shortcut that immediately brings up the user switching interface.

Using Function Keys (TTY Switching)

Ubuntu, like most Linux distributions, provides multiple virtual consoles or TTYs that can be accessed using function keys:

  • Press Ctrl+Alt+F1 through Ctrl+Alt+F6 to access text-based consoles
  • Ctrl+Alt+F7 (or sometimes F1 or F2) typically returns to the graphical interface

Each TTY operates independently, allowing you to log in as different users simultaneously. This is particularly useful when troubleshooting graphical interface issues or when you need to work with multiple user sessions concurrently.

To use TTY for user switching:

  1. Press Ctrl+Alt+F3 (or any available function key from F1-F6)
  2. You’ll see a text-based login prompt
  3. Enter the username and password for the user you want to log in as
  4. Work in this text-based session as needed
  5. When finished, press Ctrl+Alt+F7 (or whichever function key leads back to your graphical environment)

This method is especially valuable when the graphical interface is unresponsive or when you need to perform operations that might affect the display server.

Managing User Sessions

Understanding how Ubuntu manages sessions is crucial for effective user switching and system management.

Understanding Session Persistence

When you switch users rather than logging out completely, your session persists in the background. This means:

  • All your applications continue to run
  • System resources (RAM, CPU) are still being consumed by your session
  • Any processes or operations you’ve started will continue to execute

This persistence is beneficial when you need to run long processes (like downloads or computations) while someone else uses the computer. However, it also means that system resources are divided among all active sessions, which can affect performance on systems with limited resources.

Lock Screen vs. User Switching

Ubuntu offers two similar but distinct options for temporarily leaving your session:

Lock Screen:

  • Secures your session without allowing others to use the computer
  • Requires only your password to resume your session
  • Applications continue running in the background
  • No new user can access the system until you unlock

User Switching:

  • Secures your session but allows others to log in to their accounts
  • Your applications continue running in the background
  • Multiple users can be actively logged in simultaneously
  • Requires more system resources than simply locking the screen

Choose the lock screen option when you want to secure your session but prevent others from using the computer. Choose user switching when you want to allow others to use the computer with their own accounts while maintaining your session.

Common Issues and Troubleshooting

Even with Ubuntu’s robust user management, issues can occasionally arise during user switching. Here are solutions to common problems:

Authentication Problems

Forgotten Password:
If you’ve forgotten a user password, you can reset it from an administrative account:

sudo passwd username

Enter the new password when prompted. This requires sudo privileges from another admin account.

Permission Denied Errors:
If you encounter “Permission denied” errors when switching users, verify that:

  1. You’re entering the correct password
  2. The account isn’t locked or disabled
  3. The user has login permissions

To check if an account is locked:

sudo passwd -S username

Look for “L” in the output, which indicates a locked account. Unlock it with:

sudo passwd -u username

Session and Display Issues

Black Screen After Switching:
If you encounter a black screen when switching users:

  1. Try pressing Alt+F2 and typing r then Enter to restart the GNOME shell
  2. Switch to a TTY with Ctrl+Alt+F3, log in, and restart the display manager:
    sudo systemctl restart gdm

    (or lightdm instead of gdm depending on your display manager)

Login Loops:
If you’re stuck in a login loop (enter password, screen goes black, returns to login):

  1. Switch to a TTY console with Ctrl+Alt+F3
  2. Log in with your username and password
  3. Check ownership of your home directory:
    ls -la ~/ | grep .Xauthority
  4. If .Xauthority is owned by root instead of your user, fix it:
    sudo chown username:username ~/.Xauthority
  5. Return to the graphical interface with Ctrl+Alt+F7 and try logging in again

Application Persistence Problems

Applications Not Responding After Switching:
Some applications may behave unexpectedly after user switching, particularly those with complex graphics or hardware access. If an application is unresponsive:

  1. Save your work in other applications
  2. Try closing and reopening the problematic application
  3. If necessary, log out completely and log back in

Saving Work Before Switching:
It’s always good practice to save your work before switching users, even though your session persists. Some applications might not handle the display server switching gracefully, potentially resulting in data loss.

Use Cases and Scenarios

User switching in Ubuntu serves various practical purposes across different environments:

Home Environment

In a family setting, user switching allows:

  • Parents to maintain administrative accounts while children use restricted accounts
  • Different family members to have personalized desktop environments
  • Quick transitions between users without closing applications
  • Privacy and separation of personal files and settings

For example, parents might switch to a child’s account to help them with homework, then switch back to their account to continue working, all without disrupting running applications in either session.

Professional Environment

In workplace settings, user switching facilitates:

  • System administrators to temporarily access user accounts for troubleshooting
  • Shared workstations with distinct environments for different employees
  • Testing software behavior across different user permission levels
  • Quick transitions between standard and administrative accounts

A system administrator might switch to a root account to perform system updates, then return to their regular user account for daily tasks, enhancing security by minimizing the time spent with elevated privileges.

Educational Setting

In educational environments, user switching enables:

  • Teachers to access administrative functions on student computers
  • Multiple students to share laboratory computers with personalized settings
  • Demonstration of different user environments for teaching purposes
  • Quick transitions between instructor and student accounts during training

For example, in a computer lab, a teacher might quickly switch to their account on a student’s computer to demonstrate a concept, then switch back to allow the student to continue working.

Creating and Managing User Accounts

Effective user switching depends on properly configured user accounts. Here’s how to manage users in Ubuntu:

Adding New Users

To add a new user via the terminal:

sudo adduser username

Follow the prompts to set the password and optional user information.

For a more minimal user creation:

sudo useradd -m username
sudo passwd username

The -m flag creates a home directory for the user. You’ll be prompted to set and confirm a password.

To add a user to the sudo group (administrative privileges):

sudo usermod -aG sudo username

Modifying Existing User Accounts

To change a user’s password:

sudo passwd username

To modify user groups:

sudo usermod -aG groupname username

To remove a user from a group:

sudo deluser username groupname

To completely delete a user account:

sudo deluser --remove-home username

The --remove-home flag deletes the user’s home directory and mail spool.

Best Practices for Secure User Switching

Maintain security while leveraging Ubuntu’s multi-user capabilities with these best practices:

Password Management

  • Implement strong, unique passwords for each user account
  • Consider password complexity requirements (mix of upper/lowercase, numbers, symbols)
  • Change passwords regularly, especially for administrative accounts
  • Avoid sharing passwords between users, even temporarily

Enable automatic screen locking for additional security:

  1. Go to Settings > Privacy > Screen Lock
  2. Set “Automatic Screen Lock” to “On”
  3. Choose an appropriate delay time (shorter for higher security)

Regular Auditing

Maintain system security by regularly reviewing user activities:

To check recent login attempts:

last

To view currently logged-in users:

who

To check failed login attempts:

sudo lastb

Review authentication logs for suspicious activities:

sudo cat /var/log/auth.log | grep "Failed password"

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button