4 Simple Ways to Linux Switch Users

Written by

Reviewed by

Last updated: July 12, 2024

Expert verified

SVG Image

TL;DR

To Linux switch users, follow these steps:

  1. Open your Terminal by pressing Ctrl+Alt+T.
  2. Type su - username (replace username with the user you want to switch to) and press Enter.
  3. Enter the password for the user account when prompted and press Enter.
  4. You are now logged in as that user and can execute commands and perform tasks.

Switching users in Linux is something you’ll need to do often, whether you’re running commands with higher privileges, accessing another user’s files, or testing settings. There’s a solution for every situation, and this post will show you how. I’ll guide you through using commands like su and sudo, as well as graphical interfaces, to Linux change user efficiently. You’ll also get tips on best practices for managing users securely. By the end, you’ll have a solid understanding of how to handle user switching in Linux confidently.

When You Need to Switch the User?

Switching users in Linux is a common task for several important reasons. Here’s when you might need to change user Linux:

  • Accessing Another User’s Files: If you need to access or manage files that belong to another user, you’ll need to switch to that user. This is useful for system administrators who need to troubleshoot issues or manage user accounts. By switching users, you can access the files and settings specific to that user.
  • Testing User-Specific Settings: When configuring a system, you might need to test settings or applications from the perspective of different users. Switching users allows you to see exactly how changes affect each user, ensuring that everything works correctly for everyone.
  • Running Commands with Different Permissions: Sometimes, you need to run a command that requires higher privileges than your current user account has. For example, installing software or changing system settings usually requires root access. In this case, you switch to the root user or you can also grant root permissions to users in Linux. For giving root permissions to users in Linux, explore this step-by-step guide.

    Types of Users in Linux

    Understanding the different types of users in Linux is essential for effective system management and security. Each user type has specific roles and permissions, designed to help maintain order and control over the system. In Linux, users can be broadly categorized into three main types: root users, regular users, and system users.

    1. Root User: The root user is the most powerful user on a Linux system, with complete control to run any command, access any file, and change any setting. Use the root account only when necessary to avoid accidental changes that could harm the system.
    2. Regular Users: Regular users have their own home directories and personal files. They can perform normal tasks like editing files and running programs but cannot make system-wide changes without permission. Create separate regular user accounts for different people to keep files and settings secure.
    3. System Users: System users are special accounts created by the system to run services and manage tasks. They have limited permissions and usually do not have a home directory. Examples include users for web servers and databases. Avoid using system user accounts for regular tasks to keep services running securely.

    How to Switch Users on Linux?

    To switch users on Linux, you can use the su (switch user) command followed by the username of the account you want to switch to. For instance, entering su username in the terminal prompts you to enter the password for that user account. Once authenticated, you are logged in as that user. To switch to the root account, simply type su and enter the root password. If you prefer a login shell that resets the environment variables, use su - username.

    That was the quick answer. Below, we’ll dive into more details on this method along with 3 more methods.

    1. su Command

    The su (switch user) command in Linux is a powerful tool that allows users to switch from their current user account to another user account directly from the terminal. This command is particularly useful for system administrators and users who need to perform tasks with different user permissions without logging out and back in. Follow these steps to change user in Linux:

    1. Open your Terminal window by pressing Ctrl+Alt+T.
    open terminal
    1. Run the following command to switch users:
    su - username

    Replace the username with the name of the user you wish to switch to. Press enter.

    switching to different user using su command
    1. You will be prompted for the password of the user account you are switching to. Enter the password and press enter. You can now execute commands and perform tasks as this user.
    entering password to switch to user

    2. sudo Command

    The sudo command stands for superuser do and allows a permitted user to execute a command as the superuser or another user, as specified in the sudoers file. This tool is essential for managing permissions and performing administrative tasks without switching from the current user account. Here is how to switch user in Linux using sudo command:

    1. Access the Terminal through your Linux environment.
    2. Execute the following command: 
    sudo -u username

    Replace username with the user you want to execute the command as, and command with the command you wish to run. If you omit username, the command runs as the superuser by default.

    1. Enter your password if prompted.
    executing command as another user

    Configuring sudo Permissions

    1. Type the following command in the Terminal:
    sudo visudo

    This command opens the sudoers file in your default editor, which is configured for safe editing of this file.

    opening sudoers file
    1. Follow the syntax username ALL=(ALL:ALL) ALL to grant a user full sudo privileges or customize the command as needed for specific permissions.
    editing and saving sudoers file
    1. Make sure to save your changes. The editor will check for syntax errors to ensure your sudoers configuration is correct.

    3. Login Command

    The login command offers a straightforward method for switching between user accounts by initiating a new session right from the terminal. This approach is especially useful for users who require a complete login process, including the loading of all environment variables and startup scripts associated with the new user. Follow these steps to switch user:

    1. Begin by opening your Terminal. 
    2. Run the following command:
    sudo login username

    Press Enter after typing the command.

    switching to user using login command
    1. After executing the command, you will be prompted to enter the password for that user account. Carefully type the password and press Enter.
    enter password of the user
    1. Once you’ve successfully authenticated with the correct password, you’ll be logged into a new session as the specified user. This new session will have all the environment settings, variables, and startup scripts associated with that user, allowing you to work as if you had logged in from the beginning.
    switched to desired user
    1. To end the session, you can simply type exit or press Ctrl + D. It will log you out of the current user session and return you to the previous state.
    exiting from the user

    4. Graphical User Interface (GUI) Method

    Linux distributions with a graphical user interface (GUI) offer a user-friendly way to switch between user accounts without using the command line. This method is ideal for users who prefer a visual approach to system administration. Here is how to switch user Linux using GUI:

    1. Click on the system menu on your desktop environment located in the top right corner.
    opening system menu on ubuntu
    1. Look for an option labeled Switch User, Log Out, or something similar. Selecting this will Linux logout other user.
    logging out from current user
    1. Selecting this will bring you to the login screen.
    go to login menu
    1. On the login screen, select the user you wish to switch to and enter their password.
    Selecting user to switch
    1. Enter the user password.
    switching to user through GUI

    4 Best Practices for Linux User Management 

    Efficient user management is pivotal for safeguarding Linux systems against unauthorized access and ensuring operational integrity. The following best practices are essential for administrators aiming to enhance security and manage user privileges effectively.

    1. 🔒 Conduct Periodic User Account Audits: Regularly review all user accounts on your system to verify their necessity. Immediate removal or deactivation of unused or outdated accounts is crucial for mitigating security risks and preventing unauthorized access to sensitive information. Read this guide to learn how to check login history in Linux to monitor system activity.
    2. 🔑 Implement Robust Password Policies: Establish and enforce strong password policies that require complex, hard-to-guess passwords, which must be changed at regular intervals. This approach significantly reduces the likelihood of security breaches through compromised user credentials.
    3. 👤 Restrict Sudo Privileges to Necessary Users: Grant sudo privileges solely to individuals who require administrative access for their roles. Minimizing the number of users with elevated privileges helps in preventing accidental or deliberate misuse of administrative powers.
    4. 🛡 Utilize User Groups for Efficient Permission Management: Instead of managing file permissions on a per-user basis, leverage user groups to streamline permission assignment. This method facilitates the efficient allocation of access rights, adhering to the principle of least privilege and simplifying administrative tasks.

    Linux Switch User: In a Nutshell

    Switching users in Linux can be done using commands like su and sudo, or through graphical interfaces. Choose the right method for your needs: use su for full user switches, sudo for specific commands with elevated privileges, and GUI methods for a visual approach.

    If you found this article helpful, I recommend checking out:

    Frequently Asked Questions

    What Is a su Command?

    The su command, or “substitute user” command, is a Unix command used to switch the current user account to another user account. By executing su followed by a username, the operating system allows you to access the computer as if you were logged in as that user. This is particularly useful for administrators needing to switch contexts to another user without logging out and back in. Additionally, su can be used without specifying a username to switch to the root account, provided the correct root password is entered.

    What Is a sudo Command?

    The sudo command, short for “superuser do,” is a program for Unix-like operating systems that allows a permitted user to execute a command as the superuser or another user, as specified by the security policy. Typically configured through the /etc/sudoers file, sudo provides a vital audit trail and logging of each command, offering security and administrative benefits over the less secure su command.

    What Is a login Command?

    The login command is a Unix and Linux system utility used to initiate a new session on the system. When a user types the login command, it prompts them to enter their username and password, and if authenticated successfully, it grants access to a user-specific environment. This command is essential for managing user access and ensuring that resources and data are kept secure on multi-user systems.

    How can I prevent a user from using the su command to switch to specific users?

    To prevent a user from using the su command to switch to specific users, configure the /etc/pam.d/su file. Restricting access through PAM (Pluggable Authentication Modules) settings allows you to control who can use su for switching users.

    How do I switch back to the previous user without entering a password again?

    To switch back to the previous user without re-entering a password, you can use sudo with configured nopasswd privileges for specific commands or users. This allows seamless switching without password prompts, but ensure it’s securely set up in the sudoers file.

    What’s the difference between adding a user to the sudoers file and adding them to the sudo group?

    Adding a user to the sudoers file allows for precise control over their sudo privileges, specifying what commands they can run. In contrast, adding them to the sudo group grants them general sudo access based on the group’s default permissions in the sudoers file, offering less granularity.

    Is it possible to restrict user login times or the number of active sessions?

    Yes, it’s possible to restrict user login times and active sessions using PAM configurations. By editing the /etc/security/time.conf and /etc/security/limits.conf files, you can define time-based access and limit the number of concurrent sessions for users, enhancing security and managing system resources.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    How to Create a Systemd Service in Linux? [11 Proven Steps]

    Next Post

    How to Find Bash String Length [5 Best Methods]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Read next