Score:1

Environment configuration not reflected when switching users in Ubuntu 22.04

ru flag

I have two users named Alice and Sally on my Ubuntu 22.04 system. Alice has configured her environment variables in the .profile file, and they are working correctly for her. However, when Alice switches to the user Sally using the command sudo -s -u sally, the environment settings from Alice's .profile are not being reflected for Sally.

alice@dev7 192 0 % echo $appuser     
alice
alice@dev7 193 0 % env  | grep appuser
appuser=alice
alice@dev7 194 0 % sudo -s -u sally
sally@dev7 196 0 % echo $appuser

sally@dev7 197 0 %

I have verified that the .profile file is present in Sally's home directory and has the necessary configuration. I would like to understand why the environment variables are not being applied for Sally when Alice switches to her user using sudo -s -u sally. Are there any additional steps or configurations required to ensure that the environment settings are carried over when switching users in this manner?

hr flag
Your question is confusing imho - you ask about environment settings being "carried over" (which suggests you are expecting the variables to be *inherited*) but also mention that you "verified that the .profile file is present in Sally's home directory and has the necessary configuration" (which suggests you expect them to be read by Sally's shell). Which are really asking about?
Score:1
jp flag

~/.profile is sourced on user login ... Therefore, withe sudo -u user you need to use the -i, --login in order to "run the shell specified by the target user's password database entry as a login shell" like so:

sudo -i -u sally

Notice: You may not specify both the -i and -s options.

If, however, you want to preserve your current users' choice of shell, then you can use sudo su ... like so:

sudo su - sally --shell "$SHELL"

or specify your choice of shell e.g. /bin/bash like so:

sudo su - sally --shell /bin/bash

regardless of the shell specified by the target user's password database entry.

Vikram  avatar
ru flag
Not working env not reflecting
Raffa avatar
jp flag
@Vikram `env` needs the variable to be exported like `export appuser=alice` in order to reflect it ... Make sure that is the case in the target user's `~/.profile` file and not just assigning it like `appuser=alice`
Vikram  avatar
ru flag
Yes its already presnet as mentioned above but not reflecting. Also, note here another thing is that the home environment variables is not reflecting previously I asked one question but no answers FYR : https://askubuntu.com/questions/1471640/dot-gitconfig-file-not-reflecting-when-switch-user-on-ubuntu-22-04-lts
Raffa avatar
jp flag
@Vikram Can you please edit your question to add the relevant part in **sally's** `/home/sally/.profile` file and alice's `.profile` as well.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.