[Fixed] bash: sudo: command not found

sudo, Linux’s most useful command, is occasionally missing from several Linux distributions, most notably docker containers and lightweight Linux distributions.

sudo is a utility that allows users to run commands with root access. It is the most helpful command and is included in almost all major Linux distributions. Yes, almost all. Several Linux distros, particularly docker images, do not ship the sudo package by default. If you see the error bash: sudo: command not found it simply means that sudo is not installed.

bash: sudo: command not found

In most circumstances, starting your package manager and installing sudo will be enough. However, installing a new package necessitates the use of root capabilities. Log in as the root user and install sudo.

For Ubuntu or its derivatives

$ apt install sudo

For Fedora or its derivatives

$ dnf install sudo

For RHEL or its derivatives

$ yum install sudo

For Arch Linux or its derivatives

$ pacman install sudo

For Docker images

However, if there is no root account, like in the case of numerous docker images, this may not be possible. When you create a docker container from a docker image, the image may or may not have a root user. In that case, we’ll have to create a custom image with sudo pre-installed. Don’t worry, it is easy.

Create custom Docker image

A Dockerfile is required to construct a custom docker image. Dockerfile is used to define everything in a certain syntax, and the docker build command is used to generate an image from it. For example, if you wish to create a Docker container using a Docker image hosted on Docker Hub, we may specify in the Dockerfile that image as the base image and pass in the commands to install additional tools, i.e. sudo in our case.

Create Dockerfile

touch Dockerfile

Open the Dockerfile in your favorite text editor.

nano Dockerfile or gedit Dockerfile
# This is the base image we will use to create our custom docker image
FROM ubuntu:latest
# the maintainer of an image
LABEL maintainer="[email protected]"
# Install the tools (sudo)
RUN apt-get update && apt-get upgrade -y && apt install sudo tasksel -y

We used the RUN label in the third part, which is marked with “Install the tools”, and any command after RUN will be executed before generating our custom image. So, by typing a command here, we can install any package we wish for. If we keep writing commands, we can even spin up a full server.

For more knowledge on Dockerfile, refer to the docker’s official documentation.

Save and close the Dockerfile. Now, open the command line and navigate to the directory containing the Dockerfile and run the following command –

sudo docker build -t image-name:tag .

Remember that the docker build command only accepts the directory, not the Dockerfile. If you specify in the command the Dockerfile, you’ll get the error – unable to prepare context: context must be a directory: /home/sandy/Dockerfile. So you must be inside the directory containing Dockerfile and use . to specify the current directory or the full path to the Directory containing Dockerfile.

And that’s it. docker build will start building the command. It will take a few seconds. Once done, our custom docker image is saved in our local image cache. We can now use the image-name:tag to create a docker container that has sudo pre-installed.


Mohd Sohail is a web developer and a Linux sysAdmin. He also loves to write how-to articles, applications reviews and loves to use new Linux distributions.

Articles: 853

Leave a Reply

Your email address will not be published. Required fields are marked *