- How to use the sudo command to become a root user
- Using the “sudo su –”
- Using the “sudo su”
- Using the “sudo -i”
- Using “sudo -s”
- How to use su command to become a root user
- Using “su –”
- Using “su -l”
- Using “–login”
- Bonus Tip
- Conclusion
- About the author
- Change user in CLI
- Change user using su
- Change user using sudo
- Change users in GUI
- Change user in GNOME
- Final thoughts
- About the author
- Method #1 – Graphical Environment
- Method #2 – The su command
- Basic su Command Usage
- How to use the su command to switch to a different user
- How to use the su command to log in with a different shell
- How to use the su command while preserving the environment
- How to use the su command to run a command as a different user
- Method #3 – SSH
- Conclusion
- About the author
- How to Run Commands in the Terminal as a root User?
- Using the “su” Command
- Using the “-s” Command
- How to Set a Password for root?
- Become root Using the “-login” Command
- Become root Using the “-l” Command
- What is root user? Why is it locked in Ubuntu?
- How to run commands as root user in Ubuntu?
- How to become root user in Ubuntu?
- How to enable root user in Ubuntu?
- Change user in Linux command line
- Change users in Linux graphically (for desktop Linux)
- How to login as a root in the GNOME desktop
- Step 1: Enable root account
- Step 2: Change GDM configuration
- Step 3: Configure PAM authentication
- Step 4: Log in as root
- Things you should know when running the system as a root user
- Su Command Syntax
- su Command Options
- Switch to a Different User
- Run Specific Command as a Different User
- Use a Different Shell
- Use a Different User in the Same Environment
- Command Comparison
- Syntax
- Options
- Granting sudo Privileges
- RedHat and CentOS
- Debian and Ubuntu
- Using visudo and the sudoers Group
- Examples of sudo in Linux
- Basic Sudo Usage
- Run Command as a Different User
- Switch to Root User
- Execute Previous Commands with sudo
- Run Multiple Commands in One Line
- Add a String of Text to an Existing File
- How to Use the su Command
- How Does the su Command Work?
- How Does the su – [hyphen] Command Work?
- How to Use the sudo Command
- Adding a User to the Sudoers Group
- How sudo and su Work on Different Linux Distributions
- Enabling the Root Account
- Additional Features
How to use the sudo command to become a root user
Using the “sudo su –”
Using the “sudo su”

