Why sudoing twice shows unexpected values for USER, USERNAME and LOGNAME environment variables

Solution Verified - Updated -

Issue

  • When sudoing twice, USER, USERNAME and LOGNAME environment variables contain root instead of the user we are logged in

    # sudo sudo -u testuser bash -c 'echo "LOGNAME=$LOGNAME; USER=$USER; USERNAME=$USERNAME"'
    LOGNAME=root; USER=root; USERNAME=root
    
  • When sudoing only once, USER, USERNAME and LOGNAME environment variables contain the login of the user we are logged in

    # sudo -u testuser bash -c 'echo "LOGNAME=$LOGNAME; USER=$USER; USERNAME=$USERNAME"'
    LOGNAME=testuser; USER=testuser; USERNAME=testuser
    

Environment

  • Red Hat Enterprise Linux 7.5 and later
  • sudo >= 1.8.19p2-10

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content