Login as Root User on Raspberry Pi

Jinku Hu Jan 24, 2022 Nov 04, 2021
  1. General Access Control Concepts in Raspberry Pi OS and Root Account
  2. Log in as Root From the Default pi User
  3. Setup a Password for Root User in Raspberry Pi OS
  4. Access Root User via SSH Session
Login as Root User on Raspberry Pi

This article will introduce several cases of logging in as the root user on Raspberry Pi OS.

General Access Control Concepts in Raspberry Pi OS and Root Account

As you already might know, Raspberry Pi OS is based on Debian Linux, and many core parts of the operating system have the same interface as far as the system administrator is concerned.

Generally, a Linux-based system can have multiple users, and they can simultaneously use the resources on it without conflicting with each other. Linux systems also define a special administrative user called - root, which is capable of manipulating every aspect of the system. Although, this unlimited permission raises the issue of properly guarding access to it, which is why some Linux distributions like Raspberry Pi OS disable it by default.

Newly installed Pi OS has the default pi user and the password raspberry. These credentials are used for logging in on the first boot. Also, at this stage, the system setup GUI prompt will suggest changing the default password. If you missed this step on the initial setup process, you could always modify it using the passwd command via a CLI.

Log in as Root From the Default pi User

In this case, we are assuming that you are logged in as the pi user and have access to the command-line interface. Enter the following command to log in as the root user.

sudo su

The above method should work without requiring any password on the current Pi OS version, assuming that corresponding system config modifications weren’t done until this point. Note that the sudo command is used to elevate the privileges for regular users of the operating system to the superuser level. This way, a single command (like su) is executed with the root privilege, and once it returns, the control is given back to the regular user. This technique is used as a security measure for large multi-user systems, and it has many benefits.

Here, we essentially utilize sudo provided privileges to run su command, which yields a root shell access. Notice that executing a sudo prefixed commands is in itself a privilege for regular users, and the pi user happens to have that by default in Pi OS. Once you have accessed a root shell, you are able to run all commands using the superuser privileges, and when you are done, you can exit it using the Ctrl+D shortcut.

Setup a Password for Root User in Raspberry Pi OS

The previous solution to access the root user is a small hack that requires you to log in as pi user first, but we can have a solution where a root user is activated and has a separate password setup. This requires sudo privileges to configure at first. Thus, we need to run the following command from the pi user:

sudo passwd root

This command will prompt for a new password to be entered and then retyped once again. Consequently, you can log in as root user from the current terminal session, or you may log out and log in using new root credentials. Note that you’d need to run su command without sudo prefix to access the root shell using a new password.

Access Root User via SSH Session

If you followed the last instruction and did set up a separate password for the root user, you can proceed with the next commands and allow access over SSH. At first, you need to identify if your user is pi or root, which you can quickly notice on the left of the CLI line or retrieve it by executing a whoami command. Then you can run one of the commands based on the current logged in user:

pi user:

sudo nano /etc/ssh/sshd_config

root user:

nano /etc/ssh/sshd_config

These commands open the SSH configuration file using a CLI text editor nano, a more user-friendly interface for Linux/Unix beginners. However, you can open this file for editing with any CLI text editors you’re familiar with e.g., Vim or Emacs. Then you should look for a line that says:

#PermitRootLogin prohibit-password

Now, delete # to uncomment the text and replace prohibit-password with yes string. Then, save the changes by clicking Ctrl+X and then inputting Y. At this point, nano will prompt to overwrite the given file, and you can confirm by pressing the Enter key. Finally, you need to reboot the SSH service using the next command for it to load a new configuration:

pi user:

sudo systemctl restart ssh

root user:

systemctl restart ssh
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.


Related Article - Raspberry Pi