DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Fixing “sudo: command not found” on Linux

Seeing sudo: command not found doesn’t necessarily mean you need to install anything. You might already be root, your shell might not be able to find the sudo program, or you may be working in a minimal container that doesn’t include it.

Start by checking what account you’re using and how your shell resolves the command:

whoami
id -u
ls -l /usr/bin/sudo
echo "$PATH"
type sudo
Enter fullscreen mode Exit fullscreen mode

If id -u prints 0, you’re root. If /usr/bin/sudo exists but type sudo can’t find it, look at PATH. If the binary is missing, you’ll need root access to install it.

First, separate the similar-looking errors

These messages point to different problems:

  • sudo: command not found: the shell can’t locate sudo.
  • user is not in the sudoers file: sudo runs, but your account isn’t authorized to use it.
  • sudo: apt: command not found: sudo ran, but the command after it wasn’t found. Your system may use another package manager, or that particular tool may be missing.

Don’t try to fix a permissions error by reinstalling sudo. First identify which command is actually missing.

If you’re already root, leave out sudo

sudo lets an authorized non-root user run a command with elevated privileges. A root shell already has those privileges, so sudo may be unnecessary—and may not be installed, especially in a minimal environment.

Check with:

whoami
Enter fullscreen mode Exit fullscreen mode

If it prints root, run the command directly. For example, use apt update rather than sudo apt update.

If sudo is missing, install it as root

You can’t use a missing sudo command to install itself. Switch to a root shell or use another authorized route to get root access, then use the package manager for your distribution.

On Debian, you can become root with su -, then install sudo and add your account to the sudo group:

su -
apt update
apt install -y sudo
usermod -aG sudo yourusername
Enter fullscreen mode Exit fullscreen mode

Replace yourusername with the account you want to authorize. Log out and back in for the new group membership to take effect.

Other distributions use different package managers and may use different groups. For example, the source’s examples include pacman -S sudo on Arch, dnf install -y sudo on Fedora-family systems, and apk add sudo on Alpine. Some systems require additional sudoers configuration before a user can run sudo.

To confirm which distribution you’re on, check:

cat /etc/os-release
Enter fullscreen mode Exit fullscreen mode

If sudo is installed but your account gets a sudoers error, that’s an authorization issue rather than a missing-command issue. Follow your distribution’s configuration method; adding a user to sudoers or the appropriate group is a separate step from installing the package.

If the file exists, check PATH and shell overrides

Your shell searches the directories in PATH when you type a command. If /usr/bin/sudo exists but sudo alone isn’t found, check whether /usr/bin appears in the output of:

echo "$PATH"
Enter fullscreen mode Exit fullscreen mode

You can test the full path directly:

/usr/bin/sudo whoami
Enter fullscreen mode Exit fullscreen mode

If that works, inspect your shell startup files for a line that replaces PATH instead of adding to it. This drops the existing system directories:

export PATH="$HOME/bin"
Enter fullscreen mode Exit fullscreen mode

This preserves the current value while adding your own directory:

export PATH="$HOME/bin:$PATH"
Enter fullscreen mode Exit fullscreen mode

After correcting the relevant file, start a new shell or reload that file. Avoid blindly changing PATH system-wide: restore the directories appropriate for your environment.

Also check whether an alias or function is taking precedence over the executable:

type sudo
grep -n 'sudo' ~/.bashrc ~/.zshrc ~/.profile 2>/dev/null
Enter fullscreen mode Exit fullscreen mode

If type reports an unintended alias or function, remove it from the relevant configuration and open a fresh shell.

Minimal containers and new server accounts

A minimal container or cloud image may omit sudo to keep the environment small. Some also start you as root, in which case using sudo adds nothing. Check your identity before changing the image or installing packages.

If you’re setting up an account that needs administrative access, remember that creating the account, installing sudo, and granting sudo permission are distinct tasks. The steps for creating a Linux user can help clarify the account-creation part.

A quick troubleshooting order

When the error appears, work through these checks in order:

  1. Run whoami or id -u; if you’re root, omit sudo.
  2. Check whether /usr/bin/sudo exists.
  3. If it exists, inspect PATH and run type sudo.
  4. If it’s absent, identify the distribution and install it from a root shell.
  5. If the message mentions sudoers, configure authorization rather than reinstalling the command.

That sequence helps avoid the most common mistake: treating every sudo-related error as an installation problem.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)