PDF download Download Article
Switch to root or unlock the root account with this complete guide
PDF download Download Article

The "root" user account on Linux has full administrative privileges over the entire system. If you want to edit system configuration files, install system-wide software, add users, or virtually anything else outside of your home directory, you'll need root access. For most tasks, you won't need to log or switch to the root user account—you can run administrative tasks with the sudo command to prevent you from doing damage while logged in with full superuser permissions. If you're using Ubuntu, the root account is locked by default to prevent this from happening. If you need to keep root access while doing a large number of system tasks, it's still possible to become root. Read on to learn all the ways to gain root access in Linux.

How to Run Commands as Root in Linux

Many Linux distros disable the root user by default for security reasons. You can still run commands as root by prefacing them with "sudo". You can also start a root shell using "sudo -i." To log in as root or switch to the root user in a terminal, set a password for the root user, then use "su -" to become root.

Method 1
Method 1 of 5:

PDF download Download Article
  1. Open the terminal . Ubuntu and several other distributions lock the root account automatically to prevent you from using commands that can damage your system. You can unlock the root account in the terminal. If you're in the desktop environment, you can press Ctrl + Alt + T to start the terminal. [1]
    • The root account is locked in many Linux distributions to discourage you from logging in as the root user. In most cases, you should never need to log in as root because you can run elevated commands using {{kbd|sudo]}. To learn how to use it, jump to Using Sudo .
  2. When prompted for a password, enter your user password. [2]
    Advertisement
  3. You'll be prompted to create a password for the root account and enter it twice. Once a password has been set, the root account will be active. [3]
  4. If you want to lock the root account, enter the following command to remove the password and lock root:
    • sudo passwd -dl root
  5. Advertisement
Method 2
Method 2 of 5:

Running Commands as Root Using Sudo

PDF download Download Article
  1. Sudo, which stands for "superuser do," is the best way to run commands as root in Linux. When you use sudo, you have root access for long enough to run the command, which, in most cases, is all you'll ever need. Instead of entering the root password, you'll enter your own password.
    • For example, type sudo vim /etc/apache2/apache2.conf to open the file apache2.conf as the root user, which allows you to make changes to the file even if your user account doesn't have write access to the file.
    • sudo is the preferred method for distributions like Ubuntu, where it will work even when the root account is locked.
    • You can only use sudo if your user account is in the /etc/sudoers configuration file. [4]
  2. 2
    Use sudo -i to start a root shell. If you want to run many commands as root without having to type your password multiple times, you can use this command to access a root shell that doesn't require enabling the root user, even in Ubuntu. [5]
  3. 3
    Use sudo -s to start a root shell using your environment. If you want to run a series of root commands without typing sudo before each, you can use this command to open an interactive root shell. The difference between the -s and -i options is that -s loads your own .bashrc file and keeps your current directory, whereas sudo -i simulates an actual root login.
  4. Advertisement
Method 3
Method 3 of 5:

Using the Terminal as Root

PDF download Download Article
  1. This will attempt to log you in as root. You can actually use the su command to log in as any user on the machine, but when you don't specify a username, it will attempt to log in as root.
  2. You now have a root shell.
    • If you get an "authentication error" message, your root account is likely locked. You will need to unlock the root account .
  3. When you are logged in as root, the prompt will end with # instead of $ if you're using bash, korn, or bourne shell, or % if you're using csh, tcsh, or zsh.
    • Once you are logged in as root, you can enter commands without having to re-enter the root password.
  4. Advertisement
Method 4
Method 4 of 5:

