How can I log in as the root user in Linux or Unix?

sudo command demo

See all UNIX related articles/faq
Are you trying to log in as a root user on a Linux, Apple macOS (OS X), *BSD, or UNIX-like operating system? Here are some helpful tips and tricks to successfully login as a root user on your machine.

The person responsible for setting up and maintaining the system or server is called the system administrator, and it is the role of the system administrator. Therefore, they need to log in as an admin user on all Linux and Unix machines. The root user acts as a superuser on Linux, macOS, *BSD, and UNIX-like operating systems. It is the conventional name of the user who has all rights or permissions on the system. The root user can do many things an ordinary user cannot, such as changing the ownership of files, mounting disk, formatting & restating a new file system, starting/stopping services, and binding to ports numbered below 1024 and more. It is not good practice for anyone to use root as their normal user account because of security risks. Therefore, you use a normal user account instead. You need to use the su or sudo or doas command to switch to root user account. Let us see how to log in as a root user.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux or Unix terminal
Category System Management
OS compatibility BSD Linux macOS Unix
Est. reading time 3 minutes
Advertisement

su command

The su command is used to change the user ID. In other words, to become a superuser during a login session, use the su command. This command allows you to become a superuser or substitute user, spoof user, set user or switch user. The su command only works if you know the target or root user’s password.

su command syntax

The syntax is:
$ su - {user-name}
If invoked without a user name, su defaults to becoming the superuser. The user will be prompted for a password, if appropriate. For example, type the following command to log in as a root user. Next, open the Terminal application and type the following command followed by the [Enter] key:
$ su -
You need to supply the root user account password when prompted:

Password: *******
# 

Once logged in, your prompt should change from $ to # . To log in as another user, say nixcraft, type the following command (you need to supply a nixcraft user account password when prompted):
{tom@linuxbox:~ }$ su - nixcraft
Sample outputs:

Password:
{nixcraft@linuxbox:~ }$ 

Remote root login over the ssh session

You can use the ssh client/command command as follows:
$ ssh root@server.ip-address-here
$ ssh root@server1.cyberciti.biz
$ ssh root@1.2.3.4

However, remote root login over ssh session is disabled in most cases for security reasons. Hence, first, log in as a regular user and then switch to the root account using the su command:

 ## STEP #1 - Login as a normal user account ##
ssh user1@server1.cyberciti.biz
 
## STEP #2 - Now switch to the root account ##
su -
 
## STEP #3 - Let us run sysadmin commands such as updating packages ##
yum upgrade # <-- FOR RHEL/CentOS server
apt update && apt upgrade  # <-- For Debian/Ubuntu server

su command and log files

The su command logs its usage in a system log file. Now, this is useful to find out su login information. For example, if you are RHEL/CentOS (CentOS Stream)/Alma/Rocky/Fedora Linux user, type the following tail command as root to see the contents of /var/log/secure:
# tail -f /var/log/secure
Debian / Ubuntu Linux user try the following tail command on the /var/log/auth.log:
# tail -f /var/log/auth.log
Here is what I see:

May 30 23:02:56 wks05 su[23520]: pam_authenticate: Authentication failure
May 30 23:02:56 wks05 su[23520]: FAILED su for root by nixcraft
May 30 23:02:56 wks05 su[23520]: - /dev/pts/3 nixcraft:root
May 30 23:02:59 wks05 su[23521]: pam_unix(su:auth): authentication failure; logname=nixcraft uid=1000 euid=0 tty=/dev/pts/3 ruser=nixcraft rhost=  user=root
May 30 23:03:01 wks05 su[23521]: pam_authenticate: Authentication failure
May 30 23:03:01 wks05 su[23521]: FAILED su for root by nixcraft
May 30 23:03:01 wks05 su[23521]: - /dev/pts/3 nixcraft:root

Say hello to the sudo command

The sudo is a program for Linux / Apple OS X / *BSD / Unix-like computer operating systems that allows users to run programs with the security privileges of another user, usually the superuser (root). By default, sudo will prompt for a user password, but it may be configured to require the root password or no password at all. Apple macOS, Ubuntu Linux, and many others use the sudo command for many administrative tasks by default.

Sudo executes a command as another user but follows a set of rules about which users can execute which commands as which other users. The sudo comes with /etc/sudoers config file. Unlike su, sudo authenticates users against their own password rather than that of the target user. See how to configure and use sudo tool under Linux operating system.

For example, to log in as root under Linux or Unix like operating system, type:
$ sudo -s
OR
$ sudo -i
When prompted type your password. Here is a sample sudo command session to login as the root user:

Fig.01: Switching to root account using sudo command

Fig.01: Switching to root account using sudo command

The sudo command and log file

The sudo log can be viewed by issuing the following command as root user:

## Generic file for Unix/Linux ##
tail -f /var/log/messages
 
## Debian/Ubuntu and friends ##
tail -f /var/log/auth.log
 
## RHEL/CentOS/SL/Fedora Linux ##
tail -f /var/log/auth.log

Sample outputs:

