When root, source another user's ~/.bashrc and get all environment variables

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I'm trying to develop a small system app that is run as root.
In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



What I'm trying to accomplish is equivalent to:



  • Change from root to user

  • source ~/.bashrc or ~/.profile etc.

  • printenv [and get it after the script]

  • Change to root again

There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



Any guidance is appreciated,



In case of missing information, please let me know.



Kind regards,







share|improve this question
























    up vote
    0
    down vote

    favorite












    I'm trying to develop a small system app that is run as root.
    In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



    What I'm trying to accomplish is equivalent to:



    • Change from root to user

    • source ~/.bashrc or ~/.profile etc.

    • printenv [and get it after the script]

    • Change to root again

    There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



    I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



    sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


    Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



    Any guidance is appreciated,



    In case of missing information, please let me know.



    Kind regards,







    share|improve this question






















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm trying to develop a small system app that is run as root.
      In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



      What I'm trying to accomplish is equivalent to:



      • Change from root to user

      • source ~/.bashrc or ~/.profile etc.

      • printenv [and get it after the script]

      • Change to root again

      There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



      I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



      sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


      Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



      Any guidance is appreciated,



      In case of missing information, please let me know.



      Kind regards,







      share|improve this question












      I'm trying to develop a small system app that is run as root.
      In this app, I would like to list all environment variables of the default user (not the $USER, but the user who calls runs the app as root, i.e. $SUDO_USER). Since the app runs not from a terminal and relies on a process opened by C in Qt environment, I would like to first prepare the environment, then get the environment variables in this subprocess.



      What I'm trying to accomplish is equivalent to:



      • Change from root to user

      • source ~/.bashrc or ~/.profile etc.

      • printenv [and get it after the script]

      • Change to root again

      There are some workarounds that I discovered and read online, but I found those workarounds (for running a command as another user) not helpful in what I'm trying to achieve.



      I need a one-liner for the aforementioned task. What I have right now is given below (run as root):



      sudo -i -u user sh -c '. /home/user/.bashrc && printenv > /tmp/user.env'


      Although I'm able to see a few environment variables with this, I cant see the custom exported ones from /home/user/.bashrc



      Any guidance is appreciated,



      In case of missing information, please let me know.



      Kind regards,









      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 7 at 18:06









      mozcelikors

      1316




      1316




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer




















          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
            – mozcelikors
            Feb 7 at 18:45










          • I've tested it on CentOS.
            – Kevin Lemaire
            Feb 7 at 18:46










          • If you su - user1 and then printenv, do you see the env variables you want?
            – Kevin Lemaire
            Feb 7 at 18:47










          • Yes, that works. If I printenv when I'm user. Then, it works.
            – mozcelikors
            Feb 7 at 18:48










          • But I need a one-liner that involves environment refresh
            – mozcelikors
            Feb 7 at 18:49










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422610%2fwhen-root-source-another-users-bashrc-and-get-all-environment-variables%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer




















          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
            – mozcelikors
            Feb 7 at 18:45










          • I've tested it on CentOS.
            – Kevin Lemaire
            Feb 7 at 18:46










          • If you su - user1 and then printenv, do you see the env variables you want?
            – Kevin Lemaire
            Feb 7 at 18:47










          • Yes, that works. If I printenv when I'm user. Then, it works.
            – mozcelikors
            Feb 7 at 18:48










          • But I need a one-liner that involves environment refresh
            – mozcelikors
            Feb 7 at 18:49














          up vote
          0
          down vote













          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer




















          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
            – mozcelikors
            Feb 7 at 18:45










          • I've tested it on CentOS.
            – Kevin Lemaire
            Feb 7 at 18:46










          • If you su - user1 and then printenv, do you see the env variables you want?
            – Kevin Lemaire
            Feb 7 at 18:47










          • Yes, that works. If I printenv when I'm user. Then, it works.
            – mozcelikors
            Feb 7 at 18:48










          • But I need a one-liner that involves environment refresh
            – mozcelikors
            Feb 7 at 18:49












          up vote
          0
          down vote










          up vote
          0
          down vote









          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >





          share|improve this answer












          I've tried your command and it seems to me that it's working just find.



          sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'


          I've used the source command instead of .



          With ~/.bashrc containing:



          [ws] root ~ >cat /home/user1/.bashrc 
          # .bashrc
          ...
          export TESTENV="test"
          ...


          execute your command and display the content of /tmp/user.env:



          [ws] root ~ >sudo -i -u user1 sh -c 'source ~/.bashrc && printenv > /tmp/user.env'

          [ws] root ~ >cat /tmp/user.env
          ...
          SHELL=/bin/bash
          USER=user1
          SUDO_COMMAND=/bin/bash -c sh -c source ~/.bashrc && printenv >
          ...
          TESTENV=test
          ...
          [ws] root ~ >






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 7 at 18:39









          Kevin Lemaire

          1,037421




          1,037421











          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
            – mozcelikors
            Feb 7 at 18:45










          • I've tested it on CentOS.
            – Kevin Lemaire
            Feb 7 at 18:46










          • If you su - user1 and then printenv, do you see the env variables you want?
            – Kevin Lemaire
            Feb 7 at 18:47










          • Yes, that works. If I printenv when I'm user. Then, it works.
            – mozcelikors
            Feb 7 at 18:48










          • But I need a one-liner that involves environment refresh
            – mozcelikors
            Feb 7 at 18:49
















          • That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
            – mozcelikors
            Feb 7 at 18:45










          • I've tested it on CentOS.
            – Kevin Lemaire
            Feb 7 at 18:46










          • If you su - user1 and then printenv, do you see the env variables you want?
            – Kevin Lemaire
            Feb 7 at 18:47










          • Yes, that works. If I printenv when I'm user. Then, it works.
            – mozcelikors
            Feb 7 at 18:48










          • But I need a one-liner that involves environment refresh
            – mozcelikors
            Feb 7 at 18:49















          That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
          – mozcelikors
          Feb 7 at 18:45




          That is really weird. That is exactly what I tried right now and it doesnt seem to work for me. I'm using Ubuntu 16.04. I don't get the variables in ~/.bashrc
          – mozcelikors
          Feb 7 at 18:45












          I've tested it on CentOS.
          – Kevin Lemaire
          Feb 7 at 18:46




          I've tested it on CentOS.
          – Kevin Lemaire
          Feb 7 at 18:46












          If you su - user1 and then printenv, do you see the env variables you want?
          – Kevin Lemaire
          Feb 7 at 18:47




          If you su - user1 and then printenv, do you see the env variables you want?
          – Kevin Lemaire
          Feb 7 at 18:47












          Yes, that works. If I printenv when I'm user. Then, it works.
          – mozcelikors
          Feb 7 at 18:48




          Yes, that works. If I printenv when I'm user. Then, it works.
          – mozcelikors
          Feb 7 at 18:48












          But I need a one-liner that involves environment refresh
          – mozcelikors
          Feb 7 at 18:49




          But I need a one-liner that involves environment refresh
          – mozcelikors
          Feb 7 at 18:49












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422610%2fwhen-root-source-another-users-bashrc-and-get-all-environment-variables%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?