How to Switch User to Another User in Ubuntu

How to Switch User to Another User in Ubuntu

Linux is a powerful and versatile operating system that allows multiple users to use a single system by creating separate personal user accounts. In addition, in Linux distributions, users can switch between different user accounts without setting and exiting the system while maintaining security and privacy. Therefore, if needed, you can switch to different user accounts for different purposes and execute commands with the permission of the Root user or the permissions of other user accounts.

To switch to different user accounts, you need to know the password and username of the other user account. As you know, the Root user has full permission to manage the Linux server, so the Root user can easily switch to user accounts without entering a password.

In this article, you will learn how to switch a user to another user in Ubuntu through the terminal and GUI. If you still have doubts about buying a Linux VPS, we must say that Linux VPS is unmatched in terms of performance and security, fully responds to the needs of its users, and optimizes server management by providing facilities and support for advanced tools. It is not without reason that Linux VPS is one of the most used VPS among users because most users have concluded that Linux is a versatile operating system that offers valuable features.

What user switching is?

In Linux, each user account is a distinct identity associated with a person or process interacting with the operating system. User accounts in Linux include specific resources, settings, permissions, and privileges to manage and control programs, files, resources, and system services. The meaning of user switching is the process of transferring from one user account to another without the need to log out of the Linux system completely. This feature of Linux is an advantage for multi-user environments where users benefit from a single system.

By switching users to another user, Linux users can enter another user account without logging out and will benefit from the permissions of the desired user account to improve management and perform tasks in specific situations. Also, when the user switches, the current session’s state (open applications, documents, etc.) is preserved; that is, the user’s active session is stopped, and after returning to his work session, he can resume his tasks from where he stopped.

Switching between different Linux accounts does not lead to security issues because you cannot change to another user account without knowing the password of another user account as a regular user, so the security and privacy of each user are maintained, and only the root user can log into the user accounts of normal users without entering the password. Also, using this feature, a regular user can temporarily log into the root or superuser account and perform essential tasks.

Overall, user switching increases the usability and versatility of a system by accommodating multiple users while maintaining security and convenience.

What is the importance of user switching for multi-user environments?

  • Implementation of simultaneous sessions in a single system by several users for joint tasks and training sessions, etc
  • Privacy protection and separation of data belonging to each user
  • Ensuring security and preventing unauthorized access to private data and active sessions of other users without proper authorization
  • User switch in multi-user environments reduces the need to share superuser accounts and increases security.
  • Fast user switching between accounts in emergencies without logging out
  • Facilitating and improving the centralized management of user accounts, permissions, and settings in business and educational environments
  • Providing seamless collaboration conditions

Switch User in Ubuntu via Terminal

For user management in Linux, sudo (superuser do), Su (switch user), and ssh commands are used. You can switch to another user using the su command while preserving the current session. The su command supports various options to expand its functionality and behavior like other Linux commands, which we will discuss in the following about how to use the su command for different purposes.

As we mentioned at the beginning of the article, to change the user in Linux, you must know the exact username and password of the other user’s account for security reasons. So, the first step to switch to another user in ubuntu is to list Linux Users. You can use different commands to list Ubuntu users, the compgen command is one of the useful commands for this purpose. So enter the Ubuntu terminal by pressing Ctrl + Alt + T and run the following command to access a list of usernames of Ubuntu users:

compgen -u

In the output of the previous command, consider the exact name of the user you want; in addition, you can get a list of Linux users by checking the contents of the /etc/passwd file. Finally, you can switch between different user accounts using user management commands.

Switching to the desired user from the root and normal user

If, as a regular user, you want to change to another normal user, you must know the password of another user account and then switch to another user using the su command:

su other_username

After running the previous command, you’ll be prompted to enter the password for the user you’re switching to (other_username), so you must enter the password and press enter. You will be switched to the specified user’s shell if the password is correct.

Note: Nothing is displayed on the screen for security reasons when typing the password.

The Root user or a user with sudo privilege does not need to enter a password to switch to a regular user after running the su command.

If you have sudo privileges and you don’t know the other user’s password, you can easily switch to another normal user’s account by running the following command without having to enter the other user’s password:

sudo su other_username

Since you are a sudo user, executing the previous command requires entering your account password.

To switch back to your logged-in user, type the following command:

exit

Switch to the root user from the normal user in ubuntu

The su command allows you to switch to the Root user account, although the su command is one of the ways to switch from a regular user to a Root user. To change the user to the Root user, you need to know the password of the Root user, then run the following command from a normal user account in Ubuntu, and then press enter:

su

