• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

How To Use sudo su

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Published: 1/31/2024

About Terminus

In Unix-like operating systems, the su command is used to temporarily log into another user account and execute commands using its privileges. Like the sudo command, it is generally used to execute commands as the superuser (also known as the "root" user). The difference in su vs. sudo resides in the fact that the su command gives access to an interactive shell session, whereas the sudo command only allows to execute one command at a time.

In this article, we’ll explain why these two commands are often combined together in order to access a root shell, and how to run sudo commands without having to type in the user password.

Spawning a root shell with the su command

When called with no user specified, the su command will attempt to run an interactive shell as root, prompting you to enter the root password.

Since the root account is disabled by default on most Linux distributions —which means that the root password is not set, in order to prevent anyone from directly logging into it—using the su command alone will certainly result in an authentication error with a message like su: Authentication failure:

Thumbnail for

Logging in as the root user

To go around this restriction and gain access to a root shell, a user registered on the sudoers list can prepend the su command with sudo, and enter their own password instead of the root password. Note that since the default behavior of su is to connect to the root account, executing either sudo su or sudo su root will both have the same effect.

Thumbnail for

Once you’ve entered your password, you can confirm that you are logged in as the root user by using the whoami command that prints the effective user name of the current session.

Thumbnail for

From here, you can execute any command that usually requires elevated privileges without having to prefix it with sudo.

How to exit sudo su

To terminate the current shell session and come back to the user account you were previously logged in as, you can run the exit command.

Use sudo su - to run a login shell

By default, the su command will preserve the environment variables and the current working directory of the previous user.

To start the shell as a login shell with an environment similar to a real login, you can use the - option:

 $ sudo su -

Run in Warp

Which will:

  • Clear all the environment variables except for TERM.
  • Initializes the environment variables HOME, SHELL, USER, LOGNAME and PATH.
  • Change the current directory to the user’s home directory.

Don’t confuse sudo -su with sudo su -!

Note that the sudo -su command differs from sudo su - in the sense that the su expression will be treated as option flags of the sudo command, where the -s flag is used to run a new shell, and the -u flag is used to run a command as a user different from root.

Running sudo su without a password

By default, a command run with sudo requires that the user authenticates themselves using their own password. In some cases, it may be useful to disable this mechanism. For example, when there is only one user account registered on the system, or when an automated script requires elevated privileges to perform certain tasks.

To do so, you can edit the sudoers file located at /etc/sudoers using the visudo command:

 $ sudo visudo

Run in Warp

And prepend the NOPASSWD directive separated by a single colon (:) to the last argument of the desired user privileges line:

 user ALL=(ALL:ALL) NOPASSWD:ALL

Run in Warp

Troubleshooting sudo su not working

If the sudo su command doesn’t work, the issue is usually caused by two things.

First, you need to make sure that the user account you are using is part of the sudoers list, which can be verified by displaying the content of the /etc/group file.

Thumbnail for

Second, you need to make sure that the su command is part of the allowed commands your root account can run, which can be verified by displaying the content of  the /etc/sudoers file.

Thumbnail for

Written by

Thumbnail for Razvan LudosanuRazvan Ludosanu

Razvan Ludosanu

Founder, learnbackend.dev

Filed Under

Related Articles

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash
Thumbnail for Gabriel ManricksGabriel Manricks

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Bash

POST JSON Data With Curl

How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.

BashUnixLinux
Thumbnail for Neeran GulNeeran Gul

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuRazvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuRazvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Thumbnail for Neeran GulNeeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraBrett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuRazvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzZev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniAmit Jotwani

Bash Aliases

Create an alias for common commands

BashLinuxUnix
Thumbnail for Brett TerpstraBrett Terpstra

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for null