The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 864β€…    Votes:  4β€…    βœ… Solution
Tags: command-line   sudo   paths  
Link: πŸ” See Original Question on Ask Ubuntu πŸ”—

URL: https://askubuntu.com/q/1012922
Title: Can I make `sudo` follow my path via CLI?
ID: /2018/03/08/Can-I-make-_sudo_-follow-my-path-via-CLI_
Created: March 8, 2018
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false


You can edit sudoers file and set secure_path to match your path. This is documented in Unix & Linux.

Is there a command line switch I can pass to sudo such that it uses the path I have set? For example:

rick@alien:~$ sudo echo $PATH
/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ echo $PATH
/home/rick/bin:/home/rick/.local/bin:/mnt/e/bin:/mnt/e/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ sudo which auto-brightness-config
/usr/local/bin/auto-brightness-config
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ which auto-brightness-config
/mnt/e/usr/local/bin/auto-brightness-config
───────────────────────────────────────────────────────────────────────────────
rick@alien:~$ sudo locate auto-brightness-config
/Desktop/Link to auto-brightness-config
/home/rick/Pictures/display-auto-brightness-config 1.png
/mnt/e/Desktop/Link to auto-brightness-config
/mnt/e/usr/local/bin/.auto-brightness-config
/mnt/e/usr/local/bin/Link to auto-brightness-config
/mnt/e/usr/local/bin/auto-brightness-config
/usr/local/bin/.auto-brightness-config
/usr/local/bin/Link to auto-brightness-config
/usr/local/bin/auto-brightness-config

When I run using sudo it is finding the wrong copy of the script in /usr/local/bin when I really want the version in /mnt/e/usr/local/bin .

If I don’t want to maintain the path in sudoers file, is there a switch I can pass to sudo to use my path to find the right command?

⇧ Systemd logs (`journalctl`) are too large and slow How do I get the parent process ID of a given child process?  β‡©