Using the “sudo -i”
The output shows that the dollar sign($) has been replaced by the pound key(#) which also directs that you are currently logged in using the root privileges.
Using “sudo -s”

How to use su command to become a root user
Using “su –”

Using “su -l”

Using “–login”

During the authentication of su command, you have to put the root password.

Bonus Tip
The output shows that you are not logged in using the root privileges.
Conclusion
About the author
- System: System processes are run under this user.
- User: The account that general users login to by typing the username and password.
- Root: A special user that has unrestricted privilege over every part of the system.
Change user in CLI
Change user using su



Verify the change.

It’s strongly recommended not to log in as root unless you have to. If it’s just for running commands with root privilege, then the sudo tool offers more flexibility and security.

Change user using sudo

Verify the action.

Change users in GUI
Change user in GNOME
GNOME is one of the most popular desktop environments out there. It’s available on almost any popular Linux distro. Its interface is pleasing, modern, and fluid. Of course, you can tweak it to give it a more pleasant vibe. Check out some of the best GTK3 themes for Linux. These are GNOME-compatible themes and should work on any modern version of GNOME.
Final thoughts
About the author
Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.
Method #1 – Graphical Environment


Method #2 – The su command
Basic su Command Usage
The su command is easy to use with straightforward syntax.
Using the su command with no option does not change the current working directory to /root.
The most common use case of the su command is with the – or -l option. The -l allows su to changes the home directory and spawn a shell session similar to a real-login (set in /etc/passwd).
How to use the su command to switch to a different user
How to use the su command to log in with a different shell
By default, su uses the environment variables stated in the /etc/passwd file. Such options include the default shell. However, you can change the login shell using the -s option.
ubuntu bin
How to use the su command while preserving the environment
In that case, we can use the command:

How to use the su command to run a command as a different user
Method #3 – SSH

Conclusion
About the author
- How to Run Commands in the Terminal as a root User?
- How to Become Root In Linux?
- How to Set a Password for root?
How to Run Commands in the Terminal as a root User?
There are some scenarios where you want to use the terminal as a root but don’t want to switch, which you’ve done many times, as seen below.
Using the “su” Command
Using the “-s” Command
How to Set a Password for root?
Activating the root account raises security concerns, especially if a service is exposed to the web and you’re using it, so it is not recommended to activate it. Professionals should only use this account, and to deactivate it, remove the password using this command.
If you see the message “password expiry information changed.” Then the root account is locked again.
Become root Using the “-login” Command
Become root Using the “-l” Command
Either you run commands with root privilege like this:
sudo any_commandsudo suWhat is root user? Why is it locked in Ubuntu?

You don’t need to have root privilege for your daily tasks like moving file in your home directory, downloading files from internet, creating documents etc.
Take this analogy for understanding it better. If you have to cut a fruit, you use a kitchen knife. If you have to cut down a tree, you have to use a saw. Now, you may use the saw to cut fruits but that’s not wise, is it?
Does this mean that you cannot be root in Ubuntu or use the system with root privileges? No, you can still have root access with the help of ‘sudo’ (explained in the next section).
How to run commands as root user in Ubuntu?
apt update
Reading package lists... Done
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)So, how do you run commands as root? The simple answer is to add sudo before the commands that require to be run as root.
sudo apt update[email protected]:~$ sudo apt update
[sudo] password for abhishek: If you are absolutely new to Linux, you might be surprised that when you start typing your password in the terminal, nothing happens on the screen. This is perfectly normal because as the default security feature, nothing is displayed on the screen. Not even the asterisks (*). You type your password and press enter.
Bottom line:
To run commands as root in Ubuntu, add sudo before the command.
When asked for password, enter your account’s password.
When you type the password on the screen, nothing is visible. Just keep on typing the password and press enter.
How to become root user in Ubuntu?
The sudo command allows you to simulate a root login shell with this command:
sudo -i[email protected]:~$ sudo -i
[sudo] password for abhishek: [email protected]:~# whoami
root[email protected]:~# You’ll notice that when you switch to root, the shell command prompt changes from $ (dollar key sign) to # (pound key sign). This makes me crack a (lame) joke that pound is stronger than dollar.
sudo suIf you try to use the su command without sudo, you’ll encounter ‘su authentication failure’ error.
exitHow to enable root user in Ubuntu?
sudo passwd rootAgain, this is not recommended and I won’t encourage you to do that on your desktop. If you forgot it, you won’t be able to change the root password in Ubuntu again.
sudo passwd -dl rootI hope you have a slightly better understanding of the root concept now. If you still have some confusion and questions about it, please let me know in the comments. I’ll try to answer your questions and might update the article as well.
su another_username
Change user in Linux command line

su another_usernamesu - another_username
Note: though -is more popular, it is advised to use –login option.
Change users in Linux graphically (for desktop Linux)
- Switch User: You get to keep your session active (applications keep on running) for current user. Good for temporarily switching users as you won’t lose your work.
- Log out: Current session ends (all applications are closed). Good when you want to switch to the other user for a long time.
You can choose whichever option is more suited for your need.

By default, Ubuntu disables the root account. You must use the sudo command for any tasks requiring root privileges.
This is for your own security, of course. Using the system as root all the time is like running around with a sword in your hand. It increases the chances of messing up things.
Logging in as root is still common in the servers. On the desktop side, it’s quite rare to log in as root. Even Kali Linux has changed it.
in this guide, I will show you how to log in as a root in your GNOME desktop using Ubuntu.
How to login as a root in the GNOME desktop
I won’t advise login as root on the desktop. You have sudo mechanism for all your root needs. Do it only if you have a good enough reason. This tutorial is for demo purposes only. You have been cautioned.
Step 1: Enable root account
You want to log in as root. But the root account is disabled by default. The first step is to enable it.
Change the root account password that will eventually enable the root account for you:
sudo passwd root
It goes without saying that you should not forget the root password.
Step 2: Change GDM configuration
Ubuntu uses GNOME by default and GNOME uses the GDM display manager.
To allow log in as root into GNOME, you need to make some changes in the GDM configuration file located at /etc/gdm3/custom.conf.
Make a backup of the config file:
cp /etc/gdm3/custom.conf /etc/gdm3/custom.conf~In the worst case, if you somehow mess things up, the back up file can be used to replace the existing one from the TTY.
sudo nano /etc/gdm3/custom.confAllowRoot=true
Press Ctrl+X to exit Nano while saving it.
Step 3: Configure PAM authentication
sudo nano /etc/pam.d/gdm-passwordauth required pam_succeed_if.so user != root quiet_success
Save changes and exit from the nano text editor.
Step 4: Log in as root
Now, reboot your system:
reboot

That’s it! Now, you are running your Ubuntu system as a root.
Things you should know when running the system as a root user
There is a reason why Ubuntu disables a root account by default. Want to know why? Here you have it:


