Encountering the “sudo: command not found” error on Ubuntu is a common issue that can be quite frustrating for users. This error typically occurs when the sudo
package is not installed or the PATH variable does not include the directory where sudo
is located. It can disrupt workflow and hinder system management tasks, leaving users searching for a quick fix.
The error “sudo: source command not found” on Ubuntu occurs because the source
command is a shell built-in command and not a standalone executable. When you use sudo
, it runs the command in a new shell that doesn’t recognize built-in commands like source
.
Here are the technical reasons behind this error:
Shell Built-in Command: source
is a built-in command in bash and other shells. It reads and executes commands from a file in the current shell environment. Since it’s not an executable file, it can’t be found in the system’s PATH.
New Shell Environment: When you use sudo
, it creates a new shell environment to run the command with elevated privileges. This new shell doesn’t have the same built-in commands as your current shell.
PATH Variable: The new shell environment created by sudo
may not have the same PATH variable settings as your current shell, leading to the command not being found.
To work around this, you can use the .
(dot) command, which is equivalent to source
, but works within the current shell environment:
sudo bash -c '. /path/to/your/script.sh'
This command runs a new bash shell with elevated privileges and sources the script within that shell.
Here are the common causes of the “sudo: command not found” error on Ubuntu:
Missing sudo package:
sudo
package might not be installed on your system. You can install it using:apt-get install sudo
Incorrect PATH variable:
sudo
command might not be included in your PATH environment variable. You can check your PATH variable with:echo $PATH
/usr/bin
or the directory where sudo
is located is included in the PATH.User permissions issues:
sudo
. Ensure the user is part of the sudo
group:usermod -aG sudo username
Corrupted sudo installation:
sudo
package might be corrupted. Reinstalling sudo
can help:apt-get --reinstall install sudo
Shell issues:
sudo
command. Switching to a different shell or ensuring the current shell is properly configured can resolve this.These steps should help you troubleshoot and resolve the “sudo: command not found” error on Ubuntu.
Here’s a detailed, step-by-step guide to resolve the ‘sudo: command not found’ error on Ubuntu:
First, switch to the root user to gain the necessary permissions:
su -
Enter the root password when prompted.
sudo
PackageUpdate the package list and install the sudo
package:
apt update
apt install sudo
sudo
InstallationCheck if sudo
is installed correctly:
sudo -v
If you see no errors, sudo
is installed successfully.
sudo
GroupAdd your user to the sudo
group to grant sudo permissions:
usermod -aG sudo your_username
Replace your_username
with your actual username.
Ensure the sudo
command is in your PATH. Open the .bashrc
file in your home directory:
nano ~/.bashrc
Add the following line at the end of the file:
export PATH=$PATH:/usr/bin
Save and close the file (Ctrl + X, then Y, then Enter).
Reload the .bashrc
file to apply the changes:
source ~/.bashrc
Check if the PATH variable includes /usr/bin
:
echo $PATH
You should see /usr/bin
in the output.
sudo
CommandFinally, test the sudo
command to ensure everything is working:
sudo -v
These steps should resolve the ‘sudo: command not found’ error on Ubuntu.
To prevent the “sudo: command not found” error on Ubuntu in the future, consider these measures:
sudo apt update && sudo apt upgrade
to ensure all packages, including sudo
, are current.sudo
group with usermod -aG sudo username
./usr/bin
is included in your PATH variable by adding export PATH=$PATH:/usr/bin
to your .bashrc
or .profile
.sudo
is installed with sudo apt install sudo
.These steps should help maintain a smooth and error-free experience.
Follow these steps:
apt update
and install the sudo
package using apt install sudo
.sudo
is installed correctly by running sudo -v
. If you see no errors, it’s installed successfully.sudo
group with usermod -aG sudo your_username
, replacing ‘your_username’ with your actual username.export PATH=$PATH:/usr/bin
to your .bashrc
file and saving changes.source ~/.bashrc
./usr/bin
using echo $PATH
.sudo
command with sudo -v
.To prevent this error in the future, consider these measures:
sudo apt update && sudo apt upgrade
.sudo
group./usr/bin
is included in your PATH variable.sudo
package.By following these steps and preventive measures, you can maintain a smooth and error-free experience on Ubuntu.