Skip to main content
Tips

How to Change Users in Linux Command Line

Linux systems have different users and different types of users. Learn how to switch the users in Linux command line or how to change to the root user in Linux.

Helder

Warp Terminal

Linux systems have different types of users with different types or permissions as well.

Not all users can execute all commands and not all users are allowed to switch to other users neither. This all might sound confusing but, I will try to explain these so it can be easy to understand.

For the moment, here’s a quick summary of how to switch users in Linux command line.

To switch users, you need to know the password of that user. You can switch the users with this command:

su – <username>

To switch to root user in Ubuntu, you can use this command:

sudo -i

Various user types in Linux

If you list all users in Linux, you’ll see a lot of users that you didn’t know about. Who are these users? Where did they come from? I could write an entire article in regards of how users work in Linux, however, this is not the idea for this one.

Basically, there are 3 types of users in Linux:

1. System Users

These are the users that are automatically created in Linux systems to be able to run services or applications and are not intended to log in to the system (in fact you can’t log in as any of these users).

2. Regular Users

These are the (human) users who can log in to a system. Each of these users might have or not different permissions or levels in the system which is given by the groups they belong to.

3. Super Users

These are system administrators or users who can perform high-level tasks that can be considered critical or system dangerous.

Switch users in the command line

When using a Linux system you can log in with a user and then simply “switch” to another user through the same command line session. In order to do this, there is a command “su -“, which allows you to switch to become another user:

johndoe@system:~$ su - janedoe 
Password:  
janedoe@system:~$ 

In the above example, you need to know the password of janedoe in order to switch to that user. Which makes sense because if you are going to switch to a user, you need to know the password of that user else it will be a security risk.

Switch to root user

For security reasons, some systems have ‘root’ account blocked for direct login, either locally or remotely, so this means it will not accept someone who tries to log in using ‘root’ even with the correct password.

So, how do you perform actions as the ‘root’ user? That’s what the ‘sudo’ command allows you to.

The sudo command will basically execute anything you want in the system as if the ‘root’ was doing it. You don’t need to know the ‘root’ user’s password, in fact, probably nobody knows it or there is no password assigned to ‘root’. You only need to know your own user’s password and that user must be in the ‘sudoers’ group, which is basically the group of users which can use ‘sudo’ in the system.

Normally, it is a good practice to run the commands with sudo that needs to run with root permission like this:

sudo <command>

But if you want to change to root user so that all the subsequent commands will be run as root, you can use:

sudo -i

You’ll use your own password here, not the root account’s password.

Change to root user in Linux

As a sudo user yourself, you can create sudo user by adding the user to sudo group.

Conclusion

Linux systems allow you to easily switch users or execute high-level commands with the usage of ‘su‘ and ‘sudo’ commands. And remember: with great sudo power comes great responsibility!

Helder
@heldmar Montevideo, UY