How do you make a sudo preserve PATH?

How do you make a sudo preserve PATH?

3 Answers

  1. Use the full path: sudo ~/bin/my-command ; or.
  2. Add the directory containing the command to secure_path . Run sudo visudo and edit the secure path line: Defaults secure_path=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/youruser/bin/”

What is Secure_path in sudo?

The secure_path value, if set, will be used as PATH environment variable for the commands you run using sudo . That means, when you run e.g. sudo apt update , the system will try to look up the command apt in the directories specified in the secure_path , in the specified order.

How to keep environment variables when using sudo..?

-E, –preserve-env Indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the user does not have permission to preserve the environment. The trick is to add environment variables to sudoers file via sudo visudo command and add these lines:

Why does Sudo not preserve TMPDIR in glibc?

This doesn’t work neither, it actually doesn’t even make TMPDIR appear in the whitelist (that is, what “sudo sudo -V` prints as “Environment variables to preserve”. (I’m running Ubuntu 10.04.) It looks like glibc will remove certain environment variables when running setuid programs ( sudo is, of course, setuid).

How to keep environment variables when…Stack Overflow?

The security policy may return an error if the user does not have permission to preserve the environment. The trick is to add environment variables to sudoers file via sudo visudo command and add these lines: taken from ArchLinux wiki.

How to edit sudoers by Sudo visudo?

You need to edit your sudoers by sudo visudo as possibly you’ve security policy plugin enabled which overrides your PATH by secure_path option. So add the path to the list and you can also use env_keep instead, for example: alias sudo=’sudo env PATH=$PATH VAR1=SOME_VALUE VAR2=SOME_VALUE…’