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 COMMANDruns one command as root, for examplesudo apt update. According to the sudoers manual, sudo asks for your own password, not root's.sudo -u deploy COMMANDruns the command as another user (heredeploy).sudo -iopens a root login shell;sudo -sopens a root shell without the login files. Typeexitto leave.sudo !!repeats your previous command with sudo (a Bash history feature, interactive shells only).sudo -llists 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
- Start the script with a shebang line such as
#!/bin/bashand make it executable:chmod +x backup.sh. - Run it with a path:
sudo ./backup.sh(current folder) orsudo /full/path/backup.sh. - 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)". - 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/binor/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 portcheckthen works. On AlmaLinux 10 it still gavesudo: portcheck: command not found, because/usr/localis not in itssecure_path: call it assudo /usr/local/sbin/portcheck. sudo env "PATH=$PATH" portcheck.shworks on all four systems, but it runs root with yourPATH. Use it for a one-off test only.sudo -Eandsudo --preserve-env=PATHdo not help: in our tests both still ended incommand not found. sudo-rs on Ubuntu 26.04 also printssudo: 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 insecure_pathcan place a script there and get it run as root. If you change sudoers at all, usevisudo(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 sayssudo: cannot execute '/home/alice/bin/portcheck.sh': Permission denied (os error 13). Runchmod +x, orsudo bash ./portcheck.sh. sudo sh script.shgives a syntax error: on Debian and Ubuntu/bin/shis dash, so Bash features fail, for exampleSyntax error: "(" unexpectedfor an array. Usesudo bash script.sh. On AlmaLinux/bin/shis Bash.sudo -u deploy ~/bin/script.shfails: the other user cannot enter your home folder (mode 750 on Ubuntu, 700 on Debian 13 and AlmaLinux 10). Classic sudo saysunable to execute ... Permission denied, sudo-rs sayscommand not found. Put scripts that several users run in/usr/local/sbinor/opt.
Let a user run one script as root
- 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. - Create a rule with
sudo visudo -f /etc/sudoers.d/deploy-backup:deploy ALL=(root) NOPASSWD: /usr/local/sbin/backup.sh
- 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.shsudo /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.