The USER env variable is unchanged after gaining root access.

[user1@comp]$ echo $USER

Logged in the system as 'user1'. Now check the output after gaining root access.

[user1@comp]$ su

[root@comp]# echo $USER

If I were to login as 'root' user, then USER env is set to 'root'. There is no issue there. This behavior was observed in Fedora and CentOS. Why is the env variable not updated?

  • what OS are you on? the $USER env variable often doesnt get updated when you switch to root, you can try whoami or some other alternative like , I have read who am i works for some people too (but not for me on Ubuntu) – the_velour_fog Jan 28 '16 at 6:02
  • @the_velour_fog. Thanks that didn't click my mind – 0aslam0 Jan 28 '16 at 6:18

Use su - instead, to ensure you inherit the root env.

  • Ah. i just needed to do man su to understand my mistake. Thanks – 0aslam0 Jan 28 '16 at 6:45

Use sudo -E su command which is set them in /etc/environment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.