using 'exec sudo' command env variable $USER not getting updated

General support questions
Post Reply
shagun
Posts: 43
Joined: 2016/11/04 12:30:30

using 'exec sudo' command env variable $USER not getting updated

Post by shagun » 2018/03/23 07:11:43

Hi All,

We are facing an issue with exec sudo command which is resulting in an infinite loop.
We have one script in which we are checking if $USER is not equal to root then execute the same script with exec sudo so that it will run with root.
Code snippet:
script1:

if [[ "$USER" != "root" ]]; then
exec sudo $0 $@
fi
If we run the script with some another user say root1 using below
"sudo -u root1 script1" script1 will run in an infinite loop.
In CentOS 7.4 when exec sudo command runs, the value of $USER remains same i.e root1 and again it enters and if block and execute the scipt1 and so on ..
While in case of Centos 7.3 after running exec sudo command value of $USER gets updated to root and it works fine.

Centos 7.4 sudo Version :
Sudo version 1.8.19p2

Centos 7.3 sudo version:
Sudo version 1.8.6p7

We want to know the root cause of this issue and also want to know what has been changed in sudo package regarding this problem.
Please help.

Regards,
Shagun

User avatar
TrevorH
Site Admin
Posts: 33219
Joined: 2009/09/24 10:40:56
Location: Brighton, UK

Re: using 'exec sudo' command env variable $USER not getting updated

Post by TrevorH » 2018/03/23 07:26:03

Use sudo -i
The future appears to be RHEL or Debian. I think I'm going Debian.
Info for USB installs on http://wiki.centos.org/HowTos/InstallFromUSBkey
CentOS 5 and 6 are deadest, do not use them.
Use the FAQ Luke

shagun
Posts: 43
Joined: 2016/11/04 12:30:30

Re: using 'exec sudo' command env variable $USER not getting updated

Post by shagun » 2018/03/23 08:20:58

Thanks for the response.
I run the sudo -i command but it is not working for me because it is asking for a password.
But my question is what has been changed in CentOS 7.4 for sudo package So, the same command which was running in 7.3 is not running in 7.4?

Regards,
Shagun

stevemowbray
Posts: 519
Joined: 2012/06/26 14:20:47

Re: using 'exec sudo' command env variable $USER not getting updated

Post by stevemowbray » 2018/03/23 11:09:31

I would say test $EUID rather than $USER.

If you want to know about changes to versions of sudo look at the project page:
https://www.sudo.ws/

Post Reply