Navigating the command line in Linux can feel like learning a new dance, especially when it comes to switching user accounts. To switch users in Linux, you simply need to use the su - <username> command. This command strips the complexities away and makes jumping between accounts straightforward and efficient.

Imagine you’re working on a system where you need administrative privileges: entering sudo -i if you’re on Ubuntu grants you root access effortlessly. It’s almost like having a master key that opens all doors.
Besides the command line, Linux offers other ways to switch users, adding a layer of flexibility. For those using the GNOME desktop environment, you can change users through the System menu by navigating to “Power Off / Log Out” and then “Switch User.” This GUI method brings comfort to those who prefer visual interfaces over command prompts.
Contents
Understanding User Privileges in Linux
In Linux, user privileges play an essential role in maintaining system security and functionality. Users, permissions, and groups are key elements in defining what actions can be performed on the system.
The Role of Root and Sudo in System Administration
The root user is the ultimate authority in a Linux system, possessing unrestricted access to all commands and files. This makes the root user incredibly powerful but also potentially dangerous if compromised.
Regular users often don’t have all the necessary privileges. This is where sudo comes in. Sudo allows trusted users to execute commands with superuser (or other user) privileges. To control who can use sudo, the sudoers file is consulted. This file lists users and groups that can gain elevated privileges.
Using sudo ensures that administrative tasks are performed securely. It prevents the need for sharing the root password, as each user only needs their own password to gain access.
Pro Tip: Always use sudo instead of logging in as root directly to minimize risks.
Managing User Accounts and Groups
A Linux system manages user permissions through user accounts and groups. User accounts identify individual users, while groups allow for collective permission management.
When a user account is created, a unique user ID (UID) and an initial group are assigned. These groups help in managing permissions easily. For instance, adding a user to the sudo group grants them sudo privileges without modifying the sudoers file.
Groups can be as follows:
- System Groups: Predefined for system operations.
- Regular Groups: Created by users for managing permissions among various user accounts.
User permissions can be managed using commands like chmod, chown, and chgrp. These commands set read, write, and execute permissions at various levels.
| **Command** | **Function** | **Example** |
| chmod | Change file permissions | chmod 755 filename |
| chown | Change file owner | chown user:group filename |
| chgrp | Change file group | chgrp groupname filename |
Effective management of user accounts and groups ensures a secure and efficient Linux environment. Assigning appropriate permissions to users and regularly monitoring these settings helps maintain system integrity.
Mastering the Command Line for User Management
Diving into the command line for user management involves understanding basic and advanced techniques. We’ll cover the fundamental user commands and then explore more sophisticated methods for switching between users.
Managing users on the command line starts with a few essential commands. Understanding these can make our tasks simpler and more efficient.
Switching Users: Using the su command allows us to switch to a different user. The syntax is straightforward:
su - username
Replace username with the target account. If we need to switch to the root user, we can use:
sudo -i
This gives us root privileges, assuming we have the necessary permission.
Identifying Users: To check which user we are currently logged in as, we can use the whoami command:
whoami
This command displays our current username in the terminal.
Changing Passwords: The passwd command helps us update our password:
passwd
It will prompt us to enter the current password and then the new one.
Advanced Techniques for User Switching
For more advanced scenarios, we might need to execute commands as another user without fully switching to that user’s environment.
Executing Commands as Another User: The su command can be combined with -c to run a single command as a different user:
sudo su -c "command" -s /bin/bash username
Replace command with the specific command and username with the target user. This is useful for running operations that require temporary escalated privileges.
Using exec: If we want to completely replace the current shell with a new one as a different user, the exec command is handy:
exec su - username
This command logs us in as the new user and terminates the original session. Suitable for secure environments where the original user should not regain control.
Combining Commands: We often need to chain commands. Here, using && allows us to run multiple commands in sequence:
sudo su - root && cd /var/log && tail -f syslog
This switches to the root user, navigates to the logs directory, and displays the system log files continually.
Mastering these commands helps us manage user environments efficiently, ensuring our workflow on Linux remains smooth and secure.