How to Log in as Superuser or Root User in Linux
The superuser, also known as the root user or admin account, is a special user account reserved for system administration tasks on Linux. For security reasons, regular users and developers are typically restricted from directly accessing the root account by default. Here’s how you can become a superuser in Linux:
Tutorials Details | |
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux Terminal |
Category | User Information |
What is a Linux superuser account?
In Linux and other Unix-like operating systems, the “root” user is the account with full privileges and access to all files and programs across all modes, whether in single-user or multi-user environments. The root user can perform tasks that ordinary users cannot, such as changing file ownership and accessing ports below number 1024. The term “root” likely originates from the fact that this user is the only one with the authority to modify the root directory of a Unix system. The role of a system administrator (sysadmin) is diverse and can vary significantly depending on the organization. Sysadmins are responsible for installing, supporting, and maintaining servers and computer systems, as well as planning for and addressing service outages and other issues.
Command to Login as Superuser in Linux
To log in as the superuser or root user on Linux, you can use one of the following commands:
- su command – Run a command with a substitute user and group ID.
- sudo command – Execute a command as another user.
- doas command – An alternative to the sudo command, originating from the OpenBSD project. It is compatible with several Linux distributions, such as Alpine Linux.
- /etc/passwd – The Linux user account file.
- /etc/group – The file that defines group memberships, granting users superuser access.
- /etc/shadow – The file containing Linux password information for each user, with passwords stored in a hashed format.
How to Become a Superuser in Linux
Here are some examples of using the su
and sudo
commands to gain superuser privileges in Linux.
Become a Superuser in Linux Using the sudo Command
To use the sudo
command, you must be part of a specific secondary group on Linux:
- sudo group on Debian or Ubuntu
- wheel group on CentOS, RHEL, Fedora, or Oracle Linux
You can check your secondary group membership by running either the groups
command or the id
command.
groups
id
uid=1000(admin) gid=1000(admin) groups=1000(admin),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev)
You can also use the grep
or egrep
command to check if you are part of the sudo
or wheel
group. For example:
grep 'sudo' /etc/group
egrep 'sudo|wheel' /etc/group
egrep 'sudo|wheel' /etc/group | grep $USER
sudo:x:27:admin
Next, run the following command to switch to the root/superuser:
Superuser Login – How to Become a Superuser in Linux Using su
In Linux and similar Unix-based systems, the su
command allows you to switch to another user during a login session or log in as the superuser. If used without specifying a username, su
defaults to switching to the superuser account.
It is highly recommended to use the -
option with the su
command. This option provides an environment similar to what the root user would have if they logged in directly. Use the su
command as follows:
su -
Exiting a su or sudo Session
To exit a su
or sudo
session, simply type one of the following commands:
exit
logout
Another way to exit a su
or sudo
session is by pressing the CTRL and D keys simultaneously.