PDF download Download Article
  1. If you want to log in to your Ubuntu Linux system as root instead of as your user account on the initial login screen, use this method to enable root logins. When you type this command, this will open the "custom.conf" file for editing. [6]
    • When prompted, enter your user password to continue.
  2. Go to the bottom of the file. Add "AllowRoot=True" below "TimedLoginDelay = 10." [7] Add a few spaces so that the "A" in "AllowRoot" goes right below the "T" in "TimedLoginDelay".
  3. This displays a prompt that asks if you want to save the file.
  4. This saves the "Custom.conf" file. You should return to the normal Terminal prompt.
  5. This is the command to edit the password authentication manager file. [8]
    • Enter your user password when prompted.
  6. " To do this, type a hash symbol (#) before the line: # auth required pam_succeed_If .
  7. This saves the "custom.conf" file. You should return to the normal Terminal prompt. [9]
  8. When signing in to Ubuntu at the login screen, select Not Listed from the menu and type root as the username. You'll then use the root password you set earlier to log in as root.
    • If you want to remove the ability to log in to Ubuntu as root, delete the line you added to /etc/gdm3/custom.conf and uncomment the line you commented in etc/pam.d/gdm-password.
  9. Advertisement
Method 5
Method 5 of 5:

Recovering a Lost Root Password

PDF download Download Article
  1. If you've forgotten the root password and your user password, you'll need to boot into recovery mode in order to change them. If you know your user password and need to change the root password , just type sudo passwd root , enter your user password, then create a new root password.
  2. This will open the GRUB menu.
    • The timing on this can be tricky, so you may have to try multiple times.
  3. This will load recovery mode for your current distribution.
  4. This will start the terminal with you logged in as the root account.
  5. When you boot into recovery mode, you will typically only have read permissions. Enter the following command to enable write access: [10]
    • mount -rw -o remount /
  6. Once you're logged in as root and have changed the access permissions, you can create a new password for any account: [11]
    • Type passwd accountName and press Enter . If you need to change the root password, type passwd root .
    • Enter the new password twice when prompted.
  7. Once you're finished resetting passwords, you can reboot and use your computer as normal. Your new passwords will take effect immediately. [12]
  8. Advertisement

Community Q&A

Search
Add New Question
  • Question
    How can I make a user a superuser?
    Community Answer
    Type in "sudo usermod -aG sudo " replacing the username of the user you want to grant superuser access.
  • Question
    How do we log in as a root in Method 3? I have my admin account and my guest account. Do I have to log as a root in terminal first?
    Community Answer
    Yes. The root account must be unlocked first if you're using Ubuntu. Follow the instructions in method two. If you're not using Ubuntu, check your distribution's documentation.
  • Question
    What is the tilde used for?
    Community Answer
    It designates a home folder (typically aliased to the current user's home folder).
See more answers
Ask a Question
      Advertisement

      Tips

      Tips from our Readers

      The advice in this section is based on the lived experiences of wikiHow readers like you. If you have a helpful tip you’d like to share on wikiHow, please submit it in the field below.
      • Make sure to lock the root account every time you've committed the changes you need.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!

      Warnings

      • Only use the root account when you have to, and log out as soon as you are done.
      • Only share your root password with people who are A) trusted, and B) need to know it.
      • Running programs as root gives the program full admin access to your system. It's highly recommended that you use sudo or su to run programs instead of logging in as root.
      Advertisement

      About This Article

      Article Summary X

      To become root in Linux by gaining access in the terminal, type “su -” and press “Enter” to log in as a “super user.” When you’re prompted, enter the root password and the commands that require root access. If you don’t know the root password and only want temporary root access, then use “sudo” instead of “su-” and enter your own password when prompted. In case of an emergency, like having to deal with disk failure or restoring a locked account, log in as root by entering “root” as the user name followed by the password. When logged in as root, avoid running complex programs, which may have a negative effect on your system when it has root access. To learn how to reset the root or admin password, keep reading!

      Did this summary help you?
      Thanks to all authors for creating a page that has been read 1,604,587 times.

      Reader Success Stories

      • Anonymous

        Jan 31, 2018

        "I'm actually in the eleventh hour of doing things right now, and your article is just what I wanted to refer ..." more
        Rated this article:
      Share your story

      Is this article up to date?

      Advertisement