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
For example, the absolute path to the ls command binary is /usr/bin/ls
.
$ ls -l /usr/bin/ls
The $PATH
variable makes it possible to execute the ls command as:
$ ls
instead of:
$ /usr/bin/ls
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
However, executing the command on its own even with sudo leads to an error:
$ onescript.sh or $ sudo onescript.sh
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
However, running the script as a Sudoer user still leads to the “command not found” error.
$ sudo onescript.sh
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
).
Save the file, and re-run our script with sudo:
$ sudo onescript.sh
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.