[CentOS-devel] Environment variable $USER not getting updated on using 'exec sudo command'

Nico Kadel-Garcia

nkadel at gmail.com
Wed Apr 11 13:56:37 UTC 2018


On Wed, Apr 11, 2018 at 2:31 AM, Veetil, Vyshnav
<Vyshnav.Veetil at harman.com> wrote:
> 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 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

The man page for sudo says you can use "sudo -H" to set the home
directory to that of the target user, and "sudo -i" to get closer to
their full login settings. But relying on USER.... can be erratic.
Definitely use "id -u" to report the numeric uid,  And, by the way?
Make sure to terminate loops like that, to avoid getting into an
infinite loop of shell commands calling shell commands and running
your system out of RAM or process ids.



More information about the CentOS-devel mailing list