Updated: 22 October 2026 · Applies to: sudo 1.9.15p5 (Ubuntu 24.04), 1.9.16p2 (Debian 13), 1.9.17p2 (AlmaLinux 10.2) and sudo-rs 0.2.13 (Ubuntu 26.04), all tested

To run a shell script with sudo, give sudo the path to the script, not only its name: sudo ./backup.sh from the script's folder, or sudo /home/alice/bin/backup.sh from anywhere. A bare name such as sudo backup.sh fails with command not found even when the script runs fine without sudo, because sudo replaces your PATH with the secure_path from /etc/sudoers. If the script is not executable, run it through the shell: sudo bash ./backup.sh.

sudo basics: run a command as root

  • sudo COMMAND runs one command as root, for example sudo apt update. According to the sudoers manual, sudo asks for your own password, not root's.
  • sudo -u deploy COMMAND runs the command as another user (here deploy).
  • sudo -i opens a root login shell; sudo -s opens a root shell without the login files. Type exit to leave.
  • sudo !! repeats your previous command with sudo (a Bash history feature, interactive shells only).
  • sudo -l lists what you are allowed to run.

If sudo refuses you completely, your user has no sudo rights yet: see User Is Not in the sudoers File: Fix It on Debian and Ubuntu or Add a User to wheel for sudo on AlmaLinux, Rocky Linux, RHEL. On Ubuntu 26.04 (sudo-rs) the refusal reads sudo: I'm sorry deploy. I'm afraid I can't do that.

Run a shell script with sudo, step by step

  1. Start the script with a shebang line such as #!/bin/bash and make it executable: chmod +x backup.sh.
  2. Run it with a path: sudo ./backup.sh (current folder) or sudo /full/path/backup.sh.
  3. If the script is in a folder on your own PATH (such as ~/bin), let your shell find the full path for sudo: sudo "$(command -v backup.sh)".
  4. Pass arguments as usual: sudo ./backup.sh --full.

Lab example from Ubuntu 26.04 with a test script that prints the user and the PATH it gets (~/bin is on alice's PATH):

portcheck.sh

Output:

Running as: alice
sudo portcheck.sh

Output:

sudo: 'portcheck.sh': command not found
sudo ~/bin/portcheck.sh

Output:

Running as: root
PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

Classic sudo (Ubuntu 24.04, Debian 13, AlmaLinux 10) prints the same error without quotes: sudo: portcheck.sh: command not found.

Why sudo says command not found: secure_path

sudo does not use your PATH. It searches only the folders in secure_path, so your own folders (~/bin, ~/.local/bin, a project folder) are never searched. The defaults we found:

  • Ubuntu 24.04 and 26.04: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
  • Debian 13: the same without /snap/bin
  • AlmaLinux 10: /sbin:/bin:/usr/sbin:/usr/bin (no /usr/local/bin or /usr/local/sbin)

Check yours with sudo grep secure_path /etc/sudoers. Classic sudo also shows it in sudo -l.

Workarounds that work, and ones that do not

  • Install the script where sudo looks (best for scripts you use often): sudo install -m 755 -o root -g root portcheck.sh /usr/local/sbin/portcheck. On Ubuntu and Debian, sudo portcheck then works. On AlmaLinux 10 it still gave sudo: portcheck: command not found, because /usr/local is not in its secure_path: call it as sudo /usr/local/sbin/portcheck.
  • sudo env "PATH=$PATH" portcheck.sh works on all four systems, but it runs root with your PATH. Use it for a one-off test only.
  • sudo -E and sudo --preserve-env=PATH do not help: in our tests both still ended in command not found. sudo-rs on Ubuntu 26.04 also prints sudo: preserving the entire environment is not supported, '-E' is ignored.
  • Do not add your home folder to secure_path. Anyone who can write to a folder in secure_path can place a script there and get it run as root. If you change sudoers at all, use visudo (see how to fix a broken sudoers file).

Other errors when running a script with sudo

  • Script not executable: classic sudo says sudo: ./bin/portcheck.sh: command not found; sudo-rs says sudo: cannot execute '/home/alice/bin/portcheck.sh': Permission denied (os error 13). Run chmod +x, or sudo bash ./portcheck.sh.
  • sudo sh script.sh gives a syntax error: on Debian and Ubuntu /bin/sh is dash, so Bash features fail, for example Syntax error: "(" unexpected for an array. Use sudo bash script.sh. On AlmaLinux /bin/sh is Bash.
  • sudo -u deploy ~/bin/script.sh fails: the other user cannot enter your home folder (mode 750 on Ubuntu, 700 on Debian 13 and AlmaLinux 10). Classic sudo says unable to execute ... Permission denied, sudo-rs says command not found. Put scripts that several users run in /usr/local/sbin or /opt.

Let a user run one script as root

  1. Install the script owned by root, so the user cannot change it: sudo install -m 755 -o root -g root backup.sh /usr/local/sbin/backup.sh. If the user can edit the script, the rule below gives them full root access.
  2. Create a rule with sudo visudo -f /etc/sudoers.d/deploy-backup:
    deploy ALL=(root) NOPASSWD: /usr/local/sbin/backup.sh
  3. Test it as the user (lab output from Ubuntu 26.04, host name replaced with vps):
    sudo -l

    Output:

    User deploy may run the following commands on vps:
        (root) NOPASSWD: /usr/local/sbin/backup.sh
    sudo /usr/local/sbin/backup.sh --full

    Output:

    backup.sh running as root with args: --full

Always call the script by its full path. On AlmaLinux 10, sudo backup.sh did not match the rule and asked for a password, because /usr/local/sbin is not in secure_path. According to the sudoers manual, a rule without arguments allows any arguments, and "" after the path allows none. More on passwordless rules: How to Run sudo Without a Password (sudoers NOPASSWD).

Make a script ask for sudo itself

Put this at the top of a script that needs root. It restarts the script through sudo with the same arguments:

#!/bin/bash
if [ "$(id -u)" -ne 0 ]; then
  exec sudo -- "$0" "$@"
fi
echo "Now running as $(id -un) with args: $*"

Run as alice from /usr/local/bin, it printed Now running as root with args: one two on all four test systems. If sudo keeps rejecting your password, see sudo 3 Incorrect Password Attempts: Fix and Set the Password.

Official documentation: sudoers manual and sudo manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket