Home Linux Troubleshooting Fixing “Command Not Found” Error When Using Sudo

Fixing “Command Not Found” Error When Using Sudo

It is a right of passage for almost all Linux users to go through the headaches associated with the error “command not found” whenever you try to execute a script or a file on your Linux terminal as a sudoer user.

This error directly implies that the script or file you are trying to execute either does not exist on your system or the PATH variable is unaware of its unspecified location.

This article guide will walk us through diagnosing and fixing this “command not found” sudo-associated error.

Linux Environment Variables

For users with some software programming experience, a variable is nothing but a changeable placeholder of some value. For instance, when you use the term “my pc” to describe the computer you are using, the term “my pc” is being used as a generic placeholder/variable since it can either be a Lenovo, Hp, Mac, or Raspberry Pi at any given time.

The Linux environment variables are unique variables associated with a system user login session info. The configuration of these variables; in most cases, is by default and occurs during user creation and/or package installation.

They are helpful because scripts, applications, and the system shell need them to effectively execute running commands. These variables can either be global (system-defined) or local (user-defined).

The Linux $PATH Variable

When executing a command on a Linux operating system terminal environment, the Linux system tends to search through a colon-separated directories list specified by the $PATH variable.

For instance, on a freshly installed Linux system, the $PATH variable is assigned the default value /usr/bin:/usr/local/bin. This instance tells the Linux system to query the two binary file locations (/usr/bin and /usr/local/bin) before executing any command.

$ echo $PATH 
List Linux Environment Variables
List Linux Environment Variables

For example, the absolute path to the ls command binary is /usr/bin/ls.

$ ls -l /usr/bin/ls
Check Path of ls Command
Check Path of ls Command

The $PATH variable makes it possible to execute the ls command as:

$ ls

instead of:

$ /usr/bin/ls
ls Command Example
ls Command Example

To add additional directories to $PATH variable, we can reference the following command:

$ PATH=”$PATH:/path/to/this/directory:/path/to/that/directory”.

Working with Script Files in Linux

Let us for example assume the existence of the following script file.

$ nano onescript.sh

The contents of a script file.

#!/bin/bash

echo "Welcome to LinuxShellTips!"

Let us save the file and make it executable:

$ chmod u+x onescript.sh 

When we execute the above script while pointing to its location, we should get a response:

$ ./onescript.sh
Run Script in Linux
Run Script in Linux

However, executing the command on its own even with sudo leads to an error:

$ onescript.sh
or
$ sudo onescript.sh
Command Not Found Error
Command Not Found Error

Fixing “Command not Found” Error in Linux

If we point the $PATH variable to the above script’s pwd (parent working directory), we should be able to easily execute it without running into the “command not found” error.

$ PATH="$PATH:$HOME"
$ onescript.sh
$ which onescript.sh
Set PATH Variable to Script
Set PATH Variable to Script

However, running the script as a Sudoer user still leads to the “command not found” error.

$ sudo onescript.sh 
Sudo Command Not Found Error
Sudo Command Not Found Error

To permanently fix the “command not found” error, we need to manually modify the /etc/sudoer file.

Firstly, let us move our script file to the $HOME/bin directory:

$ mkdir -p $HOME/bin
$ mv onescript.sh $HOME/bin

Let us now modify the /etc/sudoer file.

$ sudo nano /etc/sudoer 

Identify the line starting with Defaults secure_path and add the path to your script location (in my case:$HOME/bin which translates to /home/dnyce/bin).

Add PATH Variable to Sudoer
Add PATH Variable to Sudoer

Save the file, and re-run our script with sudo:

$ sudo onescript.sh 
Run Script Using Sudo in Linux
Run Script Using Sudo in Linux

We have finally solved the mystery behind the “command not found” error when using Sudo.

By manually editing the variable secure_path on /etc/sudoers file, and pointing it to the bin location of your binary file, you can globally access your scripts and apps execution files without using their absolute path.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.