1

I'm having trouble with my PATH. I have a clean instance of an Amazon Linux virtual server and I set this in /etc/profile

M3_HOME=/usr/local/maven
JAVA_HOME=/usr/java/latest
PATH="$PATH:$M3_HOME/bin:$JAVA_HOME/bin"

In my myuser (my own user) account, I set this in my ~/.bash_profile

PATH=$PATH:$HOME/bin

And then when I output the "env" as my user I get

PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/maven/bin:/usr/java/latest/bin:/home/myuser/bin

But when I output it as sudo I get

sudo env
PATH=/sbin:/bin:/usr/sbin:/usr/bin

How do I get my sudo user to use the same path as my normal user?

Dave
  • 2,548

1 Answers1

2

Because when you execute sudo you are promoted to root, a different user with a different environment.

You can maintain the same environment variables with --preserve-env option.

This was lifted straight from the man page for sudo and checked on my Linux box under bash. The following give the same output

env | grep PATH

sudo --preserve-env=PATH env | grep PATH 

Check your man page.

bu5hman
  • 4,756
  • I'm not clear on how to solve my problem given what you list here. Entering "su - " does not change the PATH var of sudo. – Dave Nov 03 '17 at 18:28
  • I misread the linked post but just rechecked the sudo man page. – bu5hman Nov 05 '17 at 14:39
  • I think I have been looking for this AGES. If I run sudo --preserve-env=PATH env the $PATH of the user inking sudo is preserved. Thanks! – muammar Mar 25 '21 at 15:31