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.
Steps
-
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] X Research source
- 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 .
-
Type sudo passwd root and press ↵ Enter . When prompted for a password, enter your user password. [2] X Research sourceAdvertisement
-
Set a root user password. 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] X Research source
- Now that the root account is active, jump down to Using the Terminal as Root to learn how to become root.
-
Lock the root account again (optional). If you want to lock the root account, enter the following command to remove the password and lock root:
- sudo passwd -dl root
-
Preface a command with sudo to run it as root. 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] X Research source
-
2Use 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] X Research source
-
3Use 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.
-
Type su - and press ↵ Enter . 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.
-
Type the root password and press ↵ Enter . 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 .
-
Run commands as root. 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.
-
In a terminal, type sudo nano /etc/gdm3/custom.conf and press ↵ Enter . 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] X Research source
- When prompted, enter your user password to continue.
-
Add AllowRoot=True to the file. Go to the bottom of the file. Add "AllowRoot=True" below "TimedLoginDelay = 10." [7] X Research source Add a few spaces so that the "A" in "AllowRoot" goes right below the "T" in "TimedLoginDelay".
-
Press Ctrl + X . This displays a prompt that asks if you want to save the file.
-
Press Y followed by ↵ Enter . This saves the "Custom.conf" file. You should return to the normal Terminal prompt.
-
Type sudo nano /etc/pam.d/gdm-password and press ↵ Enter . This is the command to edit the password authentication manager file. [8] X Research source
- Enter your user password when prompted.
-
Comment out the line "auth required pam_succeed_If. " To do this, type a hash symbol (#) before the line: # auth required pam_succeed_If .
-
Press Ctrl + X and then keypress + Y to confirm. This saves the "custom.conf" file. You should return to the normal Terminal prompt. [9] X Research source
-
Log in as root on the login screen. 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.
-
Reset the root password if it has been forgotten. 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.
-
Reboot your computer and hold left- ⇧ Shift after the BIOS screen. This will open the GRUB menu.
- The timing on this can be tricky, so you may have to try multiple times.
-
Select the first (recovery mode) entry on the list. This will load recovery mode for your current distribution.
-
Select the root option from the menu that appears. This will start the terminal with you logged in as the root account.
-
Remount the drive with write permissions. When you boot into recovery mode, you will typically only have read permissions. Enter the following command to enable write access: [10] X Research source
- mount -rw -o remount /
-
Create a new password for any accounts you're locked out of. Once you're logged in as root and have changed the access permissions, you can create a new password for any account: [11] X Research source
- Type passwd accountName and press ↵ Enter . If you need to change the root password, type passwd root .
- Enter the new password twice when prompted.
-
Reboot your computer after resetting passwords. Once you're finished resetting passwords, you can reboot and use your computer as normal. Your new passwords will take effect immediately. [12] X Research source
Community Q&A
-
QuestionHow can I make a user a superuser?Community AnswerType in "sudo usermod -aG sudo " replacing the username of the user you want to grant superuser access.
-
QuestionHow 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 AnswerYes. 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.
-
QuestionWhat is the tilde used for?Community AnswerIt designates a home folder (typically aliased to the current user's home folder).
Tips
Tips from our Readers
- Make sure to lock the root account every time you've committed the changes you need.
Warnings
- Only use the root account when you have to, and log out as soon as you are done.Thanks
- Only share your root password with people who are A) trusted, and B) need to know it.Thanks
- 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.Thanks
References
- ↑ https://ubuntu.com/tutorials/command-line-for-beginners#3-opening-a-terminal
- ↑ https://linuxhint.com/change-root-password-ubuntu-linux/
- ↑ https://help.ubuntu.com/community/RootSudo
- ↑ https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/managing-sudo-access_configuring-basic-system-settings
- ↑ https://www.sudo.ws/docs/man/1.8.15/sudo.man/
- ↑ https://linuxconfig.org/how-to-allow-gui-root-login-on-ubuntu-20-04-focal-fossa-linux
- ↑ https://linuxconfig.org/how-to-allow-gui-root-login-on-ubuntu-20-04-focal-fossa-linux
- ↑ https://linuxconfig.org/how-to-allow-gui-root-login-on-ubuntu-20-04-focal-fossa-linux
- ↑ https://www.youtube.com/watch?v=FgbkbwIEj1E
About This Article
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!
Reader Success Stories
- "I'm actually in the eleventh hour of doing things right now, and your article is just what I wanted to refer to. This will be quite useful for beginners. Thanks a lot to the person who created this." ..." more