Then, you need to enter the root user’s password and, if successful, switch to the root user’s shell.

If you do not know the password of the Root user but you have sudo privileges, run the following command to switch to the root user account:

sudo su

Then, instead of entering the root user password, enter your account password with sudo access and switch to the root user account.

To exit the root user’s shell and return to your original user account, simply type:

exit

Note that you can fix sudo command not found error by switching to root user and installing sudo.

Switching to Another User’s Shell

The su command allows changing to another user’s shell by supporting , -l, or -login options. By running the su command with the -l option, you can start a shell as the login shell of the other user, which means you can initialize the environment variables. The command that you can use to switch to another user’s shell in ubuntu is as follows:

su -l other_username

In addition to the su command, as a user with sudo access, you can also use the sudo command with the -i or -s options to open a shell as another user :

sudo -i -u other_username

Or

sudo -s -u other_username

If you use the sudo command to switch to another user’s shell, you’ll be prompted to enter your own password, and after entering your account password, you’ll be switched to the specified user’s shell.

To enter the root shell session from the sudo user, run the following command:

sudo -i

You’ll need to enter your own password (since you’re using sudo).

Switching to Another User’s Shell while preserving the environment

For some users, it is important to preserve environment variables such as PATH, home directory, log name, etc., when switching to another user’s shell; for this reason, they can run the su command along with the -p or -preserve-environment option. To log in to another user’s shell while preserving the environment variables, run the following command:

su -p other_username

For example:

sudo cat /etc/passwd | grep Opera
Opera: x:1001:1001: Opera,,,:/home/Opera:/ bin/bash
ubuntu at ubuntu2104 in ~
su -p Opera
password:
ubuntu2104% pwd
/home/ubuntu
ubuntu2104%

If you pay attention to our example, by executing the su -p command, we are still in the /home/ubuntu directory, the zsh shell is used, and no variables have been changed.

Running command as another user

If the purpose of switching to another user account is to execute commands with the permissions and privileges of the desired user, you can execute commands as another user without needing to switch to their user account. To run commands as another user, you can run the su command with the -c option:

su -c command_to_run user_name

Replace command_to_run with the actual command you want to execute with another user’s privileges.

Note: You can execute the command with a single argument through the previous command, and in this situation, you must type the command inside quotes.

For example:

su -c "touch a.txt" Opera

If you are a user with sudo access, run the following command to run the command as root user:

sudo command_to_run

User Switching in Ubuntu via GUI

Ubuntu, like other Linux distributions, provides a user-friendly graphical user interface in addition to the terminal. The popular and user-friendly graphical user interface of Ubuntu (Gnome) is an interface that has attracted novice users to make changes and settings in Ubuntu. Until this part of the article, you learned how to change a user to another user through the terminal; in this part of the article, we will teach how to switch user to another user through GUI in Ubuntu. So follow the steps below:

1. In the Ubuntu desktop environment, navigate to the top bar and click on the Power icon in the right corner of the bar.

2. Select one of the logout or switch user options from the drop-down menu according to your needs. (Selecting the Logout option ends your current session, but the switch user option is used to keep the current session and running programs.)

3. By selecting any of the options, you will be directed to the login window, and you must enter the desired user name and user password to switch to the selected user account.

how to switch user to another user

As a result, the screen will change, and you will enter your desired user account.

Note: Depending on the version of the operating system you are using, the switch option may not be displayed in the drop-down menu; in this case, by pressing the CTRL + ALT + F2 buttons from the keyboard, you can enter the shell prompt to enter the username and password of the desired user.

FAQ

Make sure the password is correct to change to the desired user, and if the problem is not solved, reset the password or troubleshoot authentication issues.

System settings and policies may be causing this problem; check system settings regarding user switching.

  • Password protection and preventing unauthorized access by forcing the user to enter the desired password when switching
  • Prevent new user access to Root user's files
  • Session locking to require password authentication before accessing another user's session
  • Switching the user safely and preventing any manipulation or bypassing of security mechanisms
  • Implementation of access control based on user roles and permissions

Conclusion

Linux’s support for user switching in multi-user environments is one of its commendable advantages, which creates a balance between data protection and collaborative work. Switching different user accounts provides a seamless collaboration experience by separating users, preserving privacy, and preventing data interference; As a result, it improves the convenience and simplification of collaboration.

Through this guide, you gained a new experience in managing users and became an expert in switching users quickly in ubuntu through the terminal and GUI. As a result, now, as a system administrator, you can use the full potential of a multi-user system while maintaining strong security measures.

If you have any questions about how to switch a user to another user, ask our technical support team in the comments section.

Leave a Reply

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