May 30 23:12:42 wks05 sudo: nixcraft : TTY=pts/3 ; PWD=/tmp ; USER=root ; COMMAND=/bin/bash
May 30 23:12:42 wks05 sudo: pam_unix(sudo:session): session opened for user root by nixcraft(uid=1000)

Say hello to the doas command

Prerequisite
By default, doas command may not be installed on your system. Hence, use the apk command on Alpine Linux, dnf command/yum command on RHEL & co, apt command/apt-get command on Debian, Ubuntu & co, zypper command on SUSE/OpenSUSE, pacman command on Arch Linux to install the doas.
The doas utility executes the given command as another user. The doas command first appeared in OpenBSD 5.8 as a drop-in replacement for the sudo. This command is defaulted on OpenBSD and may be installed on other operating systems like Alpine Linux. Run the apk command to install doas package:
# apk add doas
Edit the /etc/doas.d/doas.conf file:
# vim /etc/doas.d/doas.conf
Uncomment or the following line so that we can allow any member of group “wheel” to become root:

permit persist :wheel

Save and close the file.

Adding a new user named ‘vivek’ (admin user)

Essential files

  1. The /etc/passwd file contains one line for each user account.
  2. The /etc/group file defines the groups on the system.
  3. Users encrypted password hash stored in the /etc/shadow file.
  4. The /etc/shells is a text file that contains the full pathnames of valid login shells. You must only shell listed in this file when setting or editing up user accounts on Linux to avoid login errors.
See a list of all available shells using the cat command:
# cat /etc/shells
Create a new user named ‘vivek’ (admin user on Alpine Linux):
# adduser -h /home/vivek -g 'Vivek Gite sysadmin' -s /bin/sh vivek
Add a user named ‘vivek’ to a group called ‘wheel’:
# adduser vivek wheel
Verify group membership for the user named ‘vivek’ using the grep command as follows:
# id vivek
# grep vivek /etc/group
# grep vivek /etc/passwd

Add a new user account on Alpine Linux demo

Adding admin user named ‘vivek’ on Alpine Linux (click to enalrge)

Make sure doas working. First, log in as vivek using the su command and then try to switch to the root account by issuing the doas command:
# su - vivek
$ doas sh

When prompted, type vivek’s password and then press [Enter] key:

doas (vivek@nixcraft-osf) password:
# 

Again run the id command for verification:
# id
# exit
# exit

doas syntax

The syntax is as follows to run command as the root user:
$ doas command
$ doas ps aux

Want to execute the command as user named ‘nginx’ (the default is the root user)? Pass the -u {USER} option:
$ doas -u nginx command1

Summing up

Do read the following man pages using the man command or help command:
$ man sudo
$ man su
$ man doas

See also

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

33 comments… add one
  • Arif May 13, 2014 @ 21:39

    hi,

    I ran following commands as root and now im am unable to su from any user/admin please advise.

    chmod -o-x /bin/su
    chmod -g-x /bin/su

  • robert Oct 14, 2014 @ 20:04

    Linux has become more and more like microsoft, not allowing root log in? really?
    I thoguht the whole selling point for linux back in the day was that we didnt have the hood welded shut!! Any one remember that, I also remember that in the beginning of linux.. back when it was all terminal there was only root!! you could add other users of course, but why? Any way my point being is that with out Root LOG IN!! you are just anothe windows microsoft puppet.

  • Leo Leavitt Mar 22, 2015 @ 0:28

    I have read all of the notes on root login and still do not have the answer that i want. I have over 30 years in the business and since retired a 100% disabled Vet. I just want to be able to Login as root just like any other user and I do not want to have to su each time to do things. I am well aware of the risks but since this is my own Ubuntu System I think I am capable of managing it. I am running UBuntu 14.10 and what I would like is to be able to do the following;
    Login root
    passwrd lslslslslsl
    and then I will be root for anything that I need . Please help with a simple straight way to do this in UbunTu.. DO I need to use Fedora instead ??
    Leo
    Leo Leavitt
    leoleavitt@gmail.com

  • Mashkoor Qadir Dec 2, 2016 @ 6:10

    Hi Team,

    I am having a problem since yesterday. A server was running since 2 years and every thing was working fine. suddenly yesterday it refused to accept the credentials. It doesn’t allow to login even with root user.

  • JOS Jan 12, 2017 @ 23:23

    QUERO UTILIZA O ROOT PARA PROGETOS

  • Elmore Toppin Feb 10, 2017 @ 15:40

    Hi

    I installed Ubuntu through VMware Fusion on a Mac, when the installation process completed. I was only given an option to create a root user and root password. This does not allow you to log onto the system. The question is how do you log onto Ubuntu with this root password. Or how do you log onto Ubuntu, to get to this root user when no other user credentials were created at installation.
    Kind regards
    ET

  • Kristian May 29, 2023 @ 17:37

    Thank you for providing such a helpful page that met my needs. I am now able to utilize my Red Hat Enterprise Linux 9 with sudo command. I was previously unaware that I needed to enter my own password to gain root-level system access, but your page proved to be the most useful resource for me.

  • SURESH Jun 3, 2024 @ 11:12

    very nice and useful commands demo. thank.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.