In this guide, you will learn how to use the su command, with practical examples.

- A system running Linux
- Access to a terminal window/command-line (Ctrl-Alt-T, Ctrl-Alt-F2)
Su Command Syntax
su [options] [username [arguments]]su Command Options
su –h
Here are some common options to use with the su command:
Username– Replace username with the actual username you want to log in with. This can be any user, not just root.–cor–command [command]– Runs a specific command as the specified user.–or–lor–login [username]– Runs a login script to change to a specific username. You’ll need to enter a password for that user.–sor–shell [shell]– Allows you to specify a different shell environment to run in.–hor–help– Show the help file for thesucommand.–por––preserve–environment– Preserve the shell environment (HOME, SHELL, USER, LOGNAME).
Switch to a Different User
su –l [other_user]Run Specific Command as a Different User
su –c [command] [other_user]When you enter this example, the system will use the specified account to run the ls (list directory contents) command.
Use a Different Shell
su –s /usr/bin/zshUse a Different User in the Same Environment
su –p [other_user]Command Comparison
sudo su -su is an older but more fully-featured command. It can duplicate the functionality of sudo by use of the –c option to pass a single command to the shell.
Next, consider checking out our Linux Command Cheat Sheet Tutorial With Examples.

- A system running Linux
- Access to a command line/terminal window (Activities > Search > Terminal)
- A user account with
sudoorrootprivileges
Syntax
sudo [command]Options
sudo can be used with additional options:
-h– help; displays syntax and command options-V– version; displays the current version of the sudo application-v– validate; refresh the time limit on sudo without running a command-l– list; lists the user’s privileges, or checks a specific command-k– kill; end the current sudo privileges
Additional options can be found under the -h option.

Granting sudo Privileges
RedHat and CentOS
usermod -aG wheel [username]Debian and Ubuntu
usermod -aG sudo [username]Using visudo and the sudoers Group
1. Use the visudo command to edit the configuration file:
sudo visudo[username] ALL=(ALL:ALL) ALL
3. Save and exit the file.
Here’s a breakdown of the granted sudo privileges:
[username] [any-hostname]=([run-as-username]:[run-as-groupname]) [commands-allowed]Examples of sudo in Linux
Basic Sudo Usage
apt-get update2. You should see an error message. You do not have the necessary permissions to run the command.

3. Try the same command with sudo:
sudo apt-get update4. Type your password when prompted. The system executes the command and updates the repositories.

Run Command as a Different User
whoamisudo -u [different_username] whoami
Switch to Root User
sudo bashYour command line should change to:
root@hostname:/home/[username]
Execute Previous Commands with sudo
The Linux command line keeps a record of previously executed commands. These records can be accessed by pressing the up arrow. To repeat the last command with elevated privileges, use:
sudo !!sudo !6This example repeats the 6th entry in history with the sudo command.
To learn about how to efficiently use history command, check out our article on sudo history command with examples.
Run Multiple Commands in One Line
String multiple commands together, separated by a semicolon:
sudo ls; whoami; hostname
Add a String of Text to an Existing File
echo ‘string-of-text’ | sudo tee -a [path_to_file]echo "deb http://nginx.org/packages/debian `lsb_release -cs` nginx" \ | sudo tee /etc/apt/sources.list.d/nginx.list
Note: This would add the Nginx software repositories to your system.
You should now understand the sudo command, and how to use it. Next, learn the difference between the sudo and su command.
In this tutorial, learn all about the difference between su and sudo.

How to Use the su Command
The main syntax is:
su [user_name]su - [user_name]How Does the su Command Work?
su phoenixnapThen, provide the password for the phoenixnap account and hit Enter.

How Does the su – [hyphen] Command Work?
su - phoenixnapHow to Use the sudo Command
The main syntax is:
sudo [command]Adding a User to the Sudoers Group
usermod -aG sudo [user_name]For instance, to add the account phoenixnap, you would type:
sudo usermod -aG sudo phoenixnapTo see a list of accounts that belong to the sudoers group run:
sudo getent group sudoThe output should display the account added in the previous step:

How sudo and su Work on Different Linux Distributions
su is an older but more fully-featured command included in all Linux distributions. It is the traditional way to switch to the root account.
Linux discourages working as root as it may cause unwanted system-wide changes and suggests using sudo instead. For this reason, all Ubuntu-based releases are sudo-only, meaning the root account is not active by default.
Enabling the Root Account

sudo passwd root
Additional Features
sudo -i
Likewise, su can also function as sudo and run a single command as the root:
su -c [command]After reading this article, you should understand the difference between the sudo and su command. Remember to use sudo whenever you can to prevent potential security and system-wide issues.






