How Can I Log In As root User?


How do I log in as root user under Linux, Apple OS X, *BSD, and UNIX-like operating systems?

On Linux, *BSD, and UNIX like opeating systems the root user act as a superuser. Root user 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 new file system, starting/stopping services, and binding to ports numbered below 1024 and more.

It is not good a 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 command to switch to root user account.

su command

The su command is use to change user ID or become super-user during a login session i.e. it allows you to become a super user or substitute user, spoof user, set user or switch user.

Note: su 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 super user. The user will be prompted for a password, if appropriate.

To log in as root user type the following command (you need to supply root user account password when prompted):
$ su -
Sample outputs:

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 nixcraft user account password when prompted):
$ su - nixcraft
Sample outputs:

Password:

Remote root login over the ssh session

You can use the ssh client as follows:
$ ssh [email protected]
$ ssh [email protected]
$ ssh [email protected]

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

 ## login as a normal user ##
ssh user1@server1.sxi.io
 
## now switch to root account ##
su -

su command and log files

The su command logs its usage in a system log file. This is useful to find out su login information. If you are RHEL / CentOS / Fedora Linux user type the following as root to see the contents of /var/log/secure:
# tail -f /var/log/secure
Debian / Ubuntu Linux user try:
# tail -f /var/log/auth.log
Sample outputs:

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 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, normally 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 Mac OS X and Ubuntu Linux and many other oses uses sudo command for many administrative tasks.

sudo executes a command as another user but follows a set of rules about which users can execute which commands as which other users. This is configured in a filed named /etc/sudoers. 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 login as root under Linux or Unix like operating system, type:
$ sudo -s

Sample sudo command session

Fig.01: Switching to root account using sudo command

Sudo 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)
See also

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.