Site icon LinuxAndUbuntu

[Fixed] How to Fix “bash: sudo: command not found” Error

bash sudo command not found

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 command that allows users to run commands with root access. It is the most helpful command 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

Fedora or its derivatives

$ dnf install sudo

For RHEL or its derivatives

$ yum install sudo

Arch Linux or its derivatives

$ pacman install sudo

For Docker images

It may not be possible if there is no root account. When you build a docker container from a docker image, the image may or may not have a root user. So we will 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. To create a Docker container you need to define everything in a Dockerfile using a specific syntax. Next you can use the “docker build” command to generate an image from that Dockerfile. For example if you want to use a Docker image that is hosted on Docker Hub you can specify it as the base image in your Dockerfile and then add the necessary commands to install additional tools such as sudo.

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="name@company.com"
 
#
# 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.

Exit mobile version