Linux users will inevitably need to log into the root account, or use administrator privileges, quite frequently. Tasks like installing or removing software, configuring system settings, adjusting file permissions, and many others usually require access to the root user account in order to perform. The root account can be accessed either by logging directly into the account, or by using the
sudo Linux command to temporarily access root and execute a command with administrator privileges. In this tutorial, we will show you how to change to the root account on a Linux system, and how to use
sudo to execute commands with root privileges.
In this tutorial you will learn:
- How to log in to the root account
- How to use the
- How to open a new terminal as the root user
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to Switch to Root User on Linux
Here are a few different methods to switch to the root user on Linux. Depending on your Linux distribution and the way your user is configured (i.e., whether they have
sudoaccess or not), you may find that one method works better than another, so try them all.
- The majority of Linux distributions come with the
sudoconvention configured by default for the user that is set up during installation. If your user has access to the
sudocommand, then you can switch to the root account by appending the
-ioption, like so:
$ sudo -i
After providing your sudo password, you will be logged into the root account and no longer required to continue prefacing your commands with
sudoin order to run them with root privileges.
- Alternatively, there is no reason to log into the root user account if you have access to the
sudocommand and simply want to run a few commands as the root user. This is actually the recommended approach, as it is more secure than having users to log into the root account directly. For example, to run a command as root, preface it with
$ sudo whoami root
Although we are running this command as an ordinary user, the whoami command shows us the effective user that is running the command, which is root.
DID YOU KNOW?
See our tutorial on giving a user sudo permissions if your account has not yet been configured to use the
- If your user does not have
sudopermissions, you should be able to log in to the root user account via the
sucommand. In this case, you will need to know the password of the root user. The majority of times, you will want to append the
-option, in order to update your local environment variables.
$ su -
The above command will only work if the root account has a password configured. On some systems, the root account does not have a password by default, and instead is expected to be accessed strictly via
sudoas shown in the above methods.
If your root account does not have a password and you need to set one, you can execute the following command. Note that this is going against security recommendations.
$ sudo passwd
After setting the root password, you will be able to log in with the
- Another option is to open a new terminal as the root user. The command for this will vary depending on your installed desktop environment, but if you want to open a brand new terminal as the root user on the GNOME desktop environment, you can execute the following command.
$ sudo gnome-terminal
This same method can also be used to launch other GUI applications as the root account, although many of them will not like being accessed by root, and may produce an error.
In this tutorial, we saw how to switch to the root user account on a Linux system. The recommended approach is to use the
sudocommand in order to execute commands with root privileges, but we can also log directly into the root user account, either with the
su -commands, depending on how our system and user account has been configured.