How do I set the root password so I can use su instead of sudo?

535,171

Solution 1

Warning: Directly logging in as root is like playing with fire, because one little typo is enough to lose critical data or make your system unbootable. Note that desktop environments will also function incorrectly if you login to them as root.

See these questions for the reasons behind why sudo is preferred and why root-login is disabled by default:

  1. What are the benefits of sudo over su?
  2. Why is there no option to login as root?

You may have noticed that you can’t log in as root on Ubuntu, this is because root doesn’t actually have a password set.

Enter the following command:

sudo passwd

You will be prompted for your current user's password, followed by the password you want to set for root. The messages prompted should be similar to the following:

[sudo] password for <username>: <Type your user password and press return>
Type new UNIX password: <Type the root password you want>
Retype new UNIX password: <Retype the root password you chose in the previous prompt>

The following message will appear after that:

passwd: password updated successfully

If the above message showed up on your terminal, you can now enter as root from your current user entering the following command:

su

And you will be prompted for the root password you've set. That's it!

Solution 2

If you still want to use su after reading the warnings in the question, there is no need to set a root password. Just do sudo su and you're on your way with your regular password.

Solution 3

What I do to create the root on ubuntu: first I do this

sudo -i

then

passwd

as @Zignd mentioned the classical messages will appear:

Type new UNIX password: [Type the root password you want]
Retype new UNIX password: [Retype the root password you chosen before]
passwd: password updated successfully

then I can use the su as a normal root acount...

Share:
535,171
Zignd
Author by

Zignd

Updated on September 18, 2022

Comments

  • Zignd
    Zignd over 1 year

    Possible Duplicate:
    How to know my root password?

    I'd like to define a password for root so that I can use su directly, without having to prepend it with sudo. And because I'd like to log in as root as well.

    So how to set a password for the root user?

    • Keith Thompson
      Keith Thompson almost 12 years
      Why do you want to have a root password? You don't need it; any time you need root access, you can use sudo.
    • Keith Thompson
      Keith Thompson almost 12 years
      @Zignd: No, sudo requires your password, not the root password. Ubuntu is designed to work without a root password. See help.ubuntu.com/community/RootSudo
    • CashCow
      CashCow almost 8 years
      You sometimes need root password for recovery
    • cheshirekow
      cheshirekow almost 7 years
      @KeithThompson If systemd fails it will ask for root password to enter maintenance mode: Welcome to emergency mode! Give root password for maintenance (or press Control-D to continue):
    • Keith Thompson
      Keith Thompson almost 7 years
      @cheshirekow: Hmm, I've never run into that. I'm currently running Ubuntu 16.10. Looking at my /etc/shadow, I see that my system doesn't have a root password, and I was never prompted to create one when I set up the system. I guess if I ever get that "Give root password for maintenance" prompt I'll just have to press Control-D. (I haven't looked into systemd.)
    • Manachi
      Manachi almost 7 years
      @KeithThompson it's not unreasonable to want to use su to login as root if you so wish. It is after all, your environment.
    • Keith Thompson
      Keith Thompson almost 7 years
      @Manachi: Sure, you can do anything you like with your environment. But you can get a root shell via sudo -i or equivalently sudo --login, which doesn't require a root password. (But see the above comments regarding systemd, which I still haven't looked into.)
  • Keith Thompson
    Keith Thompson almost 7 years
    Or sudo -i, or sudo --login
  • Yves
    Yves about 6 years
    To be simplified, sudo -i passwd can do the same thing.
  • Prajwal Dhatwalia
    Prajwal Dhatwalia over 4 years
    FYI, this works for RedHat Linux on vagrant as well.